2009-07-20 67 views
10

我有以下jQuery代码,我用它来填充一个jqGrid。它可以在第一次点击按钮时完美发布到我的ASP.NET MVC页面。我的
问题是,当点击按钮时,似乎通过jquery代码运行的任何其他点击,但它永远不会进入POST页面。任何想法为什么?jQuery按钮点击刷新jqGrid只发射一次

<script type="text/javascript"> 

     $(document).ready(function() { 
      $('#btnSubmit').click(function() { 

       /* Refreshes the grid */ 
       $("#list").jqGrid({ 
        /* The controller action to get the grid data from */ 
        url: '/CRA/AddPart', 
        data: { partNumber: "123"}, 
        datatype: 'json', 
        mtype: 'GET', 
        /* Define the headers on the grid */ 
        colNames: ['col1', 'col2', 'col3', 'col4'], 
        /* Define what fields the row columns come from */ 
        colModel: [ 
        { name: 'col1', index: 'invid', width: 290 }, 
        { name: 'col2', index: 'invdate', width: 290 }, 
        { name: 'col3', index: 'amount', width: 290, align: 'right' }, 
        { name: 'col4', index: 'tax', width: 290, align: 'right'}], 
        height: 'auto', 
        rowNum: 10, 
        rowList: [10, 20, 30], 
        sortname: 'id', 
        sortorder: "desc", 
        viewrecords: true, 
        imgpath: '../../Scripts/jgrid/themes/steel/images', 
        caption: 'Core Return Authorization Contents:', 
        cellEdit: true 
       }); 
      }); 
     }); 

    </script> 

回答

21

网格不重新加载的原因是您调用了错误的方法。 jqGrid方法大致如下:

  1. 检查表以查看它是否已经是网格;如果是的话,退出。
  2. 将表格变成网格。
  3. 填充数据的第一页。

所以你调用该方法第二次,它什么都不做,按照步骤1

相反,你应该调用在第二和所有后续点击$("#list").trigger("reloadGrid")

现在,由于您在网格选项中的mtype,网格将执行GET,而不是POST。因此,如果POST来自按钮本身(换句话说,它是输入类型提交),则应该返回true以指示提交应该像往常一样继续。

+1

,完美的工作,谢谢! – 2009-07-20 21:05:34

6

这里是解决方案:

<script type="text/javascript"> 
     var firstClick = true; 
     $(document).ready(function() { 
      $('#btnSubmit').click(function() { 
       if (!firstClick) { 
        $("#list").trigger("reloadGrid"); 
       } 
       firstClick = false; 
       /* Refreshes the grid */ 
       $("#list").jqGrid({ 
        /* The controller action to get the grid data from */ 
        url: '/CRA/AddPart', 
        data: { partNumber: "123"}, 
        datatype: 'json', 
        mtype: 'GET', 
        /* Define the headers on the grid */ 
        colNames: ['col1', 'col2', 'col3', 'col4'], 
        /* Define what fields the row columns come from */ 
        colModel: [ 
        { name: 'col1', index: 'invid', width: 290 }, 
        { name: 'col2', index: 'invdate', width: 290 }, 
        { name: 'col3', index: 'amount', width: 290, align: 'right' }, 
        { name: 'col4', index: 'tax', width: 290, align: 'right'}], 
        height: 'auto', 
        rowNum: 10, 
        rowList: [10, 20, 30], 
        sortname: 'id', 
        sortorder: "desc", 
        viewrecords: true, 
        imgpath: '../../Scripts/jgrid/themes/steel/images', 
        caption: 'Core Return Authorization Contents:', 
        cellEdit: true 
       }); 
      }); 
     }); 

    </script> 
1

因为我需要发布新的价值为我的行动也不起作用“reloadGrid”。

我只是删除所有内容并再次创建空表。

在如果我检查网格是否有隐藏“空图”div(它只显示一条消息)。在其他方面,我只是清理围绕桌子的div,然后再次添加到桌子里面。当插件找到空表时,它会再次加载网格。

LoadTableData是具有创建和加载网格的通用功能的函数。

这大概的解决方案是不优雅,但到时候抢着...

$("#btnDownloadsPerFile").click(function() { 
      if ($('#chartContainerDownloadsPerFile .ui-jqgrid').length == 0) { 
       $('#chartContainerDownloadsPerFile .emptyChart').hide(); 
      } 
      else { 
       $('#chartContainerDownloadsPerFile').empty(); 
       $('#chartContainerDownloadsPerFile').append('<table id="downloadsPerFileGrid"></table>'); 
      } 
      LoadTableData("downloadsPerFileGrid", $('#ddlportalOptionsDownloadsPerFile').val(), "downloadsPerFile", $('#ddlTimeOptionsDownloadsPerFile').val(), $('#ddlTimeValuesDownloadsPerFile').val(), $('#ddlCountries').val()); 
     });