0

I have a Struts2 jqGrid page that loads fine. When the navigator refresh button is clicked I want it to reload the grid from the server.

    <sjg:grid
        altRows="false"
        id="gridtable"
        dataType="json"
        editurl="%{editurl}"
        filter="true"
        filterOptions="{stringResult:true}"
        gridModel="gridModel"
        gridview="true"
        height="600"
        href="%{remoteurl}"         
        loadonce="true"
        navigator="true"
        navigatorAdd="%{editPermission}"
        navigatorAddOptions="{
            closeAfterAdd:false,
            closeOnEscape:true,
            reloadAfterSubmit:true,
            addCaption:'Add Record'}" 
        navigatorDelete="false"
        navigatorEdit="%{editPermission}"
        navigatorEditOptions="{
            closeAfterEdit:false,
            closeOnEscape:true,
            afterSubmit:function(response, postdata) {
                        return isError(response.responseText);
                     }
        }"
        navigatorRefresh="true"
        navigatorSearch="false"
        onCompleteTopics="loadComplete"
        onSelectRowTopics="rowselect"
        onEditInlineBeforeTopics="beforeFormLoad"
        pager="true"
        pagerButtons="true"
        rowList="25,50,100"
        rowNum="25"
        rownumbers="true"
    >

I have the following bind to set the datatype to json when the refresh button is clicked.

$("#refresh_gridtable").bind("click", function(){
    $("#gridtable").jqGrid("setGridParam", {datatype: 'json'});
    return [true];
});

However, when I click the refresh button it only reloads the data from the server on every other click. If I perform a client-side sort on a specific column then click refresh it will not reload from the server the first click, the second click will refresh from the server without the sort (the sort icons are still visible at this point at the top of the column), then the third click will not hit the server but will apply the sort.

How do I get the refresh to reload from the server each time, then apply any existing sort/filter fields?

user3420328
  • 53
  • 1
  • 9

1 Answers1

1

The solution depends a little from the underlying jqGrid which you use. If you use free jqGrid 4.9 then you can just use additional option of navGrid: reloadGridOptions: { fromServer: true }.

If you use old version of jqGrid then you can use callback beforeRefresh to reset datatype to "json" directly before reloading. I don't use Struts2 myself. So I don't know where you can specify the callback beforeRefresh

Alternatively you can use navigatorRefresh="false" to remove the standard Refresh button from the navigator bar and to use navigatorExtraButtons which seems to be replacement for navButtonAdd. If I correctly understand the option you can specify free code of onclick, which do all what you need. You can use icon: "ui-icon-refresh" to make the custom button looks exactly like the standarda Refresh button. Inside of onclick you can set datatype: 'json' and trigger "reloadGrid".

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • The Struts2 plugin uses jqGrid 4.6.0 so I can't use that new option. But your suggestion for the older version worked perfectly. Thanks again. Is there a way to apply the existing local filter/sort after the call to .trigger("reloadGrid")? – user3420328 Jun 19 '15 at 15:35
  • @user3420328: I'm not sure how you can do this in Struts2, but what you need in general to do is saving `postData.filters` **before** reloading, set `datatype: 'json'`, trigger reloadGrid, then you need include `loadComplete` with the small code `var $self = $(this), p = $self.jqGrid("getGridParam"); if (p.datatype === "json") { setTimeout(function () { p.postData.filters = previouslySavedFilter; p.search = true; $self.trigger("reloadGrid"); }, 50);}` The code from `setTimeout` will be executed *after* jqGrid already have changed `datatype` to `"local"` and the reloading with filter be local. – Oleg Jun 19 '15 at 17:03
  • I can't seem to be able to get this code to work. I'm not familiar with javascript so I don't know how to save the postData.Filters? Can you give me an example of that process? – user3420328 Jun 22 '15 at 13:07
  • @user3420328: Sorry, but I have no examples which do exactly what you need. "Saving" is nothing more as assignment the value to variable: `previouslySavedFilter = p.postData.filters;`. If you have no problem with the usage of parameters of the grid then you can `p.previouslySavedFilter = p.postData.filters;`. I mean that `previouslySavedFilter` were you save the data and restore later can be just new parameter of jqGrid. If you would have problems in the implementation you should open new question and post *your current code*, which works not like expected, and I will help you to fix it. – Oleg Jun 22 '15 at 13:24
  • Thanks, I created the following question regarding to saving the filter postData. http://stackoverflow.com/questions/30984263/jqgrid-reload-filter-values-after-triggerreloadgrid – user3420328 Jun 22 '15 at 18:29