我一直在尝试在jQuery DataTables组件中获取JSON数据。
首先,我编写了一个JavaScript和一个视图,如下所示:
$.fn.dataTable.Editor({
ajax: "http://localhost/example22/index.php?r=site/display",
table: "#example",
fields: [{
label: "Name:",
name: "name"
}, {
label: "Designation:",
name: "designation"
}, {
label: "Address:",
name: "address"
}, {
label: "Salary:",
name: "salary"
}]
});
$('#example').DataTable({
lengthChange: false,
ajax: "http://localhost/example22/index.php?r=site/display",
columns: [{
allk: "name"
}, {
allk: "designation"
}, {
allk: "address"
}, {
allk: "salary"
}],
select: true
});
和视图,如
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Designation</th>
<th>Address</th>
<th>Salary</th>
</tr>
</thead>
</table>
和提供的url分别包含以下JSON数据
{
"allk": [
{
"name": "raju",
"designation": "developer",
"address": "he is from viswasapuram",
"salary": "30000"
},
{
"name": "bob",
"designation": "designer",
"address": "no idea",
"salary": "100000"
},
{
"name": "bob",
"designation": "designer",
"address": "no idea",
"salary": "100000"
},
{
"name": "suresh",
"designation": "designer",
"address": "fffswss",
"salary": "1212"
},
{
"name": "john",
"designation": "designer",
"address": "california",
"salary": "3000000"
},
{
"name": "suresh",
"designation": "tester",
"address": "he is from cheeran maanagar",
"salary": "20000"
}
]
}
有人能帮助我如何在此应用程序中使用DataTables吗?
2条答案
按热度按时间k0pti3hp1#
溶液
使用
ajax.dataSrc
选项指定保存JSON响应中数据属性例如:
演示版
有关代码和演示,请参见this jsFiddle。
gcxthw6b2#
在Nodejs中,当您有这样声明的DataTable时
那么你的java脚本应该是这样的
这里需要注意的一点是,来自服务器的json数据如下所示
那么您的javascript代码应该使用来自所接收的json数据的名称
kid_name
来关联您希望在列中显示的信息。如果来自服务器的json数据是这样的
那么你的java脚本应该有
dataSrc: "info"