Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
170
IGNITE UI GRID REMOTE FILTERING & PAGING OPERATION
posted

Hi Friends,

As we have verified in the below URL for the remote paging and filtering operation, we have found that for the string and integer column filtering the column name shall be hard coded in the "private IQueryable ApplyFilterExpr(NameValueCollection queryString, IQueryable data) " function for the equals and doesnotequalsto filtering condition.

https://www.igniteui.com/help/handling-remote-features-manually#filtering

Since we are using more number of grid pages and we could not able to hard code all the column names to apply the filter. is there any possibilities to get the column data type so that we will use the same in the above function to apply the filtering.

The code available in the above URL, has been copy below.

private IQueryable ApplyFilterExpr(NameValueCollection queryString, IQueryable data)
{
List exprs = GetFilterExpressions(queryString);
StringBuilder builder = new StringBuilder();
int count = 0;

for (int i = 0; i < exprs.Count; i++)
{
if (count != 0 && count <= exprs="" count="" -="" 1="" br=""> {
builder.Append(exprs[i].Logic.ToLower() == "AND".ToLower() ? " AND " : " OR ");
}
count++;

string condition = exprs[i].Condition;
string expr = exprs[i].Expr;
string colKey = exprs[i].Key;
var dt = DateTime.Now;

switch (condition.ToLower())
{
case "startswith":
builder.Append(colKey + ".StartsWith(\"" + expr + "\")");
break;
case "contains":
builder.Append(colKey + ".Contains(\"" + expr + "\")");
break;
case "endswith":
builder.Append(colKey + ".EndsWith(\"" + expr + "\")");
break;


case "equals":
if (colKey == "ShipName") {
//col type is string
builder.Append(colKey + " == \"" + expr + "\"");
}
else
{
//col type is number
builder.Append(colKey + " == " + expr);
}

break;
case "doesnotequal":
if (colKey == "ShipName")
{
//col type is string
builder.Append(colKey + " != \"" + expr + "\"");
}
else
{
//col type is number
builder.Append(colKey + " != " + expr);
}
break;

case "doesnotcontain":

builder.Append("! " + colKey + ".Contains(\"" + expr + "\")");
break;
case "lessthan":
builder.Append(colKey + " < " + expr);
break;
case "greaterthan":
builder.Append(colKey + " > " + expr);
break;
case "lessthanorequalto":
builder.Append(colKey + " <= " + expr);
break;
case "greaterthanorequalto":
builder.Append(colKey + " >= " + expr);
break;
case "on":
dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(double.Parse(expr)).ToUniversalTime();
builder.Append("(" + colKey + ".Value.Day == " + dt.Day + " AND " + colKey +
".Value.Year == " + dt.Year + " AND " +colKey + ".Value.Month == " + dt.Month + ")");
break;
case "noton":
dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(double.Parse(expr)).ToUniversalTime();
builder.Append("!("+colKey + ".Value.Day == " + dt.Day + " AND " + colKey +
".Value.Year == " + dt.Year + " AND " + colKey + ".Value.Month == " + dt.Month + ")");
break;
case "after":
dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(double.Parse(expr)).ToUniversalTime();
builder.Append("((" + colKey + ".Value.Year > " + dt.Year + " OR (" +
colKey + ".Value.Month > " + dt.Month + " AND " + colKey + ".Value.Year == " + dt.Year + ") OR (" +
colKey + ".Value.Day > " + dt.Day + " AND " + colKey + ".Value.Year == " + dt.Year + " AND " +
colKey + ".Value.Month == " + dt.Month + ")))");
break;
case "before":
dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(double.Parse(expr)).ToUniversalTime();
builder.Append("((" + colKey + ".Value.Year < " + dt.Year + " OR (" +
colKey + ".Value.Month < " + dt.Month + " AND " + colKey + ".Value.Year == " + dt.Year + ") OR (" +
colKey + ".Value.Day < " + dt.Day + " AND " + colKey + ".Value.Year == " + dt.Year + " AND " +
colKey + ".Value.Month == " + dt.Month + ")))");
break;
case "today":
builder.Append("(" + colKey + ".Value.Day == " + DateTime.Now.Day + " AND " + colKey +
".Value.Year == " + DateTime.Now.Year + " AND " + colKey + ".Value.Month == " + DateTime.Now.Month + ")");
break;
case "yesterday":
DateTime yesterday = DateTime.Now.AddDays(-1);
builder.Append("(" + colKey + ".Value.Day == " + yesterday.Day + " AND " + colKey +
".Value.Year == " + yesterday.Year + " AND " + colKey + ".Value.Month == " + yesterday.Month + ")");
break;
case "thismonth":
builder.Append("(" + colKey + ".Value.Year == " + DateTime.Now.Year + " AND " + colKey + ".Value.Month == " + DateTime.Now.Month + ")");
break;
case "lastmonth":
builder.Append("(" + colKey + ".Value.Year == " + (DateTime.Now.Year - 1) + " AND " + colKey + ".Value.Month == " + (DateTime.Now.Month - 1) + ")");
break;
case "nextmonth":
builder.Append("(" + colKey + ".Value.Year == " + (DateTime.Now.Year - 1) + " AND " + colKey + ".Value.Month == " + (DateTime.Now.Month + 1) + ")");
break;
case "thisyear":
builder.Append(colKey + ".Value.Year == " + DateTime.Now.Year);
break;
case "lastyear":
builder.Append(colKey + ".Value.Year == " + (DateTime.Now.Year - 1));
break;
case "nextyear":
builder.Append(colKey + ".Value.Year == " + (DateTime.Now.Year + 1));
break;
default:
break;
}
}
if (builder.Length > 0) {
data = data.Where(builder.ToString(), new object[0]);
}

return data;
}