This is a migrated thread and some comments may be shown as answers.

RadGrid Filter With CheckBoxes Inside RadComboBox

2 Answers 283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 13 Apr 2011, 12:12 PM
Hi,

I'm trying to create a SQL-style WHERE ColumnName IN('a', 'b', 'c') filter for one of my RadGrid columns.

In my RadGrid I have a GridBoundColumn using a FilterTemplate which has a RadComboBox with a number of CheckBoxes inside it.

<telerik:GridBoundColumn HeaderText="Type" DataField="Type" UniqueName="Type" AutoPostBackOnFilter="true">
    <FilterTemplate>
        <telerik:RadComboBox 
            ID="cmbType"
            runat="server"
            DataSource="<%# TypeFilterItems %>"
            DataValueField="TypeId"
            DataTextField="Title"
            EmptyMessage="All"                
            AllowCustomText="true"
            Width="130px"
            OnClientDropDownClosing="FilterOnType">
            <ItemTemplate>
 
                <div class="combo-item-template">
                    <div class="clear">
                        <asp:CheckBox runat="server" ID="chkType" Checked="true" onclick="TypeCheckBoxClick(this)" style="float:left;" />
                        <p class="floatLeft"><%# Eval("Title") %></p>
                    </div>
                </div>
 
            </ItemTemplate>
 
        </telerik:RadComboBox>
 
        <telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">
            <script type="text/javascript">
                function FilterOnType(sender, args) {
                    var tableView = $find("<%# Container.OwnerTableView.ClientID %>");
                    tableView.filter("Type", text, "Custom");
                }
 
                function TypeCheckBoxClick(chk) {
                    var combo = $find('<%# Container.FindControl("cmbType").ClientID %>');
 
                    // Get the collection of all items
                    var items = combo.get_items();
 
                    text = ""; values = "";
 
                    // Enumerate all items
                    for (var i = 0; i < items.get_count(); i++) {
                        var item = items.getItem(i);
 
                        // Get the checkbox element of the current item
                        var chk1 = $get(combo.get_id() + "_i" + i + "_chkType");
                        if (chk1.checked) {
                            text += item.get_text() + ",";
                            values += item.get_value() + ",";
                        }
                    }
 
                    // Remove the last comma from the string
                    text = text.replace(/,$/, "");
                    values = values.replace(/,$/, "");
                    combo.set_text(text);
                }
            </script>
        </telerik:RadScriptBlock>
    </FilterTemplate>
</telerik:GridBoundColumn>

This part is all working fine.  What I want to do is tell the RadGrid to filter and show only the options the user has ticked in the RadComboBox's CheckBoxes.

e.g.: If the user open the ComboBoxList filter and selects A, B and D, I'd like the grid to filter and show only results in that column which are A, B or D.  Is this possible with native RadGrid filtering?  And if not, any ideas on the best was to achieve it?

Cheers,

Adam

2 Answers, 1 is accepted

Sort by
0
Adam
Top achievements
Rank 1
answered on 13 Apr 2011, 02:10 PM
I've found a way of doing this, though not by using any built in RadGrid functionality.

The JavaScript in my initial example allows you to gather a comma separated list from the CurrentFilterValue of the column.

TypeFilter = grdActions.MasterTableView.GetColumn( "Type" ).CurrentFilterValue;

I've then used this to filter my DataSource before I bind it to the grid and allow the RadGrid to do its filtering.

/// <summary>Removes all types which arent in filter.</summary>
/// <param name="actions">The actions.</param>
/// <param name="filterItems">The filter items.</param>
private void RemoveAllTypesWhichArentInFilter( List<ActionTaskListItem> actions, string filterItems )
{
    var filters = new List<string>( filterItems.Split( ',' ) );
     
    var actionsToRemove = (from currentAction in actions
                           let exists = filters.Exists(filter => currentAction.Type == filter)
                           where exists == false
                           select currentAction).ToList();
 
    foreach (var actionToRemove in actionsToRemove)
    {
        actions.Remove( actionToRemove );
    }
}

Hope this helps someone else!
0
Brian
Top achievements
Rank 1
answered on 20 Jul 2011, 09:35 PM
A complete working example of this would be great!!!   Seems to cut up to follow.
Tags
Grid
Asked by
Adam
Top achievements
Rank 1
Answers by
Adam
Top achievements
Rank 1
Brian
Top achievements
Rank 1
Share this question
or