what is wrong this source?

what is wrong this source?

mirrormanmirrorman Posts: 1Questions: 1Answers: 0
edited December 2014 in Free community support

Hi~

i confused how to set data-source in table.
text file based data is good working but, server-side sample is not working.
is anything problems in below source?

really thanks your interesting.

// script part
$('#table_id').DataTable({
            sAjaxSource : '/reg/test.do',
            fnServerData : function(sSource, aoData, fnCallback, oSettings) {
                $.ajax({
                    "dataType" : 'json',
                    "type" : "POST",
                    "url" : sSource,
                    "data" : aoData,
                    "success" : cbSearch
                });
            },
            processing : true,
            serverSide : true,
            scrollX : 800,
            scrollY : 400

        });

var cbSearch = function cbSearch(responseText, statusText) {

        if (responseText == null) {
            alert("error");
        }
        else if (responseText != null) {
            var tb = $("#table_id").DataTable();
            tb.fnClearTable();
            tb.fnAddData(responseText["DATA"]);
            tb.fnDraw();
        }
    };

// contents of responseText["DATA"] 

{
"aaData": [    
[      "Tiger Nixon",      "System Architect",      "Edinburgh",      "5421",      "2011/04/25",      "$320,800"    ],
[      "Garrett Winters",      "Accountant",      "Tokyo",      "8422",      "2011/07/25",      "$170,750"    ],
[      "Ashton Cox",      "Junior Technical Author",      "San Francisco",      "1562",      "2009/01/12",      "$86,000"    ],
[      "Cedric Kelly",      "Senior Javascript Developer",      "Edinburgh",      "6224",      "2012/03/29",      "$433,060"    ],
[      "Airi Satou",      "Accountant",      "Tokyo",      "5407",      "2008/11/28",      "$162,700"    ],
[      "Brielle Williamson",      "Integration Specialist",      "New York",      "4804",      "2012/12/02",      "$372,000"    ],
[      "Herrod Chandler",      "Sales Assistant",      "San Francisco",      "9608",      "2012/08/06",      "$137,500"    ],
[      "Rhona Davidson",      "Integration Specialist",      "Tokyo",      "6200",      "2010/10/14",      "$327,900"    ]
]}

Answers

  • allanallan Posts: 63,695Questions: 1Answers: 10,500 Site admin

    To start with I would suggest using ajax rather than the old fnServerData method - since you are already using some 1.10 style parameters. Might as well be consistent!

    Also you will get Javascript errors using the fnClearTable (etc) methods since you are using $().DataTable(). You want to use the new API methods.

    Finally, since you have server-side processing enabled, the fnAddData (or newer rows.add()) are virtually useless since the redraw will make a request to the server for new data anyway.

    One more thing - do you actually need server-side processing? Are you working with 50k+ rows? Your JSON data doesn't contain the information needed for server-side processing.

    Allan

This discussion has been closed.