【发布时间】:2015-11-20 21:07:31
【问题描述】:
我有一个带有数据的示例 json 对象:
[
{
"id": 1,
"title": "Fred",
"author": "Flintstone"
}, {
"id": 2,
"title": "Fred",
"author": "Flintstone"
}, {
"id": 3,
"title": "Fred",
"author": "Flintstone"
}.....
HTML
<table class="table" id="tblRunbook">
<thead>
<tr>
<th width="60px">ID</th>
<th width="300px">Title</th>
<th width="200px">Author</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JavaScript:
$(document).ready(function() {
$('#tblRunbook').dataTable({
<!-- Retrieve a static json file, you could also have a URL to a controller method. -->
"sAjaxSource" : "/getRunbooks",
"sAjaxDataProp": "",
<!-- Indicate to dataTable what the field names are we want, in the order we want them in the table. -->
"aoColumns": [
{"data": "id"},
{"data": "title",
"render": function ( data, type, row, meta ) {
return '<a href="runbook?rbID=" + data.id + '>' + data + '</a>';}
},
{"data": "author"}
]
});
});
ID 的列将被隐藏,但我想创建链接 runbook?rbID = ID。我想访问 JSON 对象中的前一个字段,在本例中为 "data" : "id",并将其设置在函数返回 '<a href="runbook?rbID=" + data.id + '>' 的第二个字段中,其中 data.id 是 ID。
【问题讨论】:
-
您可以使用 jQuery map 从无限的其他数据源创建新的数据源,并为数据表使用新模型。数据表只能与一种来源一起使用。但如果它分配的数据可能会很慢,那么在服务器上重做模型可能是一个更好的主意。
-
我觉得这个datatables.net/forums/discussion/200/…会帮助解决。
标签: javascript jquery html json twitter-bootstrap