用JSON填充DataTable时,需要在一个列中呈现一个超链接,并在另一个JSON字段中找到一个值
Populating a DataTable with JSON, need to render a hyperlink in one column with a value found in a different JSON field
我有一个样本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
。
我找到了一个解决办法。
var runbookID;
"aoColumns": [
{"data": function(data, type, row, meta){
runbookID = data.id;
return data.id;}
},
{"data": "title",
"render": function ( data, type, row, meta ) {
return "<a href='runbook?rbID=" + runbookID + "'>" + data + '</a>';}
},
{"data": "author"}
]
相关文章:
- 根据id将json数组组合为一个json数组
- 处理一个JSON文件;完全相同的副本不是
- 使用来自不同异步函数的响应创建一个json对象
- 想要使用Javascript将一个JSON对象拆分为多个JSON对象
- JSON:在另一个JSON对象中重用JSON对象
- 我需要迭代一个JSON数组——不知道如何做到——已经搜索过了,但仍然可以'我一点也不知道
- I'我在页面加载时将整个$_SESSION变量放入一个json对象中.虽然这对我有效,但这是一个好的做法吗
- 创建一个 json cookie 数组
- NodeJS应用程序只显示第一个JSON对象.为什么?
- 从另一个JSON对象创建JSON对象
- 用javascript附加一个json文件
- 如何将两个json对象合并为一个json
- 将json作为子文档合并到另一个json中
- 其中一个json.dumps值为null
- ExtJS DataView只显示最后一个JSON元素
- 如何在javascript中为每个JSON对象添加一个JSON数组
- 从URL获取一个JSON文件并显示
- 我有一个json文件,当我点击按钮时
- 使用 javascript 为 c3js 创建一个 json 对象
- 从 javascript 打开一个 JSON 文件