I would like to be able to update the filter value dynamically through a function:
filter: [{
"field": "id_person",
"operator": "eq",
"value": GetIdPerson()
}]
And the function:
function GetIdPerson() {
try{
if (viewModel.get("SelectedMember").id_person > 0) {
return viewModel.get("SelectedMember").id_person;
}
} catch(ex) { }
return 0;
}
But the function is not being called when I call datasource.read().
Is there a better way to accomplish this?
Or if this is the best way, what am I doing wrong?
Thanks!
There is a built-in function for setting (updating) filter in a DataSource, check this. So actually I don't know why you need that function. It would be easier doing:
try{
if (viewModel.get("SelectedMember").id_person > 0) {
datasource.filter({
"field": "id_person",
"operator": "eq",
"value": viewModel.get("SelectedMember").id_person
});
}
} catch(ex) { }
I mean, define/apply a new filter for the original datasource
which condition is the one that you want.
BUT of course nothing prevents you from using a function for getting the actual value of a filter and you can do:
function GetIdPerson() {
try{
if (viewModel.get("SelectedMember").id_person > 0) {
return viewModel.get("SelectedMember").id_person;
}
} catch(ex) { }
return 0;
}
var datasource = new kendo.data.DataSource({
...
schema : {
model : {
fields: {
...
}
}
},
filter: {
"field": "id_person",
"operator": "eq",
"value": GetIdPerson()
}
});
and/or
datasource.filter({
"field": "id_person",
"operator": "eq",
"value": GetIdPerson()
});
An example here: http://jsfiddle.net/OnaBai/9gnsj/
If you want to filter based on more than filed value you can use 'and' operator other wise use 'or' operator.
Now i am creating one dynamic filter.
var filter = { logic: "and", filters: [] };
for(var index=0; index < totalPages; index++){
filter.filters.push({field: "name", operator: "eq", value: dynamic value here });
}
dataSource.filter(filter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With