PDA

View Full Version : [CLOSED] ListFilter System.InvalidCastException



cwolcott
Mar 21, 2012, 6:33 PM
I had a list filter that worked in v1.2 just fine, but I am receiving an error in v2.0. I will continue to research, but just wanted to check if there are any known errors.



<Features>
<ext:GridFilters runat="server">
<Filters>
<ext:ListFilter DataIndex="PerCatNm" Options="Dog, Cat, Fish" />
</Filters>
</ext:GridFilters>
</Features>


The filter displays just fine in the Grid column, but once I select one of the items I receive the error:


{serviceResponse:{success:false,message:"System.InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken.
at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T Token) in d:\\development\\Releases\\Json\\Working\\Src\\New tonsoft.Json\\Linq\Extensions.cs:line 261

When I traced through the code the issue seems to be at:

FilterConditions.cs - Line 65 An exception is raised here:

condition.Value= jObject.Value<string>("value");

The jObject is current
{ "type":"list", "value": [ "Dog", "Cat" ], "field":"PersCatNm"}

The Newtonsoft.Json.dll is Assembly Version 4.0.8.0, File Version 4.0.8.14612

Vladimir
Mar 21, 2012, 11:11 PM
Hi,

Fixed in SVN.
Please note that FilterConditions class was refactored.
There is no ValueAsNumeric, ValueAsBoolean and similar properties.
There is one Value generic method (and List property to retireve list of values)

Here is a sample


protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
{
List<object> data = FiltersTestData.Data;


string s = e.Parameters[this.GridFilters1.ParamPrefix];
//or with hardcoding - string s = e.Parameters["gridfilters"];;


//-- start filtering ------------------------------------------------------------
if (!string.IsNullOrEmpty(s))
{
FilterConditions fc = new FilterConditions(s);


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 (e.Sort.Length > 0)
{
data.Sort(delegate(object x, object y)
{
object a;
object b;


int direction = e.Sort[0].Direction == Ext.Net.SortDirection.DESC ? -1 : 1;


a = x.GetType().GetProperty(e.Sort[0].Property).GetValue(x, null);
b = y.GetType().GetProperty(e.Sort[0].Property).GetValue(y, null);
return CaseInsensitiveComparer.Default.Compare(a, b) * direction;
});
}
//-- end sorting ------------------------------------------------------------




//-- start paging ------------------------------------------------------------
var limit = e.Limit;

if ((e.Start + e.Limit) > data.Count)
{
limit = data.Count - e.Start;
}


List<object> rangeData = (e.Start < 0 || limit < 0) ? data : data.GetRange(e.Start, limit);
//-- end paging ------------------------------------------------------------


//The Total can be set in RefreshData event as below
//or (Store1.Proxy.Proxy as PageProxy).Total in anywhere
//Please pay attention that the Total make a sence only during DirectEvent because
//the Store with PageProxy get/refresh data using ajax request


e.Total = data.Count;

this.GridPanel1.GetStore().DataSource = rangeData;
}

cwolcott
Mar 22, 2012, 1:53 AM
Everything works great against Revision 3900. Please mark as closed.