如何通过Ajax显示jQuery DataTables中的JSON数据?
How to display JSON data in jQuery DataTables via Ajax?
我一直在尝试在 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 吗?
解决方案
使用 ajax.dataSrc
选项在您的 JSON 响应中指定 属性 持有数据。
例如:
$('#example').DataTable({
// ... skipped other options ...
ajax: {
url: "http://localhost/example22/index.php?r=site/display",
dataSrc: 'allk'
},
columns: [
{ data: "name"},
{ data: "designation"},
{ data: "address" },
{ data: "salary"}
]
});
演示
有关代码和演示,请参阅 this jsFiddle。
在 Nodejs 中,当你有一个像这样声明的数据表时
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Parents Name</th>
<th>Age</th>
</tr>
</thead>
</table>
那么你的java脚本应该是这样的
$(document).ready(function() {
$("#example").DataTable({
ajax: {
url: "../kidsinfo",
dataSrc: ""
},
columns: [
{ data: "kid_name" },
{ data: "class" },
{ data: "parents_name" },
{ data: "age" },
],
iDisplayLength: 1,
iDisplayStart: 0
});
});
这里要注意的重要一点是你的json来自服务器的数据是这样的
[{"id":1,"kid_name":"John","class":"Hancock","parents_name":"dharam","age":"21"}]
那么您的 java 脚本代码应该使用收到的 json 数据中的名称 kid_name
来关联您要在列中显示的信息。
如果服务器的json数据是这样的
{
"info": [{
"id": 1,
"kid_name": "John",
"class": "Hancock",
"parents_name": "dharam",
"age": "21"
}]
}
那么你的 java 脚本应该有 dataSrc: "info"
我一直在尝试在 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 吗?
解决方案
使用 ajax.dataSrc
选项在您的 JSON 响应中指定 属性 持有数据。
例如:
$('#example').DataTable({
// ... skipped other options ...
ajax: {
url: "http://localhost/example22/index.php?r=site/display",
dataSrc: 'allk'
},
columns: [
{ data: "name"},
{ data: "designation"},
{ data: "address" },
{ data: "salary"}
]
});
演示
有关代码和演示,请参阅 this jsFiddle。
在 Nodejs 中,当你有一个像这样声明的数据表时
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>Parents Name</th>
<th>Age</th>
</tr>
</thead>
</table>
那么你的java脚本应该是这样的
$(document).ready(function() {
$("#example").DataTable({
ajax: {
url: "../kidsinfo",
dataSrc: ""
},
columns: [
{ data: "kid_name" },
{ data: "class" },
{ data: "parents_name" },
{ data: "age" },
],
iDisplayLength: 1,
iDisplayStart: 0
});
});
这里要注意的重要一点是你的json来自服务器的数据是这样的
[{"id":1,"kid_name":"John","class":"Hancock","parents_name":"dharam","age":"21"}]
那么您的 java 脚本代码应该使用收到的 json 数据中的名称 kid_name
来关联您要在列中显示的信息。
如果服务器的json数据是这样的
{
"info": [{
"id": 1,
"kid_name": "John",
"class": "Hancock",
"parents_name": "dharam",
"age": "21"
}]
}
那么你的 java 脚本应该有 dataSrc: "info"