简体   繁体   English

jQuery按钮单击刷新jqGrid只触发一次

[英]jQuery button click refresh of jqGrid only firing once

I have the following jQuery code which I'm using to populate a jqGrid. 我有以下jQuery代码,我用来填充jqGrid。 It works perfectly posting to my ASP.NET MVC page on the first click of the button. 在第一次单击按钮时,它可以完美地发布到我的ASP.NET MVC页面。 My 我的
problem is, any other clicks past the first it seems to run through the jquery code when clicking the button but it never makes it to the POST page. 问题是,当点击按钮时,任何其他点击超过第一个点击它似乎通过jquery代码运行,但它从未进入POST页面。 Any ideas why? 有什么想法吗?

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

The reason the grid isn't reloading is that you are calling the wrong method. 网格没有重新加载的原因是你调用了错误的方法。 The jqGrid method does approximately this: jqGrid方法大致如下:

  1. Examine the table to see if it is already a grid; 检查表以查看它是否已经是网格; if so, exit. 如果是这样,退出
  2. Turn the table into a grid. 将表格变成网格。
  3. Populate the first page of data. 填充第一页数据。

So the second time you call the method, it does nothing, as per step 1. 因此,第二次调用该方法时,它不会执行任何操作,如步骤1所示。

Instead, you should be calling $("#list").trigger("reloadGrid") on the second and all subsequent clicks. 相反,你应该在第二次和所有后续点击时调用$("#list").trigger("reloadGrid")

Now, because of your mtype in the grid options, the grid is going to do a GET, not a POST. 现在,由于你在网格选项中的mtype,网格将进行GET,而不是POST。 So if the POST is coming from the button itself (in other words, it is an input of type submit), you should return true to indicate that the submit should continue as usual. 因此,如果POST来自按钮本身(换句话说,它是类型提交的输入),您应该返回true以指示提交应该像往常一样继续。

Here is the solution : 这是解决方案:

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

Because I need to POST new values to the the Action for me it does not work "reloadGrid". 因为我需要将POST的新值发布到Action,所以它不起作用“reloadGrid”。

I just remove all the content and create the empty table again. 我只是删除所有内容并再次创建空表。

In the if I check if the grid is there to hide the "empty chart" div (it shows just a message). 如果我检查网格是否存在隐藏“空图表”div(它只显示一条消息)。 In the else I just clean the div that surrounds the table and then I add inside the table again. 在其他地方我只是清理桌子周围的div然后再次添加到表格内部。 When the plugin finds the empty table then it loads the grid completely again. 当插件找到空表时,它再次完全加载网格。

LoadTableData is the function that has the common functionality to create and load the grid. LoadTableData是具有创建和加载网格的通用功能的函数。

Probably this solution is not elegant, but when the time is rushing... 可能这个解决方案并不优雅,但是时间紧迫......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM