function actionBegin (args) {
if (args.requestType == "filterchoicerequest" || args.requestType === 'filtersearchbegin') {
// you can set filter choice count based on your datasource count, by default count 1000
args['filterChoiceCount'] = 30000;
}
} |
<script>
function actionBegin(args) {
if (args.requestType == "filterchoicerequest" || args.requestType === 'filtersearchbegin') {
// you can set your custom query here
var predicate = new ej.data.Predicate("EmployeeID", "greaterthan", 1);
predicate = predicate.and("EmployeeID", "lessthan", 4);
args.query.where(predicate);
}
}
</script> |
DataSource = order.Select(p => new {p.OrderID}).Distinct().ToList(); |
Index.html
<script type="text/javascript">
function actionBegin(args)
{
if (args.requestType === "filterchoicerequest")
{
var filterfields = [];
var objFilter = Object.keys(args.filterModel.existingPredicate);
for (var i = 0; i < objFilter.length; i++) {
filterfields.push(objFilter[i]);
}
filterfields.push(args.filterModel.options.field);
args.query.distincts = [];
args.query.select(filterfields); // Created the select query
}
}
</script>
Controller:
public ActionResult UrlDatasource(DataManagerRequest dm)
{
IEnumerable DataSource = order;
var count = 0;
DataOperations operation = new DataOperations();
List<string> str = new List<string>();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
count = DataSource.Cast<Orders>().Count();
if (dm.Select != null)
{
DataSource = operation.PerformSelect(DataSource, dm.Select); // Selected the columns value based on the filter request
DataSource = DataSource.Cast<dynamic>().Distinct().AsEnumerable(); // Get the distinct values from the selected column
}
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count }) : Json(DataSource);
} |
Hi Thavasianand,I know that is what is causing the problem, unfortunately returning all the columns doesn't resolve it for us, because it defeats the whole purpose of doing server-side (indeed database-side) filtering. If I return all columns I have to return all DISTINCT rows for all columns, which therefore means returning the entire dataset - maybe tens of thousands of rows, just to get maybe 4-5 strings for a filter list!With the server-side filtering that we have built it is beautifully efficient when retrieving the actual page of data - if I need only 10 rows based on the filters applied and/or the page size then only 10 rows are returned from the database. That's because the dm.Where object is populated, and so we can use it to generate the necessary WHERE clause in SQL.However, if - when clicking on a filter symbol just to change the filter settings - I need to send from database to application server and from application server to browser the entire contents of the database table, that is incredibly inefficient and makes all the server-side filtering totally pointless.This could be very easily solved if the grid control simply populates the dm.Where object for a FilterChoiceRequest in exactly the same way as it does when retrieving a page of data. Is there any way to do that?BTW we have already implemented a partial fix for this ourselves in the ActionBegin handler as follows:function actionBegin(args){switch (args.requestType){......case "filterchoicerequest":var field = args.filterModel.options.field;args.query.addParams('filterField', field);var predicate = null;$.each(args.filterModel.filterSettings.properties.columns,function(index, item){if (item.properties.operator === "equal" && item.properties.field != field){predicate = predicate === null ? new ej.data.Predicate(item.properties.field, item.properties.operator, item.properties.value) : predicate.or(item.properties.field, item.properties.operator, item.properties.value);}});if(predicate !== null) args.query.where(predicate);break;......}}This works with the most basic filters (ticked lists). However this will not work with complex filter queries. It really should not be necessary for us to write client-side code to replicate what the grid control natively does anyway when retrieving pages of data - but if that is the only way please could you refine the code sample above to cope with the use of more complex / compound filters?Many thanks,Charles
Hi Thavasianand,I know that is what is causing the problem, unfortunately returning all the columns doesn't resolve it for us, because it defeats the whole purpose of doing server-side (indeed database-side) filtering. If I return all columns I have to return all DISTINCT rows for all columns, which therefore means returning the entire dataset - maybe tens of thousands of rows, just to get maybe 4-5 strings for a filter list!With the server-side filtering that we have built it is beautifully efficient when retrieving the actual page of data - if I need only 10 rows based on the filters applied and/or the page size then only 10 rows are returned from the database. That's because the dm.Where object is populated, and so we can use it to generate the necessary WHERE clause in SQL.However, if - when clicking on a filter symbol just to change the filter settings - I need to send from database to application server and from application server to browser the entire contents of the database table, that is incredibly inefficient and makes all the server-side filtering totally pointless.This could be very easily solved if the grid control simply populates the dm.Where object for a FilterChoiceRequest in exactly the same way as it does when retrieving a page of data. Is there any way to do that?BTW we have already implemented a partial fix for this ourselves in the ActionBegin handler as follows:function actionBegin(args){switch (args.requestType){......case "filterchoicerequest":var field = args.filterModel.options.field;args.query.addParams('filterField', field);var predicate = null;$.each(args.filterModel.filterSettings.properties.columns,function(index, item){if (item.properties.operator === "equal" && item.properties.field != field){predicate = predicate === null ? new ej.data.Predicate(item.properties.field, item.properties.operator, item.properties.value) : predicate.or(item.properties.field, item.properties.operator, item.properties.value);}});if(predicate !== null) args.query.where(predicate);break;......}}This works with the most basic filters (ticked lists). However this will not work with complex filter queries. It really should not be necessary for us to write client-side code to replicate what the grid control natively does anyway when retrieving pages of data - but if that is the only way please could you refine the code sample above to cope with the use of more complex / compound filters?Many thanks,Charles
Hello,i have a similar problem with server side filtering. I narrowed the problem down to a single line of code. I commented a file with my onActionBegin method.I have spent hours trying to get around the problem, including adding the existing predicates to where myself. It did not work, always a json-exception.Any hint much appreciated.Regards,Sascha Herrmann
Attachment: onActionBegin_edfa70bc.7z
<script type="text/javascript">
function actionBegin(args) {
if (args.requestType === "filterchoicerequest") {
var filterfields = [];
var objFilter = Object.keys(args.filterModel.existingPredicate);
for (var i = 0; i < objFilter.length; i++) {
filterfields.push(objFilter[i]);
}
if (filterfields.indexOf(args.filterModel.options.field) == -1)
filterfields.push(args.filterModel.options.field);
args.query.distincts = [];
args.query.select(filterfields); // Created the select query
if (this.filterSettings.columns.length) {
var pred = new ej.data.Predicate(this.filterSettings.columns[0].properties.field, this.filterSettings.columns[0].properties.operator, this.filterSettings.columns[0].properties.value);
for (var i = 1; i < this.filterSettings.columns.length; i++) {
pred = pred[this.filterSettings.columns[i].properties.predicate](this.filterSettings.columns[i].properties.field, this.filterSettings.columns[i].properties.operator, this.filterSettings.columns[i].properties.value);
}
args.query.where(pred);
}
}
}
</script> |