jquery datatables – 从json获取列

在jquery中,Datatables是否可以使用服务器端脚本定义列? 我需要这样的东西 在此处输入图像描述

必须从服务器加载包含日期的列。 然后列数可以变化。

我想我找到了你想要的东西

我会粘贴一些代码+发布链接到类似的Q’,你将获得更多的信息.​​..

$.ajax( { "url": 'whatever.php', "success": function ( json ) { json.bDestroy = true; $('#example').dataTable( json ); }, "dataType": "json" } ); 

其中json是这样的

 { "aaData": [ [ "2010-07-27 10:43:08", "..."], [ "2010-06-28 17:54:33", "..."], [ "2010-06-28 16:09:06", "..."], [ "2010-06-09 19:15:00", "..."] ] , "aaSorting": [ [ 1, "desc" ] ], "aoColumns": [ { "sTitle": "Title1" }, { "sTitle": "Title2" } ] } 

这里是原始主题的链接

通过JSON数组(ajax)定义列

扩展Kamal Deep Singh所说的内容:

您可以动态地动态创建表,然后将数据表应用于它以获取数据表的function。

 // up in the html 

然后:

 // in the javascript, where you would ordinarily initialize the datatable var newTable = ''; // start building a new table contents // then call the data using .ajax() $.ajax( { url: "http://my.data.source.com", data: {}, // data, if any, to send to server success: function(data) { // below use the first row to grab all the column names and set them in s $.each(data[0], function(key, value) { newTable += "" + key + ""; }); newTable += ""; // then load the data into the table $.each(data, function(key, row) { newTable += ""; $.each(row, function(key, fieldValue) { newTable += "" + fieldValue + ""; }); newTable += ""; }); newTable += ''; $('#myDatatable').html(newTable); // replace the guts of the datatable's table placeholder with the stuff we just created. } }); // Now that our table has been created, Datatables-ize it $('#myDatatable').dataTable(); 

请注意,您可以将设置放在.dataTable()中,但不是’sAjaxSource’或任何相关的数据获取函数 – 这是将数据表应用于已存在的表,即我们在运行中创建的表。

好的,所以这是一种hacky方式,但它应该工作。

目前没有内置的方法可以动态地使用数据表。 请参见: https : //github.com/DataTables/DataTables/issues/273