Jquery Datatable发送许多ajax请求

时间:2014-06-21 02:13:18

标签: jquery ajax jquery-datatables

我正在使用jquery DataTable 1.9并面临一个奇怪的问题。我的代码是向服务器发送许多(等于表中的列)请求。下面是代码,我正在使用:

  tableContainer.dataTable({

            sDom: '<"row"<"span6"l><"span6">r>t<"row"<"span6"i><"span6"p>>',
            sPaginationType: 'bootstrap',
            bProcessing: true,
            bServerSide: true,
            bStateSave: false,
            bPaginate: true, 
            oLanguage: {
                sLengthMenu: '_MENU_ records per page'
            },
            bFilter: true,
            bSort: false ,
            // Setup for responsive datatables helper.
            bAutoWidth: false,
            fnPreDrawCallback: function ()
            {
                // Initialize the responsive datatables helper once.
                if (!responsiveHelper)
                {
                    responsiveHelper = new ResponsiveDatatablesHelper(tableContainer, breakpointDefinition);
                }

            },

            fnRowCallback: function (nRow, aData, iDisplayIndex, iDisplayIndexFull)
            {
                responsiveHelper.createExpandIcon(nRow);
            },

            bDestroy: true,
            sAjaxSource: "Getdata",
            bDeferRender: true,
            aoColumns: [

                @foreach (var item in Model.gridAllColumnName)
                {
                    <text> { "sName": "@item" }, </text>
                }

            ]
               ,

            fnServerData: function (sSource, aoData, fnCallback, oSettings) {
                if (oSettings.aaSorting.length)
                {

                    aoData.push({ "name":"popUpId" ,"value": "@Model.popUpId" });
                }


                    oSettings.jqXHR =  $.ajax({

                        "dataType": 'json',
                        "contentType": "application/json; charset=utf-8",
                        "type": "GET",
                        "url": sSource,
                        "data": aoData,
                        "success":
                                    function (msg)
                                    {

                                        var json = jQuery.parseJSON(msg);
                                        fnCallback(json);
                                        $('#' + dataTableId ).removeClass('hidden');
                                    }
                    });

            },
            fnCreatedRow: function( nRow, aData, iDataIndex ) {
                // Bold the grade for all 'A' grade browsers
                $(nRow).attr('onclick', 'getValue("@Model.gridGetValue",this);');
            }

        });

我认为我的问题在函数FnServerData中导致ajax请求等于表中的列。请看看并帮助我。

1 个答案:

答案 0 :(得分:1)

我在以下代码中找到了错误区域:

   @for (int i = 0; i < Model.gridColumnsCount; i++)
      {
        if (!Model.gridHideColumns.Contains(i))
        {
            <text>
        tableContainer.fnSetColumnVis(parseInt(@i), false );

            </text>
        }
       }

上面的代码导致了很多请求。