0

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"
});  
Community
  • 1
  • 1
jayAnn
  • 827
  • 3
  • 18
  • 38

1 Answers1

1

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
    });
});
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Oleg, i already edit my post in my previous question. And if i dont put a filter in my serializedGridData, my search data wont show in the grid., instead, it shows all the data. I used 'type: 'GET''. Please take a look again at my last question. Thanks for the time. – jayAnn Apr 29 '11 at 02:47
  • and also Oleg, I dont know what you mean when you said that I discard many parameters of jqGrid inside of your implementation of the serializeGridData event handle? Those parameters that I posted in my previous question are the one I used since i started using jqgrid in my program. – jayAnn Apr 29 '11 at 05:16
  • @jayAnn: In your implementation of `serializeGridData` you use only `rows`, `page` and `sidx`. All other property of `postData` parameter of the function you ignore (discard). For example you ignore the `filters` parameter. So having such implementation of `serializeGridData` you can not extend the `postData` parameter of jqGrid. Look at my another recent answer `http://stackoverflow.com/questions/5810885/postdata-not-passing-any-parameters/5811359#5811359` which can give you some advises for better implementation. – Oleg Apr 29 '11 at 07:16
  • Oleg, if I can not extend the postData parameter of jqGrid, what shall i do? i know i should have filters in my 'serializeGridData' but i just dont know how to pass the filters from my function... thanks for replying. – jayAnn Apr 29 '11 at 08:29
  • BTW, Oleg, I already put my jqgrid definition of $('#list1'). please see my Edit. thanks – jayAnn Apr 29 '11 at 08:33
  • @jayAnn: I wrote my answer [here](http://stackoverflow.com/questions/5775022/how-to-display-the-searched-data-on-the-jgrid/5831045#5831045). – Oleg Apr 29 '11 at 11:33