jQuery 为剑道数据源动态更改过滤器值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15488095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:11:05  来源:igfitidea点击:

Change filter value dynamically for a kendo datasource?

jqueryfiltergridkendo-uidatasource

提问by Naner

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().

但是当我调用 datasource.read() 时没有调用该函数。

Is there a better way to accomplish this?

有没有更好的方法来实现这一点?

Or if this is the best way, what am I doing wrong?

或者,如果这是最好的方法,我做错了什么?

Thanks!

谢谢!

回答by OnaBai

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:

有一个用于在 DataSource 中设置(更新)过滤器的内置函数,检查这个。所以实际上我不知道你为什么需要这个功能。这样做会更容易:

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 datasourcewhich condition is the one that you want.

我的意思是,为原始datasource条件定义/应用一个新过滤器,哪个条件是您想要的条件。

BUTof 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/

这里的一个例子:http: //jsfiddle.net/OnaBai/9gnsj/

回答by Ranga Reddy

If you want to filter based on more than filed value you can use 'and' operator other wise use 'or' operator.

如果您想根据多于提交的值进行过滤,您可以使用“and”运算符,否则请使用“or”运算符。

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);

回答by Ranga Reddy

This worked for me. Not documented anywhere, but saw the _filter and _sort objects in the gridOptions.dataSource object, and hey presto it worked.

这对我有用。没有记录在任何地方,但在 gridOptions.dataSource 对象中看到了 _filter 和 _sort 对象,嘿,它很快就起作用了。

        $http.get(templateUrl).success(function(result) {
            gridOptions.columns = result.columns;
            if (result.filter) {
                gridOptions.dataSource._filter = result.filter;
                gridOptions.dataSource._sort = result.sort;
            };