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

How to create custom filter menu for one column

4 Answers 200 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 20 Jan 2011, 03:42 PM
Greetings,

I created a custom filter menu that I want to use for one of my radgrid columns. It works fine, but the problem is the menu appears for all columns. I only want this menu to appear for the column "Constituents." Note that I can't use "VaryByDataType" because my columns are all strings. Is it possible to create a custom filter menu for just one column?

ASPX:
<telerik:RadGrid ID="radGridForms" runat="server" AutoGenerateColumns="false"
                GridLines="None" Skin="Windows7" AllowFilteringByColumn="True" AllowPaging="True"
                AllowSorting="True" EnableLinqExpressions="False" Font-Bold="False" Font-Italic="False"
                 Font-Overline="False" Font-Size="Small" Font-Strikeout="False" Font-Underline="False" 
                 OnItemDataBound="radGridForms_ItemDataBound" OnNeedDataSource="radGridForms_NeedDataSource">
                <ClientSettings Selecting-AllowRowSelect="false" EnableRowHoverStyle="true">
                    <ClientEvents />
                </ClientSettings>
                <PagerStyle Position="Top" />
                <MasterTableView PageSize="10" AllowSorting="true" EnableNoRecordsTemplate="true" ShowHeadersWhenNoRecords="true"
                    Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Size="Small"
                    Font-Strikeout="False" Font-Underline="False" DataKeyNames="FormID" >
                    <RowIndicatorColumn>
                        <HeaderStyle Width="20px"></HeaderStyle>
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn>
                        <HeaderStyle Width="20px"></HeaderStyle>
                    </ExpandCollapseColumn>
                    <Columns>
                        <telerik:GridBoundColumn DataField="FormID" UniqueName="FormID" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridHyperLinkColumn DataNavigateUrlFields="PDFLink"
                            DataNavigateUrlFormatString="http://webcms/{0}" UniqueName="PDFLink"
                            AllowFiltering="false" HeaderText="Download" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </telerik:GridHyperLinkColumn>
                        <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description"
                            CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                            AllowSorting="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="State" HeaderText="State" UniqueName="State"
                            CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                            AllowSorting="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Constituents" HeaderText="Constituents" UniqueName="Constituents"
                            CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                            AllowSorting="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Keywords" HeaderText="Keywords" UniqueName="Keywords"
                            CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                            AllowSorting="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Version" AllowFiltering="false" ItemStyle-Font-Size="XX-Small" 
                            HeaderText="Ver" UniqueName="Version" >
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Type" AllowFiltering="false" ItemStyle-Font-Size="XX-Small" 
                            HeaderText="Type" UniqueName="Type" AllowSorting="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Secure" UniqueName="Secure" Visible="false">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>


C# CodeBehind:
protected void Page_Init(object senderEventArgs e)
        {
            radGridForms.FilterMenu.Items.Clear();
            GridFilterMenu filterMenu = radGridForms.FilterMenu;
            RadMenuItem onlyEmployerMenuItem = new RadMenuItem();
            RadMenuItem onlyProducerMenuItem = new RadMenuItem();
            RadMenuItem onlyIndividualsMenuItem = new RadMenuItem();
            
            radGridForms.FilterMenu.Items.Add(onlyEmployerMenuItem);
            radGridForms.FilterMenu.Items.Add(onlyProducerMenuItem);
            radGridForms.FilterMenu.Items.Add(onlyIndividualsMenuItem);
            
            onlyEmployerMenuItem.Text = "Show Only Employers";
            onlyEmployerMenuItem.Value = "Employers";
            onlyProducerMenuItem.Text = "Show Only Producers";
            onlyProducerMenuItem.Value = "Producers";
            onlyIndividualsMenuItem.Text = "Show Only Individuals";
            onlyIndividualsMenuItem.Value = "Individuals";
            filterMenu.ItemClick += new RadMenuEventHandler(filterMenu_ItemClick);

        }

        protected void filterMenu_ItemClick(object senderRadMenuEventArgs e)
        {
            if (e.Item.Value.ToString() == "Employers")
            {
                radGridForms.MasterTableView.FilterExpression = "([Constituents] = 'Employers') ";
                GridColumn column = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column.CurrentFilterFunction = GridKnownFunction.Contains;
                radGridForms.DataSource = null;
                radGridForms.Rebind();
            }
            else if(e.Item.Value.ToString() == "Producers")
            {
                radGridForms.MasterTableView.FilterExpression = "([Constituents] = 'Producers') ";
                GridColumn column = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column.CurrentFilterFunction = GridKnownFunction.Contains;
                radGridForms.DataSource = null;
                radGridForms.Rebind();
            }
            else if (e.Item.Value.ToString() == "Individuals")
            {
                radGridForms.MasterTableView.FilterExpression = "([Constituents] = 'Individuals') ";
                GridColumn column = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column.CurrentFilterFunction = GridKnownFunction.Contains;
                radGridForms.DataSource = null;
                radGridForms.Rebind();
            }
        }

