jqueryjqgridstruts2-jquery-gridstruts2-jquery-plugin

jqGrid Refresh From Server Issue


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?


Solution

  • 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".