自定义日期 colmn telerik 网格的过滤器选项
Posted
技术标签:
【中文标题】自定义日期 colmn telerik 网格的过滤器选项【英文标题】:customize the filter option for a date colmn telerik grid 【发布时间】:2011-05-19 17:36:25 【问题描述】:我正在使用 Telerik 网格。我需要为网格中的所有列应用过滤器。目前我正在使用以下代码自定义过滤器选项。通过使用以下代码,我将删除所有列的某些项目。但是,对于日期列,谁能告诉我在网格中过滤的可能选项是什么以及如何自定义这些过滤选项?
代码背后
protected void RGVTest_Init(object sender, EventArgs e)
GridFilterMenu menu = RGVTest.FilterMenu;
int i = 0;
while (i < menu.Items.Count)
if (menu.Items[i].Text == "Between" ||
menu.Items[i].Text == "NotBetween")
menu.Items.RemoveAt(i);
else
i++;
*aspx:*
<telerik:RadGrid ID="RGVTest" runat="server" Skin="Vista" AllowPaging="True"
AllowFilteringByColumn="true" AllowSorting="true" GridLines="None" OnItemCommand="RGVTest_ItemCommand"
PageSize="10" OnNeedDataSource="RGVTest_NeedDataSource" OnItemDataBound="RGVTest_ItemDataBound"
OnInit="RGVTest_Init">
<GroupingSettings CaseSensitive="false" />
<PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
<MasterTableView AutoGenerateColumns="False" CellSpacing="-1" >
<NoRecordsTemplate>
<div style="color: red">
No Records to display!
</div>
</NoRecordsTemplate>
<Columns>
<telerik:GridTemplateColumn DataField="SSN" ReadOnly="True" HeaderText="SSN" UniqueName="SSN"
SortExpression="SSN">
<ItemTemplate>
<asp:Label ID="LblSSN" runat="server" Text='<%#Eval("SSN") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="5%" />
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="Date" HeaderText="Date" UniqueName="Date"
SortExpression="Date">
<ItemTemplate>
<asp:Label ID="LblDate" runat="server" Text='<%#Eval("Date","0:MM/dd/yyyy") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="4%" />
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
【问题讨论】:
【参考方案1】:你确实喜欢这样:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
if (e.CommandName == RadGrid.FilterCommandName)
Pair filterPair = e.CommandArgument as Pair;
string columnName = Convert.ToString(filterPair.Second);
if (columnName == "CreationDate")
e.Canceled = true;
string date = ((TextBox)((GridFilteringItem)e.Item)[Convert.ToString(filterPair.Second)].Controls [0]).Text;
DateTime startDate = Convert.ToDateTime(date);
DateTime endDate = startDate.AddDays(1);
string newFilter = "('" + startDate.ToString("MM/dd/yyyy") + "' <= [CreationDate] AND [CreationDate] <= '" + endDate.ToString("MM/dd/yyyy") + "')";
GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnName);
dateColumn.CurrentFilterValue = startDate.ToString("MM/dd/yyyy");
RadGrid1.MasterTableView.FilterExpression = newFilter;
RadGrid1.Rebind();
【讨论】:
【参考方案2】:这样试试
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)
if (items[i].value != "NoFilter" && items[i].value != "EqualTo" && items[i].value != "NotEqualTo")
var item = menu._findItemByValue(items[i].value);
if (item != null)
item._element.style.display="none";
i++;
else
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="";
i++;
在您的网格上添加-
<ClientSettings>
<ClientEvents OnFilterMenuShowing="filterMenuShowing" />
</ClientSettings>
查看详情here
【讨论】:
【参考方案3】:You can try the way its done below for "order date" column.
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
if (e.CommandName == RadGrid.FilterCommandName &&
((Pair)e.CommandArgument).Second.ToString() == "OrderDate"
&&
((Pair)e.CommandArgument).First != "NoFilter")
e.Canceled = true;
GridFilteringItem filterItem = (GridFilteringItem)e.Item;
string currentPattern = (filterItem[((Pair)e.CommandArgument).Second.ToString()].Controls[0] as TextBox).Text;
string filterPattern = "";
string filterPatternAssist = "";
if (currentPattern.IndexOf(" ") != -1)
currentPattern = currentPattern.Replace(" ", "/");
string[] vals = currentPattern.Split("/".ToCharArray());
string filterOption = (e.CommandArgument as Pair).First.ToString();
if (filterOption != "IsNull" && filterOption != "NotIsNull")
if (vals.Length > 3)
filterPatternAssist = vals[4] + "/" + vals[3] + "/" + vals[5];
filterPattern = vals[1] + "/" + vals[0] + "/" + vals[2];
GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("OrderDate");
switch (filterOption)
case "EqualTo":
filterPattern = "[OrderDate] = '" + filterPattern + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
break;
case "NotEqualTo":
filterPattern = "Not [OrderDate] = '" + filterPattern + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.NotEqualTo;
break;
case "GreaterThan":
filterPattern = "[OrderDate] > '" + filterPattern + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThan;
break;
case "LessThan":
filterPattern = "[OrderDate] < '" + filterPattern + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.LessThan;
break;
case "GreaterThanOrEqualTo":
filterPattern = "[OrderDate] >= '" + filterPattern + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
break;
case "LessThanOrEqualTo":
filterPattern = "[OrderDate] <= '" + filterPattern + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.LessThanOrEqualTo;
break;
case "Between":
filterPattern = "'" + filterPattern + "' <= [OrderDate] AND [OrderDate] <= '" + filterPatternAssist + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.Between;
break;
case "NotBetween":
filterPattern = "[OrderDate] <= '" + filterPattern + "' OR [OrderDate] >= '" + filterPatternAssist + "'";
dateColumn.CurrentFilterFunction = GridKnownFunction.NotBetween;
break;
case "IsNull":
break;
case "NotIsNull":
break;
foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
if (column.UniqueName != "OrderDate")
column.CurrentFilterFunction = GridKnownFunction.NoFilter;
column.CurrentFilterValue = string.Empty;
Session["filterPattern"] = filterPattern;
dateColumn.CurrentFilterValue = currentPattern;
filterItem.OwnerTableView.Rebind();
//Add more conditional checks for commands here if necessary
else if (e.CommandName != RadGrid.SortCommandName && e.CommandName != RadGrid.PageCommandName)
Session["filterPattern"] = null;
GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("OrderDate");
dateColumn.CurrentFilterFunction = GridKnownFunction.NoFilter;
dateColumn.CurrentFilterValue = string.Empty;
【讨论】:
以上是关于自定义日期 colmn telerik 网格的过滤器选项的主要内容,如果未能解决你的问题,请参考以下文章
telerik radGrid - 在排序/分页/过滤器上保持客户端状态