4 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 25 Jan 2011, 03:25 PM
Hello Jon,

The grid menu object is cloned for each column for optimization issues hence its options are the same for each column. However you can show/hide some of the operations in the menu depending on the name of the column.
First server-side in the Init event you should not clear the default menu items (because they will be needed for the other column) but only add the new items you want:

protected void Page_Init(object sender, EventArgs e)
    {
        //radGridForms.FilterMenu.Items.Clear();
        GridFilterMenu filterMenu = radGridForms.FilterMenu;
        RadMenuItem onlyEmployerMenuItem = new RadMenuItem();
        RadMenuItem onlyProducerMenuItem = new RadMenuItem();
        RadMenuItem onlyIndividualsMenuItem = new RadMenuItem();
 
        radGridForms.FilterMenu.Items.Add(onlyEmployerMenuItem);
        radGridForms.FilterMenu.Items.Add(onlyProducerMenuItem);
        radGridForms.FilterMenu.Items.Add(onlyIndividualsMenuItem);
 
        onlyEmployerMenuItem.Text = "Show Only Employers";
        onlyEmployerMenuItem.Value = "Employers";
        onlyProducerMenuItem.Text = "Show Only Producers";
        onlyProducerMenuItem.Value = "Producers";
        onlyIndividualsMenuItem.Text = "Show Only Individuals";
        onlyIndividualsMenuItem.Value = "Individuals";
        filterMenu.ItemClick += new RadMenuEventHandler(filterMenu_ItemClick);
 
    }

Then we use the client side OnFilterMenuShowing event of the grid to hide some the unnecessary items depending on the column. You can refer to this help article for more detailed information. It demonstrates similar approach depending on the type of the column. We can easily modify the logic to rely on the column name like that:

var column = null;
           function MenuShowing(sender, args) {
               if (column == null)
                   return;
               var menu = sender;
               var items = menu.get_items();
               if (column.get_uniqueName() == "Constituents") {
                   //hide the default items
                   //leave visible only the newly added custom items
               }
               else {
                    // hide the custom items
               }
               column = null;
           }
           function filterMenuShowing(sender, eventArgs) {
               column = eventArgs.get_column();
           }

Regards,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Jon
Top achievements
Rank 1
answered on 25 Jan 2011, 07:16 PM
Thanks Marin,

Your solution did the trick.
0
Jon
Top achievements
Rank 1
answered on 26 Jan 2011, 04:29 PM
Greetings,

I hit another snag with my code and I am stumped about how to handle it. When I select one of my custom menu options, I sometimes get a javascript error:
"Sys.ArgumentException: 'ContainProducers' is not a valid value for enum Telerik.Web.UI.GridFilterFunction. Parameter name: value"

I can click one of these menu options the first time and it works fine but then I sometimes get the javascript error on subsequent menu clicks.

The code is below, and if anyone has so me insight into my javascript problem, I would greatly appreciate it.

