jqgrid搜索/过滤器 [英] jqgrid search/filter

查看:185
本文介绍了jqgrid搜索/过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



 过滤器= [{ '名称': 'main_account_group_code', '比较': 'STARTS_WITH', '值':$( '#searchCode')VAL()}]; 

现在,我的网格在搜索中成功显示数据。我的问题是,是否有另一种方法来做到这一点,而不使用全局变量?我的老人告诉我,这是一个不好的做法,并尝试别的。我也有这样的感觉,在jqgrid中显示我的搜索数据还有一个最好的方法(而不是使用全局),但是我不知道如何去做。



我一直在解决这个问题好几天了,但是现在还没有最好的方法来解决这个问题。任何人的帮助..

编辑:
这是我的jqgrid代码。


datatype:'local',
jsonReader:{
$ b root:function(obj){
var root = [];
$ b $ if('error'in obj)
{
showMessage(obj.error ['class'] +'error:'+ obj ['error'] ['msg']);
}
else
{
$ .each(obj [''结果'] ['main'] ['rowdata'],函数(rowIndex,rowDataValue){
var row = {};
$ .each(rowDataValue,function(columnIndex,rowArrayValue){
var fldName = obj ['result'] ['main'] ['metadata'] ['fields'] [columnIndex] .name;
row [fldName] = rowArrayValue;
});
根[rowIndex] =行;

});
};

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:{
过滤器:[{name:main_account_group_code,比较:starts_with,value:$('#searchCode').val()}]
},
rowNum:10 ,
viewrecords:true,
rowList:[10,50,100],
pager:'#tblDataPager1',
sortname:'desc',
sortorder:'desc ',
loadonce:false,
height:250,
标题:主账户
});


解决方案

不喜欢不是因为使用全局变量 filter ,而是因为复杂的逻辑。特别奇怪的是,我发现你放弃了 serializeGridData 事件句柄的实现中的许多jqGrid参数。我相信代码可以简化,但是您不会发布更多的完整JavaScript代码。即使使用哪种HTTP方法与服务器通信(您是否使用 mtype:POST参数)。



现在关于你的主要问题。变量 filter 应该是不是是全局变量。如果应该是可见的。例如:

$ p $ $(document).ready(function(){
var filter =''; //这不是全局变量
$('#btnsearchCode')。click(function(){
filter =... any value ...; //你可以在这里改变过滤器的值
//
$('#list1')。trigger('reloadGrid');
});

$('#list1') .jqGrid({
//定义jqGrid在哪里你可以使用过滤器,如果需要
//直接在任何函数的内部
});
});


this is related to my latest question in this link. i already figured out the whats the error why it displays "Server Error: Parameter 'dataType' is not specified". It's the filter="[{'name':'main_account_group_code','comparison':'starts_with','value':$('#searchCode').val()}]";

to

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

now, my grid successfully displays the data in searching. My question is, is there another way to do this without using a global variable? My senior told me that its a bad practice and to try something else. I also have this feeling that there is still one best way in displaying my search data in the jqgrid (rather than using a global), but i just dont know how to do it.

I've been solving this problem on my own for days now, but still i still dont have the best method on how to do this (still stuck). anybody help..

EDIT: This is my jqgrid code.

$("#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"
});  

解决方案

The way which you use in your previous question I don't like not because of the usage of "global" variable filter but because of complicated logic. Especially strange I find that you discard many parameters of jqGrid inside of your implementation of the serializeGridData event handle. I am sure that the code can be much simplified, but you don't posted more full JavaScript code which you use. Even which HTTP method you use to communicate with the server (do you use mtype:"POST" parameter or not).

Now about you main question. The variable filter should not be global. If should be visible. For example:

$(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
    });
});

这篇关于jqgrid搜索/过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