在jquery Datatables中,可以使用服务器端脚本定义列吗?我需要这样的东西
必须从服务器加载带有日期的列。那么列数可以改变。
发布于 2011-12-29 18:33:49
扩展一下卡迈勒·迪普·辛格所说的:
您可以动态创建表,然后对其应用数据表以获得数据表的功能。
// up in the html
<table id="myDatatable" class="... whatever you need..."></table>
然后:
// in the javascript, where you would ordinarily initialize the datatable
var newTable = '<thead><tr>'; // 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 <th>s
$.each(data[0], function(key, value) {
newTable += "<th>" + key + "</th>";
});
newTable += "</tr></thead><tbody>";
// then load the data into the table
$.each(data, function(key, row) {
newTable += "<tr>";
$.each(row, function(key, fieldValue) {
newTable += "<td>" + fieldValue + "</td>";
});
newTable += "</tr>";
});
newTable += '<tbody>';
$('#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‘或任何相关的数据获取函数--这是将数据表应用到已经存在的表中,这是我们动态创建的表。
好吧,这是一种老生常谈的方式,但它应该是可行的。
目前还没有一种内置的方法来动态地对数据表执行此操作。查看此处:https://github.com/DataTables/DataTables/issues/273
发布于 2011-12-29 21:19:13
我想我找到你要找的东西了
我会粘贴一些代码+发布一个链接到一个类似的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" }
]
}
这里有一个指向原始线程的链接
Column definition via JSON array (ajax)
https://stackoverflow.com/questions/8665309
复制相似问题