ASPX Javascript:

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
    var column = null;
    function MenuShowing(sender, args) 
    {

       if(column == null)
            return;
        var menu = sender;
        var items = menu.get_items();
        if (column.get_uniqueName() == "Constituents"
        {
            //hide the default items
            var i = 0;
            while (i < items.get_count()) 
            {

                if (!(items.getItem(i).get_value() in
                { 'Employers''''Producers''''Individuals''''ContainEmployers''''ContainProducers''''ContainIndividuals''' })) {
                    var item = items.getItem(i);
                    if (item != null)
                        item.set_visible(false);
                }
                else
                {
                    var item = items.getItem(i);
                    if (item != null)
                        item.set_visible(true);
                }
                i++;
            }
        }

        column = null;
    }

    function filterMenuShowing(sender, eventArgs) 
    {
        column = eventArgs.get_column();
    }

    </script>
</telerik:RadCodeBlock> 

ASPX Ajax and Markup for Grid:

        <telerik:RadGrid ID="radGridForms" runat="server" AutoGenerateColumns="false"
                 AllowFilteringByColumn="True" Font-Bold="False" Font-Italic="False"
                 AllowPaging="True" Font-Overline="False" Font-Size="Small"
                 AllowSorting="True" Font-Strikeout="False" Font-Underline="False"
                 OnItemDataBound="radGridForms_ItemDataBound" Skin="Windows7"
                 OnNeedDataSource="radGridForms_NeedDataSource" GridLines="None"
                 EnableLinqExpressions="False">
            <ExportSettings HideStructureColumns="true" />

            <ClientSettings Selecting-AllowRowSelect="false" EnableRowHoverStyle="true">
                <ClientEvents OnFilterMenuShowing="filterMenuShowing"/>
            </ClientSettings>
            <PagerStyle Position="TopAndBottom" />

            <MasterTableView PageSize="50" AllowSorting="true" EnableNoRecordsTemplate="true" ShowHeadersWhenNoRecords="true"
                    Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Size="Small"
                    Font-Strikeout="False" Font-Underline="False" AllowFilteringByColumn="true" 
                    DataKeyNames="FormID" >
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                
                <Columns>
                    <telerik:GridHyperLinkColumn Target="_blank" DataNavigateUrlFields="PDFLink"
                        DataNavigateUrlFormatString="http://webcms/{0}" UniqueName="PDFLink"
                        AllowFiltering="false" HeaderText="Download" ItemStyle-HorizontalAlign="Center">
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </telerik:GridHyperLinkColumn>
                    <telerik:GridBoundColumn DataField="FormID" UniqueName="FormID"  ItemStyle-Font-Size="XX-Small" 
                    HeaderText="Form ID" AllowFiltering="true" AllowSorting="true">
                    </telerik:GridBoundColumn>

                    <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description"
                        CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                        AllowSorting="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="State" HeaderText="State" UniqueName="State"
                        CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                        AllowSorting="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Constituents" ItemStyle-Font-Size="XX-Small" HeaderText="Constituents" UniqueName="Constituents" 
                    AllowFiltering="true" AllowSorting="true"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Keywords" HeaderText="Keywords" UniqueName="Keywords"
                        CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ItemStyle-Font-Size="XX-Small" 
                        AllowSorting="true">
                    </telerik:GridBoundColumn>

                    <telerik:GridBoundColumn DataField="Type" AllowFiltering="true" ItemStyle-Font-Size="XX-Small" 
                        HeaderText="Type" UniqueName="Type" AllowSorting="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Secure" UniqueName="Secure" Visible="false">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <FilterMenu OnClientShowing="MenuShowing"></FilterMenu>
        </telerik:RadGrid>

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="radGridForms">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="Fieldset2" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Windows7">
 </telerik:RadAjaxLoadingPanel>

C# Codebehind:

  protected void Page_Init(object senderEventArgs e)
        {

            GridFilterMenu filterMenu = radGridForms.FilterMenu;
            RadMenuItem onlyEmployerMenuItem = new RadMenuItem();
            RadMenuItem containsEmployerMenuItem = new RadMenuItem();
            RadMenuItem onlyProducerMenuItem = new RadMenuItem();
            RadMenuItem containsProducerMenuItem = new RadMenuItem();
            RadMenuItem onlyIndividualsMenuItem = new RadMenuItem();
            RadMenuItem containsIndividualsMenuItem = new RadMenuItem();

            radGridForms.FilterMenu.Items.Add(onlyEmployerMenuItem);
            radGridForms.FilterMenu.Items.Add(containsEmployerMenuItem);
            radGridForms.FilterMenu.Items.Add(onlyProducerMenuItem);
            radGridForms.FilterMenu.Items.Add(containsProducerMenuItem);
            radGridForms.FilterMenu.Items.Add(onlyIndividualsMenuItem);
            radGridForms.FilterMenu.Items.Add(containsIndividualsMenuItem);

            onlyEmployerMenuItem.Text = "Show Only Employers";
            onlyEmployerMenuItem.Value = "Employers";
            containsEmployerMenuItem.Text = "Show Records that Contain Employers";
            containsEmployerMenuItem.Value = "ContainEmployers";
            onlyProducerMenuItem.Text = "Show Only Producers";
            onlyProducerMenuItem.Value = "Producers";
            containsProducerMenuItem.Text = "Show Records that Contain Producers";
            containsProducerMenuItem.Value = "ContainProducers";
            onlyIndividualsMenuItem.Text = "Show Only Individuals";
            onlyIndividualsMenuItem.Value = "Individuals";
            containsIndividualsMenuItem.Text = "Show Records that Contain Individuals";
            containsIndividualsMenuItem.Value = "ContainIndividuals";
            filterMenu.ItemClick += new RadMenuEventHandler(filterMenu_ItemClick);

        }

        protected void filterMenu_ItemClick(object senderRadMenuEventArgs e)
        {
            switch (e.Item.Value.ToString() )
            {
                case "Employers":
                radGridForms.MasterTableView.FilterExpression = "([Constituents] = 'Employers') ";
                GridColumn column = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column.CurrentFilterFunction = GridKnownFunction.Contains;
                radGridForms.DataSource = null;
                radGridForms.Rebind();
                break;
                
                case "Producers":
                radGridForms.MasterTableView.FilterExpression = "([Constituents] = 'Producers') ";
                GridColumn column2 = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column2.CurrentFilterFunction = GridKnownFunction.Contains;
                radGridForms.DataSource = null;
                radGridForms.Rebind();
                break;
                
                case "Individuals":
                radGridForms.MasterTableView.FilterExpression = "([Constituents] = 'Individuals') ";
                GridColumn column3 = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column3.CurrentFilterFunction = GridKnownFunction.Contains;
                radGridForms.DataSource = null;
                radGridForms.Rebind();
                break;

                case "ContainEmployers":
                radGridForms.MasterTableView.FilterExpression = "([Constituents] LIKE \'%Employers%\') ";
                GridColumn column4 = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column4.CurrentFilterFunction = GridKnownFunction.Contains;
                column4.CurrentFilterValue = "Employers";
                radGridForms.DataSource = null;
                radGridForms.Rebind();
                break;

                case "ContainProducers":
                radGridForms.MasterTableView.FilterExpression = "([Constituents] LIKE \'%Producers%\') ";
                GridColumn column5 = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column5.CurrentFilterFunction = GridKnownFunction.Contains;
                column5.CurrentFilterValue = "Producers";
                radGridForms.DataSource = null;
                radGridForms.Rebind();
                break;

                case "ContainIndividuals":
                radGridForms.MasterTableView.FilterExpression = "([Constituents] LIKE \'%Individuals%\') ";
                GridColumn column6 = radGridForms.MasterTableView.GetColumnSafe("Constituents");
                column6.CurrentFilterFunction = GridKnownFunction.Contains;
                column6.CurrentFilterValue = "Individuals";
                radGridForms.DataSource = null;
                radGridForms.Rebind();
                break;

            }
        }
0
Jon
Top achievements
Rank 1
answered on 26 Jan 2011, 05:18 PM
Update.

I found the problem. In the bottom 3 entries for the switch statement, I had a line that sets the CurrentFilterValue, e.g. ,

column6.CurrentFilterValue = "Individuals";


I removed this line from the 3 cases that used it and everything works correctly now. Just an FYI in case someone wants to do something similar.
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Marin
Telerik team
Jon
Top achievements
Rank 1
Share this question
or