Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

jquery - Reload of jqgrid not happening with loadonce:true

I am newbie in using jqgrid.

While new page load the grid is properly loading the data from database. Afterwards due to loadonce: true the grid is not reloading the data from database for add/edit/delete.

My application combination is JSP, Servlet, MySQL

I have a grid with following settings

jQuery("#userList").jqGrid({
                    url: '<%=request.getContextPath()%>/userJqGrid?q=1&action=fetchData&userCookie=<%= encodedCookie%>',
                    datatype: 'xml',
                    mtype: 'GET',
                    colNames:['<%= userProp.getProperty(userColNames[0])%>',
                              '<%= userProp.getProperty(userColNames[1])%>',
                              '<%= userProp.getProperty(userColNames[2])%>',
                              '<%= userProp.getProperty(userColNames[3])%>',
                              '<%= userProp.getProperty(userColNames[4])%>',
                              '<%= userProp.getProperty(userColNames[5])%>'
],
                    colModel:[
                        {name:'<%= userColNames[0]%>',index:'<%= userColNames[0]%>',
                            width:120,sortable:true,editable:true,editrules:{required:true},formoptions:{rowpos:1, elmprefix:'*'}},
                        {name:'<%= userColNames[1]%>',index:'<%= userColNames[1]%>',
                            width:130,sortable:true,editable:true},
                        {name:'<%= userColNames[2]%>',index:'<%= userColNames[2]%>',
                            width:100,sortable:true,editable:true,editrules:{required:true},formoptions:{rowpos:3, elmprefix:'*'}},
                        {name:'<%= userColNames[3]%>',index:'<%= userColNames[3]%>',
                            width:180,sortable:true,editable:true,editrules:{email:true,required:true},formoptions:{rowpos:4, elmprefix:'*'}},
                        {name:'<%= userColNames[4]%>',index:'<%= userColNames[4]%>', 
                            width:100,sortable:true,editable:true},
                        {name:'<%= userColNames[5]%>',index:'<%= userColNames[5]%>', 
                            width:100,sortable:true,editable:true},
                    ],
                    pager: '#pager1',
                    rowNum:50,
                    height:'auto',
                    //rowList:[10,20,30],
                    loadonce: true,
                    sortname:'<%= userColNames[0]%>',
                    viewrecords: true,
                    editurl:'<%=request.getContextPath()%>/userJqGrid?q=1&action=addData&userCookie=<%= encodedCookie%>',
                    caption: 'User Grid',
                    ondblClickRow: function(rowid) {
                       $("#userList").jqGrid('editGridRow',rowid, userEditParam);
                        return;
                    }
                 });
$("#userList").jqGrid('navGrid',"#pager1",{add:true,edit:true,del:true});
$("#userList").jqGrid('gridResize', { minWidth: 450, minHeight: 150});

I tried adding following code to reload

$("#userList").jqGrid('setGridParam',{datatype:xml}).trigger('reloadGrid')

Can some one help?

Solution which worked for me

$("#userList").jqGrid('navGrid',"#pager1",{add:true,edit:true,del:true,refresh:true,

beforeRefresh: function() {
   $("#userList").jqGrid('setGridParam',{datatype:xml}).trigger('reloadGrid');
}},
{
  afterSubmit: processAddEdit,
       closeAfterAdd:true,
       closeAfterEdit:true
},{
  aftersubmit:processAddEdit,
       closeAfterAdd:true,
       closeAfterEdit:true
});

function processAddEdit() {
  $("#userList").jqGrid('setGridParam',{datatype:xml}).trigger('reloadGrid');
  return[true,'']; //no error
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Correct is datatype:'xml' and not datatype:xml. So the code like

$("#userList").jqGrid('setGridParam', {datatype:'xml'})
              .trigger('reloadGrid', [{page:1}]);

should work. See here for description of additional parameters of reloadGrid.

UPDATED: From your comments I hope that I know where you have problem with the implementation. If I understand you correct now you want that "Reload Grid" button from the navigator bar reloads the grid from the server. To do so you should redefine the standard "Reload Grid" button with your implementation which do the code which I included in my answer (see above). So you should use refresh: false as the navGrid option to remove the standard "Reload Grid" button and then use navButtonAdd to add new button which looks exactly like the standard button and have your custom implementation or the onClickButton event:

var myGrid = $('#userList');
myGrid.jqGrid({
    datatype: 'xml',
    loadonce: true,
    pager: '#pager1',
    // ... other parameters which you use
});
myGrid.jqGrid('navGrid', '#pager1', {refresh: false});
myGrid.jqGrid(
    'navButtonAdd',
    '#pager1',
    {
        caption: "",
        buttonicon: "ui-icon-refresh",
        title: $.jgrid.nav.refreshtitle,
        onClickButton: function () {
            $(this).jqGrid('setGridParam', {datatype:'xml'});
            $(this).trigger('reloadGrid', [{page:1}]);
        }
    }
);

UPDATED 2: To force reloading of the grid after Add and Edit operations you can use afterSubmit event which will be called after the receiving successful server response, but before reloadGrid made by the jqGrid.

myGrid.jqGrid('navGrid', '#pager1', {refresh: false},
    { // Edit options
        afterSubmit: function () {
            $(this).jqGrid('setGridParam', {datatype:'xml'});
            return [true,'']; // no error
        }
    },
    { // Add options
        afterSubmit: function () {
            $(this).jqGrid('setGridParam', {datatype:'xml'});
            return [true,'',false]; // no error and no new rowid
        }
    },
    { // Delete options
        afterSubmit: function () {
            $(this).jqGrid('setGridParam', {datatype:'xml'});
            return [true,'']; // no error
        }
    }
);

I am not sure that reloading of the grid from the server is really required after Edit and Delete operations in general, but reloading after the Add operation could be needed if the server not returns the new id in the server response. You can set reloadAfterSubmit: false and return the new id in the server response. See this and this answers for details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...