javascript - jQuery DataTables Ajax Data Source -
i find documentation datatables when using ajax data source limited. i've got method in controller in asp.net mvc 4 application returns simple json result, , i'm trying fill datatable following:
$.ajax({ type: "get", datatype: "json", contenttype: "application/json", url: "/chart/ajax", success: function (data) { var returndata = []; (var = 0; < data.length; i++) { //makes data array of arrays returndata.push($.makearray(data[i])); } $('#secondary').datatable({ "aadata": returndata, "aocolumns": [ { "stitle": "drugclass" }, { "stitle": "drugclasssize" }, { "stitle": "distinctpatients" }, ] }); } });
ideally, wouldn't create table element until success callback had fired, in instance empty table element on page. following code error: uncaught typeerror: object [object object] has no method 'datatable'
thing is, have different datatable on page , works fine. script @ bottom of page, after working data table. why getting error , what's easy way datatables play nice ajax data sources?
it seems though putting initialisation of datatable in success of ajax call, , need set other way round i.e initialise datatable , set correct options , plugin take care of rest.
you have controller action returns json result, need set sajaxsource
url, in case: "sajaxsource": "/chart/ajax"
.
you going in success of ajax call , set function fnserverdata
option, shown below:
$('#secondary').datatable( { "bprocessing": true, "bserverside": true, "sajaxsource": "/chart/ajax", "fnserverdata": function ( ssource, aodata, fncallback ) { var returndata = []; (var = 0; < aodata.length; i++) { //makes data array of arrays returndata.push($.makearray(aodata[i])); } $.getjson( ssource, returndata , function (json) { /* whatever additional processing want on callback, tell datatables */ fncallback(json) } ); } } );
the fncallback send json datatable in view.
Comments
Post a Comment