sarvesh kushwaha
sarvesh kushwaha

Reputation: 342

Jquery Datatable sending many ajax requests

I am using jquery DataTable 1.9 and facing a weird problem.My code is sending many(equal to the column in a table) requests to the server.Below is the code , I am using :

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

        });

I think my issue is in function FnServerData which is causing ajax requests equal to column in table.Please take a look and help me out.

Upvotes: 0

Views: 489

Answers (1)

sarvesh kushwaha
sarvesh kushwaha

Reputation: 342

I found the error area it was in the following code :

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

            </text>
        }
       }

My above code was causing the many requests.

Upvotes: 1

Related Questions