jqgrid搜索/过滤

jqgrid search/filter

本文关键字:过滤 搜索 jqgrid      更新时间:2023-09-26

这与我在这个链接中最近的问题有关。我已经弄清楚了错误是什么,为什么它显示"服务器错误:参数'dataType'未指定"。是filter="[{'name':'main_account_group_code','comparison':'starts_with','value':$('#searchCode').val()}]";

filter=[{'name':'main_account_group_code','comparison':'starts_with','value':$('#searchCode').val()}];

现在,我的网格成功地显示了搜索中的数据。我的问题是,有没有另一种方法来做到这一点,而不使用全局变量?我的前辈告诉我,这是一个不好的做法,并尝试其他的东西。我也有这种感觉,仍然有一个最好的方式来显示我的搜索数据在jqgrid(而不是使用全局),但我只是不知道如何做到这一点。

我已经自己解决这个问题好几天了,但是我仍然没有最好的方法来做这个(仍然卡住)。有人帮助. .

编辑:这是我的jqgrid代码。

$("#list1").jqGrid({
    url: '',    
    datatype: 'local',      
    jsonReader : {              
        root: function(obj) {
            var root = [];
            if  ('error' in obj) 
            {
                showMessage(obj.error['class'] + ' error: ' + obj['error']['msg']);
            }
            else
            {
                $.each(obj['result']['main']['rowdata'], function(rowIndex, rowDataValue) {
                    var row = {};
                    $.each(rowDataValue, function(columnIndex, rowArrayValue) {
                      var fldName = obj['result']['main']['metadata']['fields'][columnIndex].name;    
                       row[fldName] = rowArrayValue;                  
                    });
                    root[rowIndex] = row;
                });
            };
            return root;
        },          
        page: "result.main.page",   
        total: "result.main.pageCount",     
        records: "result.main.rows",            
        repeatitems: false,                             
        id: "0"                                                     
    },
    colNames:['Code', 'Description','Type'],        
    colModel:[
        {name:'code'},
        {name:'desc'},
        {name:'type'}
    ],
    postData: {
      filters:[{"name":"main_account_group_code", "comparison":"starts_with", "value":$('#searchCode').val()}]
    },
    rowNum:10,                      
    viewrecords: true,
    rowList:[10,50,100],    
    pager: '#tblDataPager1',
    sortname: 'desc',   
    sortorder: 'desc',      
    loadonce:false, 
    height: 250,
caption: "Main Account"
});  

你在上一个问题中使用的方式我不喜欢,不是因为使用"全局"变量filter,而是因为复杂的逻辑。特别奇怪的是,我发现你在serializeGridData事件句柄的实现中丢弃了jqGrid的许多参数。我相信,代码可以大大简化,但你没有张贴更完整的JavaScript代码,你使用。甚至您使用哪种HTTP方法与服务器通信(是否使用mtype:"POST"参数)。

现在谈谈你的主要问题。变量filter应该而不是是全局的。它应该是可见的。例如:

$(document).ready(function () {
    var filter = ''; //this is NOT global variable
    $('#btnsearchCode').click(function(){
        filter="...any value..."; // you can change the value of filter here
        //...
        $('#list1').trigger('reloadGrid');
    });
    $('#list1').jqGrid({
        // define jqGrid where you can use filter if needed
        // either directly of inside of body of any function
    });
});