OK, I'm getting the filters saved in session, but now I'm having trouble applying them after reload. The first time into Page_Load when !IsAjaxRequest I pull my session vars and run through filter/sort routine. But then the OnReadData routine fires and the StoreReadDataEventArgs are empty which I thought should contain the filters and sort I previously applied, so final loaded grid has no filters or sort. Also is there function like 'App.GridFilters1.getFilterData()' to get the current sort order? In code behind tried to get the Store1.Sorters collection, but it's only initial sort which in this example is not set, so for now I hard-coded in button click. Here's my current code, let me know what you think.
<%@ Page Language="C#"%>
<%@ Register Assembly="Ext.NET" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
this.Store1.DataSource = this.dynData;
if (Session["gridFilters"] != null && Session["gridSort"] != null)
{
ApplyFilterSort((string)Session["gridFilters"], (DataSorter[])Session["gridSort"]);
}
}
}
private List<object> dynData
{
get
{
List<object> comps = new List<object> { };
for (int i = 0; i < 100; i++)
{
comps.Add(new {company = "Company " + i, price = 71.72 + i, change = 0.02, pctChange = 0.03, lastCahnge = new DateTime(2013, 9, 1) });
}
return comps;
}
}
private void ApplyFilterSort(string filterParms, DataSorter[] gridSort)
{
List<object> data = this.dynData;
//-- start filtering ------------------------------------------------------------
if (!string.IsNullOrEmpty(filterParms))
{
FilterConditions fc = new FilterConditions(filterParms);
foreach (FilterCondition condition in fc.Conditions)
{
Comparison comparison = condition.Comparison;
string field = condition.Field;
FilterType type = condition.Type;
object value;
switch (condition.Type)
{
case FilterType.Boolean:
value = condition.Value<bool>();
break;
case FilterType.Date:
value = condition.Value<DateTime>();
break;
case FilterType.List:
value = condition.List;
break;
case FilterType.Numeric:
if (data.Count > 0 && data[0].GetType().GetProperty(field).PropertyType == typeof(int))
{
value = condition.Value<int>();
}
else
{
value = condition.Value<double>();
}
break;
case FilterType.String:
value = condition.Value<string>();
break;
default:
throw new ArgumentOutOfRangeException();
}
data.RemoveAll(
item =>
{
object oValue = item.GetType().GetProperty(field).GetValue(item, null);
IComparable cItem = oValue as IComparable;
switch (comparison)
{
case Comparison.Eq:
switch (type)
{
case FilterType.List:
return !(value as List<string>).Contains(oValue.ToString());
case FilterType.String:
return !oValue.ToString().StartsWith(value.ToString());
default:
return !cItem.Equals(value);
}
case Comparison.Gt:
return cItem.CompareTo(value) < 1;
case Comparison.Lt:
return cItem.CompareTo(value) > -1;
default:
throw new ArgumentOutOfRangeException();
}
}
);
}
}
//-- end filtering ------------------------------------------------------------
//-- start sorting ------------------------------------------------------------
if (gridSort.Length > 0)
{
data.Sort(delegate(object x, object y)
{
object a;
object b;
int direction = gridSort[0].Direction == Ext.Net.SortDirection.DESC ? -1 : 1;
a = x.GetType().GetProperty(gridSort[0].Property).GetValue(x, null);
b = y.GetType().GetProperty(gridSort[0].Property).GetValue(y, null);
return CaseInsensitiveComparer.Default.Compare(a, b) * direction;
});
}
//-- end sorting ------------------------------------------------------------
this.GridPanel1.GetStore().DataSource = data;
}
protected void Store1_RefreshData(object sender, StoreReadDataEventArgs e)
{
ApplyFilterSort(e.Parameters[this.GridFilters1.ParamPrefix], e.Sort);
}
protected void btn1_Click(object sender, DirectEventArgs e)
{
// Save Filters
string myParm = e.ExtraParams["filters"];
Session.Add("gridFilters", myParm);
DataSorter mySort = new DataSorter();
mySort.Property = "price";
mySort.Direction = Ext.Net.SortDirection.DESC;
DataSorter[] gridSort = new DataSorter[1];
gridSort[0] = mySort;
Session.Add("gridSort", gridSort);
// Redirect
Response.Redirect("SaveFilters.aspx");
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<ext:XScript ID="XScript1" runat="server">
<script type="text/javascript">
var template = '<span style="color:{0};">{1}</span>';
var change = function (value) {
return Ext.String.format(template, (value > 0) ? "green" : "red", value);
};
var pctChange = function (value) {
return Ext.String.format(template, (value > 0) ? "green" : "red", value + "%");
};
</script>
</ext:XScript>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ExtResourceManager" runat="server" DisableViewState="false" RenderStyles="Embedded"></ext:ResourceManager>
<ext:Button ID="btn1" runat="server" Text="Reload">
<DirectEvents>
<Click OnEvent="btn1_Click">
<EventMask ShowMask="true"></EventMask>
<ExtraParams>
<ext:Parameter Name="filters" Value="App.GridFilters1.getFilterData()" Mode="Raw" />
</ExtraParams>
</Click>
</DirectEvents>
</ext:Button>
<ext:GridPanel ID="GridPanel1" runat="server" Title="Sample Grid" Width="650" Height="350" EnableColumnMove="false" EmptyText="No rows matching filter criteria.">
<Store>
<ext:Store ID="Store1" runat="server" RemoteSort="true" RemoteFilter="true" OnReadData="Store1_RefreshData">
<Proxy>
<ext:PageProxy/>
</Proxy>
<Model>
<ext:Model ID="Model1" runat="server">
<Fields>
<ext:ModelField Name="company" />
<ext:ModelField Name="price" Type="Float" />
<ext:ModelField Name="change" Type="Float" />
<ext:ModelField Name="pctChange" Type="Float" />
<ext:ModelField Name="lastChange" Type="Date" DateFormat="M/d hh:mmtt" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel OverflowY="Auto">
<Columns>
<ext:RowNumbererColumn Width="50"></ext:RowNumbererColumn>
<ext:Column ID="Column1" runat="server" Text="Company" DataIndex="company" Flex="1" />
<ext:Column ID="Column2" runat="server" Text="Price" DataIndex="price">
<Renderer Format="UsMoney" />
</ext:Column>
<ext:Column ID="Column3" runat="server" Text="Change" DataIndex="change">
<Renderer Fn="change" />
</ext:Column>
<ext:Column ID="Column4" runat="server" Text="Change" DataIndex="pctChange">
<Renderer Fn="pctChange" />
</ext:Column>
<ext:DateColumn ID="DateColumn1" runat="server" Text="Last Updated" DataIndex="lastChange" />
</Columns>
</ColumnModel>
<SelectionModel>
<ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" Mode="Single" AllowDeselect="true" ShowHeaderCheckbox="false"></ext:CheckboxSelectionModel>
</SelectionModel>
<Features>
<ext:GridFilters ID="GridFilters1" runat="server" Local="false" Enabled="true">
<Filters>
<ext:StringFilter DataIndex="company"></ext:StringFilter>
<ext:NumericFilter DataIndex="price"></ext:NumericFilter>
</Filters>
</ext:GridFilters>
</Features>
</ext:GridPanel>
</form>
</body>
</html>