Change filter value dynamically for a kendo datasource?

datasource, filter, grid, jquery, kendo-ui

Solution

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/

Problem

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!

Original source