简体   繁体   English

删除单个RadGrid列的过滤器

[英]Remove the filter for a single RadGrid column

I am using a Telerik RadGrid and have custom filters set up on some of the columns. 我正在使用Telerik RadGrid,并在某些列上设置了自定义过滤器。 As part of that custom filter (which is just a textbox) I also have a button that when clicked I want to clear the filter value for that particular column only. 作为自定义过滤器(它只是一个文本框)的一部分,我还有一个按钮,当单击时我想清除该特定列的过滤器值。

I have the button working and am setting the filter for the particular column to an empty string, then rebinding the grid but this does not remove the filter value. 我有按钮工作,并将特定列的过滤器设置为空字符串,然后重新绑定网格,但这不会删除过滤器值。

It seems that only modifying the grid.MasterTableView.FilterExpression actually affects the filter but how can I remove just one column's filter values from the FilterExpression? 似乎只修改grid.MasterTableView.FilterExpression实际上会影响过滤器但是如何从FilterExpression中只删除一列的过滤器值?

<telerik:RadTextBox ID="RdTxtC" EmptyMessage="Search" runat="server" EmptyMessageStyle-Font-Italic="true"
    CssClass="padtop4" HoveredStyle-Font-Italic="true" ClientEvents-OnKeyPress="filterC"
    InputType="Text" Width="80%">
</telerik:RadTextBox>
<asp:Button ID="ibtnClearFilterTown" ClientIDMode="Static" runat="server" CssClass="clearFilterButton" OnClick="ibtnClearFilterTown_Click"/>

In the code-behind: 在代码隐藏中:

protected void ibtnClearFilterTown_Click(object sender, EventArgs e)
{
    RgClientList.MasterTableView.GetColumnSafe("TownCity").CurrentFilterValue = String.Empty;
    RgClientList.Rebind();
}

It seems a fairly simple requirement to remove the filter for a single column so i think I must be missing something obvious - any help is appreciated.... 对于单个列移除过滤器似乎是一个相当简单的要求,所以我认为我必须遗漏一些明显的东西 - 任何帮助都是值得赞赏....

Go ahead and try this instead of what you have in your code: 继续尝试这个而不是代码中的内容:

protected void ibtnClearFilterTown_Click(object sender, EventArgs e)
{
   RgClientList.MasterTableView.GetColumnSafe("TownCity").CurrentFilterFunction = GridKnownFunction.NoFilter;
   RgClientList.MasterTableView.GetColumnSafe("TownCity").CurrentFilterValue = String.Empty;
   RgClientList.Rebind();

}

Also, according to this question asked on SO , you may need to change the following property on your Radgrid: 另外,根据SO上提出的这个问题 ,您可能需要在Radgrid上更改以下属性:

EnableLinqExpressions="False"

EDIT 1: (put this in the button press) 编辑1 :(按下按钮)

I understand this looks like a lot to do what you wish, but I checked the documentation for Telerik and did not see an easy way of simply removing one filter string from the MasterTableView.FilterExpression. 我明白这看起来很像你想做的事情,但我检查了Telerik的文档,并没有看到简单的方法从MasterTableView.FilterExpression中删除一个过滤字符串。 This is mainly due to the way each column stores its filter as text and not the value that goes into the final filter string. 这主要是由于每个列将其过滤器存储为文本的方式,而不是进入最终过滤器字符串的值。

I'll walk you through the steps involved below: 我将引导您完成以下步骤:

  • It creates a collection of filters from each column, removes the one you do not want (columnwhosfilteryouwantremoved), and then loops through the columns and clears the column you wish to set = "" 它从每列创建一个过滤器集合,删除您不想要的过滤器(columnwhosfilteryouwantremoved),然后遍历列并清除您想要设置的列=“”
  • Then it sets the the filter for the grid to its new value and then rebinds. 然后它将网格的过滤器设置为新值,然后重新绑定。

     String MasterFilterExp = ""; var tempCollection = (from String sFilt in RadGrid1.MasterTableView.FilterExpression.Split(new string[] {"AND"},StringSplitOptions.None) where sFilt.Contains("[columnnameoffilteryouwantremoved]") != true select sFilt).ToArray(); MasterFilterExp = string.Join(" AND ", tempCollection); foreach (GridColumn column in RadGrid1.MasterTableView.Columns) { if (column.UniqueName == "columnnameoffilteryouwantremoved") { column.CurrentFilterFunction = GridKnownFunction.NoFilter; column.CurrentFilterValue = String.Empty; } } RadGrid1.MasterTableView.FilterExpression = MasterFilterExp; RadGrid1.Rebind(); 

It can probably be simplified, but let me know if you have any questions. 它可以简化,但如果您有任何问题,请告诉我。 I tested it and was able to remove a column's filter from the grid and rebind to show the updated info. 我测试了它,并能够从网格中删除列的过滤器并重新绑定以显示更新的信息。

Why dont you try it with Javacript: 你为什么不试试Javacript:

function FilterMenuShowing(sender, eventArgs)   
   {     
       if (eventArgs.get_column().get_uniqueName() == "IsPostable")      
       {              
           var menu = eventArgs.get_menu();   
           var items = menu._itemData;   

           var i = 0;   

           while (i < items.length)     
           {     
                   var item = menu._findItemByValue(items[i].value);   
                   if (item != null)   
                       item._element.style.display="none";   
               i++;     
           }
       }
 }

You can see details here 你可以在这里看到细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM