【问题标题】:Reload of jqgrid not happening with loadonce:trueloadonce:true 不会重新加载 jqgrid
【发布时间】:2011-10-27 09:39:07
【问题描述】:

我是使用 jqgrid 的新手。

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

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

我有一个带有以下设置的网格

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

我尝试添加以下代码以重新加载

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

【问题讨论】:

    标签: jquery jqgrid


    【解决方案1】:

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

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

    应该可以。 reloadGrid的附加参数说明见here

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

    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。

    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 答案。

    【讨论】:

    • 感谢您的快速回复。我们尝试了您的解决方案,但仍未触发重新加载。我在 $("#userList").jqGrid('gridResize', { minWidth: 450, minHeight: 150}); 之后添加了您的代码
    • @user862755:抱歉,我不明白你为什么在 'gridResize' 之后放置 'reloadGrid'。网格在定义处已经有datatype: 'xml'。因此 jqGrid 将尝试通过对 url 的相应 Ajax 调用来填充网格。如果您只是在挂起的 ajax 请求期间调用'reloadGrid',则重新加载将被丢弃。您能否先解释一下为什么何时需要重新加载网格?
    • 我想要客户端排序和导航,因此我将'reloadOnce'设置为true。另外,我将使用网格的内置功能添加/修改/删除记录。当它们保存在数据库中时,我想在网格中显示它。
    • 抱歉,我的上一条评论可能给您提供了错误信息。我已经让重新加载按钮功能正常工作了。我希望在添加/修改/删除后自动刷新网格中的数据,目前没有发生。
    • @XCoder:我不确定我的理解是否正确,但如果您的意思是添加/编辑/删除表单,那么刷新问题可以通过使用表单编辑的recreateForm: true 选项来解决。请参阅herehere 等。
    【解决方案2】:

    谢谢,

            $("#list1").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
        }
    });  jQuery("#list1").jqGrid('navGrid','#pager1',{edit:true,add:true,del:true,view:false},{url: "product.php" },{closeAfterEdit: true,closeAfterAdd: true});
           jQuery("#list1").jqGrid('gridResize',{minWidth:350,maxWidth:850,minHeight:80, maxHeight:350}).trigger('reloadGrid');
           $('#list1').jqGrid('setGridParam', {datatype:'xml'});
           $('#list1').trigger('reloadGrid', [{page:1}]);
    

    为我工作。再次非常感谢。

    【讨论】:

      【解决方案3】:
      {
          // edit options
          zIndex: 100,
          url: '/User/Edit',
          closeOnEscape: true,
          closeAfterEdit: true,
          recreateForm: true,
      
          afterComplete: function (response) {
              if (response.responseText) {
                  $("#userGrid").jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');
              }
          }
      }
      

      为我工作:

      逻辑是我们在调用返回到 JqGrid 后再次重新加载网格......试试它的工作原理

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 2011-09-11
        • 2011-11-07
        • 1970-01-01
        相关资源
        最近更新 更多