reduce options in filter menu

3 posts, 1 answers
  1. Ashraf
    Ashraf avatar
    40 posts
    Member since:
    Aug 2012

    Posted 12 Sep 2012 Link to this post

    hi,
    kindly help me .
    how to reduce options in filter menu in rad grid according to column type from server side ?
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Sep 2012 Link to this post

    Hi,

        There is a single filtering menu object server-side.The filtering menu is independent for each column in RadGrid - this means that the filtering menu options vary by the DataType of the corresponding column. If you remove some of the options from the menu on the server side, this will affect all grid columns and they will be stripped from each column filter menu options.
         Please try client side to limit the filter options displayed for a given column, you need to intercept the OnFilterMenuShowingclient event of RadGrid/OnClientShown event of the filter menu and hide some of the possible choices from within the body of the respective handler.Below is a sample code that customizes the filter menu options for columns according to data type

    ASPX:
    <telerik:RadGrid AutoGenerateColumns="false" ID="RadGrid1" DataSourceID="SqlDataSource1"
     AllowFilteringByColumn="True" runat="server" EnableLinqExpressions="false">
        <MasterTableView >
                <Columns>
                        <telerik:GridNumericColumn DataField="OrderID" HeaderText="OrderID"
                        UniqueName="OrderID"  DataType="System.Int64">
                        </telerik:GridNumericColumn>
                        <telerik:GridBoundColumn  DataField="ShipName" HeaderText="ShipName"
                         UniqueName="ShipName" DataType="System.String">
                        </telerik:GridBoundColumn>
                        <telerik:GridDateTimeColumn DataField="ShippedDate" HeaderText="ShippedDate UniqueName="ShippedDate"                                          PickerType="DatePicker" DataFormatString="{0:D}" DataType="System.DateTime">
                        </telerik:GridDateTimeColumn>
                        <telerik:GridMaskedColumn DataField="ShipPostalCode" HeaderText="ShipPostalCode" UniqueName="ShipPostalCode">
                        </telerik:GridMaskedColumn>
                        <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal" HeaderText="Freight" UniqueName="Freight">
                        </telerik:GridNumericColumn>
                </Columns>
        </MasterTableView>
        <ClientSettings>
            <Scrolling AllowScroll="false" />
            <ClientEvents OnFilterMenuShowing="filterMenuShowing" />
        </ClientSettings>
    <FilterMenu OnClientShown="MenuShowing" />
    </telerik:RadGrid>

    Javascript:
    <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_dataType() == "System.String") {
                var i = 0;
                while (i < items.get_count()) {
                    if (!(items.getItem(i).get_value() in { 'NoFilter': '', 'Contains': '', 'NotIsEmpty': '', 'IsEmpty': '', 'NotEqualTo': '', 'EqualTo': '' })) {
                        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++;
                }
            }
            if (column.get_dataType() == "System.Int64") {
                var j = 0; while (j < items.get_count()) {
                    if (!(items.getItem(j).get_value() in { 'NoFilter': '', 'GreaterThan': '', 'LessThan': '', 'NotEqualTo': '', 'EqualTo': '' })) {
                        var item = items.getItem(j); if (item != null)
                            item.set_visible(false);
                    }
                    else { var item = items.getItem(j); if (item != null) item.set_visible(true); } j++;
                }
            }
            column = null;
            menu.repaint();
        }
        function filterMenuShowing(sender, eventArgs) {
            column = eventArgs.get_column();
        }
    </script>

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ashraf
    Ashraf avatar
    40 posts
    Member since:
    Aug 2012

    Posted 13 Sep 2012 Link to this post

    thanks Shinu.
    i read your answer at documentation .
    i tried other solution to avoid bad design ,
     this is the better :
    <telerik:RadCodeBlock ID="DefaultRadCodeBlock" runat="server">
            <script type="text/javascript">
            var oldFilterStyle = "";
            function filterMenuShowing(sender, eventArgs) {
                var menu = eventArgs.get_menu();
                var items = menu.get_items();
                if (eventArgs.get_column().get_dataType() == "System.String") {
                    var i = 0;
                    while (i < items.get_count()) {
                        if (items.getItem(i).get_value() != "StartsWith" && items.getItem(i).get_value() != "Contains" && items.getItem(i).get_value() != "EqualTo") {
                            var item = items.getItem(i);
                            if (item != null) {
                                if (item.get_element().className != "hideFilterOption") {
                                    oldFilterStyle = item.get_element().className;
                                }
                                item.get_element().className = "hideFilterOption";
                            }
                        }
                        else {
                            var item = items.getItem(i);
                            if (item != null) {
                                item.get_element().className = oldFilterStyle;
                            }
                        }
                        i++;
                    }
                }
                else if (eventArgs.get_column().get_dataType() == "System.Int32") {
                    var i = 0;
                    while (i < items.get_count()) {
                        if (items.getItem(i).get_value() == "IsNull" || items.getItem(i).get_value() == "NotIsNull") {
                            var item = items.getItem(i);
                            if (item != null) {
                                if (item.get_element().className != "hideFilterOption") {
                                    oldFilterStyle = item.get_element().className;
                                }
                                item.get_element().className = "hideFilterOption";
                            }
                        }
                        else {
                            var item = items.getItem(i);
                            if (item != null) {
                                item.get_element().className = oldFilterStyle;
                            }
                        }
                        i++;
                    }
                }
                else {
                    var i = 0;
                    while (i < items.get_count()) {
                        var item = items.getItem(i);
                        if (item != null)
                            if (oldFilterStyle != "" && item.get_element().className == "hideFilterOption")
                                item.get_element().className = oldFilterStyle;
                        i++;
                    }
                }
            }
            </script>
        </telerik:RadCodeBlock>

Back to Top