loadonce:true 不会重新加载 jqgrid [英] Reload of jqgrid not happening with loadonce:true

查看:19
本文介绍了loadonce:true 不会重新加载 jqgrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 jqgrid 的新手.

I am newbie in using jqgrid.

在新页面加载时,网格正在从数据库中正确加载数据.之后由于 loadonce: true 网格没有从数据库中重新加载数据以进行添加/编辑/删除.

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.

我的应用组合是JSP、Servlet、MySQL

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')

有人可以帮忙吗?

对我有用的解决方案

$("#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
}

推荐答案

正确的是 datatype:'xml' 而不是 datatype:xml.所以代码就像

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

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

应该可以.这里了解reloadGrid的附加参数.

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

更新:从您的评论中,我希望我知道您在实施中遇到的问题.如果我现在理解你是正确的,你希望导航栏中的重新加载网格"按钮从服务器重新加载网格.为此,您应该使用您的实现重新定义标准的重新加载网格"按钮,该按钮执行我在答案中包含的代码(见上文).所以你应该使用 refresh: false 作为 navGrid 选项删除标准的重新加载网格"按钮,然后使用 navButtonAdd 添加看起来与标准按钮完全一样的新按钮,并具有您的自定义实现或 onClickButton 事件:

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}]);
        }
    }
);

更新 2:要在添加和编辑操作后强制重新加载网格,您可以使用 afterSubmit 事件,该事件将在接收到成功的服务器响应之后但在 reloadGrid 由 jqGrid 制作.

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
        }
    }
);

我不确定在编辑和删除操作之后是否真的需要从服务器重新加载网格,但是如果服务器没有在服务器响应中返回新的 id,则可能需要在添加操作之后重新加载.您可以设置 reloadAfterSubmit: false 并在服务器响应中返回新的 id.请参阅 thisthis 详细回答.

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.

这篇关于loadonce:true 不会重新加载 jqgrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