Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqGrid: How to invoke 'reloadGrid' to refresh the grid from external filters

Tags:

jqgrid

I have filters outside of jqGrid that should trigger a grid reload. This entry gave me some good insight into how to implement it, using the postData option: How to filter the jqGrid data NOT using the built in search/filter box Unfortunately the code snippets are fragments, and I cannot figure out what the overall sequence of calls should be. Here's a condensed view of my current approach:

<script>
  $(document).ready(function() {
    $("#submit").click(function(e) {
      e.preventDefault();
      myGrid.trigger('reloadGrid');
    }); 
  });

var url="${servicesUrl}/projects";

var myGrid = $("#projectList").jqGrid({
    url: url,
    datatype: 'json',
    mtype: 'GET',  
    // ...
});
</script>

How should I structure the code so that every click of the Submit button will trigger a grid reload? Once I have that sorted out, I'm sure I'll be able to add the posData part, my problem is mostly with the overall sequence of calls. I'm not sure which calls should be inside of the ready() function, and how to call 'reloadGrid' properly. Any help greatly appreciated.

like image 898
Pablo Avatar asked Jun 26 '13 14:06

Pablo


1 Answers

This is what has worked for me: I set a callback on the beforeRequest event which updates the postData property before each request is made.

Note that you will want to put all your jqGrid init code inside the $(document).ready(function(){}); function, otherwise the your table element may not be in the DOM yet

var url="${servicesUrl}/projects";

$(document).ready(function() {
    var $table = $("#projectList");

    $table.jqGrid({
        url: url,
        datatype: 'json',
        mtype: 'GET',  

        beforeRequest: function() {
            var postData = $table.getGridParam('postData');
            //add parameters to postData here
        }
        // ...
    });

    $("#submit").click(function(e) {
        e.preventDefault();
        $table.trigger('reloadGrid');
    }); 
});
like image 128
cfs Avatar answered Oct 24 '22 10:10

cfs