简体   繁体   English

Kendo ui网格-使用Ajax发布过滤

[英]Kendo ui grid - filter with ajax post

I´m trying to filter my datasource but using a ajax post, and the reason is, i have a condition on main main GET on my server side which does this: 我正在尝试过滤我的数据源,但使用的是ajax发布,原因是,我在服务器端的主main GET上有这样的条件:

WHERE idPai IS NULL OR idPai = 0

Now what i want is to filter the datsource but without that condition on my server side, so first what i have done was: 现在我想要的是过滤datsource,但在服务器端没有该条件,所以首先要做的是:

       var value = $("#inputPastaFiltro").val();

        $.ajax({
        type: 'POST',
        url: "basedados.php",
        data: {dataFilter:value},
        dataType: "json",
        success: function(result) 
        {
           $('#gridBaseDados').data('kendoGrid').dataSource = new kendo.data.DataSource({ data: result.data[0] }).query({
            filter:{
              logic:"or",
              filters:[
                {field:"nome", operator:"contains",value:result.data[0].nome}]  
             }
          });
        },

On the server side, i have create a: 在服务器端,我创建了:

else if($_POST['dataFilter'])

with the same GET i have but without the condition i have wrote above, this way(i think) it searches for everything, but the rest of the logic is not correct has you already realized. 我有相同的GET但没有上面已经写过的条件,以这种方式(我认为)它可以搜索所有内容,但是您已经意识到其余的逻辑是不正确的。 If i do just like this: 如果我这样做:

var value = $("#inputPastaFiltro").val();
dataSource.query({
                filter:{
                  logic:"or",
                  filters:[
                    {field:"nome", operator:"contains",value:value}]  
                 }
              });

it works very well, and it also does that dynamic search of refreshing the grid as soon as you insert the words in the input(which is great!)...but it is based on my MAIN GET which has that condition. 它很好地工作,并且它还执行了动态搜索,只要在输入中插入单词就刷新网格(这太棒了!)...但是它基于具有这种条件的我的MAIN GET。

Can anyone give me some help?, sorry for the bad english. 谁能帮我些忙吗?抱歉英语不好。

Regards. 问候。

Once you create a dataSource with the array result.data[0] , the filter will be performed over that data. 使用数组result.data[0]创建dataSource后,将对该数据执行过滤 So I think you want to execute the query again, when the filter it performed, right? 所以我认为您想在执行过滤器时再次执行查询,对吗? The way you're calling the ajax request isn't nice when applied with kendo. 与kendo一起使用时,您调用ajax请求的方式不太好。 You should do like that : 您应该这样做:

$('#gridBaseDados').kendoGrid({
    ...
    dataSource: {
        transport: {
            read: function(options) {
                $.ajax({
                    ...
                    success: function(result) {
                        options.success(result.data[0]);
                    }
                });
            }
        }
    }
});

Using a function in dataSource.transport.read is the best way. 最好在dataSource.transport.read使用一个函数。 So you have the options argument, which has the success callback of the dataSource. 因此,您具有options参数,该参数具有dataSource的success回调。 This will overwrite the default dataSource's request and then the entire behiviour is preserved. 这将覆盖默认dataSource的请求,然后保留整个行为。 Your filters should work as expected. 您的过滤器应该可以正常工作。 They will call that read function each time it is changed. 他们将在每次更改该read函数时对其进行调用。

Give it a try and tell us the results. 试试看,并告诉我们结果。 I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM