jqgrid搜索/过滤器

时间:2011-04-28 05:43:19

标签: javascript search jqgrid filter

这与我在link中的最新问题有关。我已经弄清楚了为什么它显示“服务器错误:没有指定参数'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"
});  

1 个答案:

答案 0 :(得分:1)

您在上一个问题中使用的方式我不喜欢,因为使用了“全局”变量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
    });
});
相关问题