2009-07-20 51 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()); 
     });