Javascript 如何解析从 Datatables ajax 调用收到的 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30100791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse JSON received from Datatables ajax call?
提问by gdrt
I can successfully fill my datatable with ajax call, but then I don't know how to parse JSON that is received by datatable with this ajax call.
我可以使用ajax调用成功填充我的数据表,但是我不知道如何使用这个ajax调用解析数据表接收到的JSON。
Here is my JavaScript code, that makes ajax call to the server and fills my datatable correctly:
这是我的 JavaScript 代码,它对服务器进行 ajax 调用并正确填充我的数据表:
$('#transactions').DataTable({
"processing": true,
"ajax": {
"url": "/transactions
},
"columns": [
{ "data": "car"},
{ "data": "card_number"},
{ "data": "invoice"},
{ "data": "status"}
]
});
This is the JSON object returned from the server:
这是从服务器返回的 JSON 对象:
{
"data": [
{
"car": 190,
"card_number": "6395637",
"invoice": 200,
"status": "success"
},
{
"car": 191,
"card_number": "9473650",
"invoice": 180,
"status": "success"
}
],
"balance": 7300
}
As you can see, the dataparameter of the returned JSON object is used by the datatables function to fill by datatables, and now I want to parse the balanceparameter, but I can't. How can i achieve this?
可以看到,data返回的JSON对象的参数被datatables函数用datatables填充,现在想解析这个balance参数,但是不行。我怎样才能做到这一点?
回答by dekkard
Something like this:
像这样的东西:
$('#transactions').dataTable({
"ajax" : {
"url" : "/transactions",
"dataSrc" : function (json) {
// manipulate your data (json)
...
// return the data that DataTables is to use to draw the table
return json.data;
}
}
});
回答by Juan Mendes
Don't use the url feature of DataTable, make the Ajax call yourself
不要使用DataTable的url特性,让Ajax自己调用
$.getJSON('/transactions', function(response) {
$('#transactions').dataTable({
processing: true,
data: response.data,
columns: [
{ data: "car"},
{ data: "card_number"},
{ data: "invoice"},
{ data: "status"}
]
});
window.someGlobalOrWhatever = response.balance
});
回答by Hula_Zell
Since DataTables 1.10, you may use the ajax.json()function: https://datatables.net/reference/api/ajax.json()I have implemented it in the example code below.
从 DataTables 1.10 开始,您可以使用该ajax.json()函数:https: //datatables.net/reference/api/ajax.json()我已经在下面的示例代码中实现了它。
$( document ).ready(function() {
$('#search-form').submit(function(e) {
e.preventDefault();
var table = $('#location-table').DataTable({
destroy: true,
ajax: "/locations.json",
columns: [
{ "data": "code" },
{ "data": "status" },
{ "data": "name" },
{ "data": "region" },
{ "data": "address" },
{ "data": "city" },
{ "data": "state" },
{ "data": "zip" },
{ "data": "phone_number" },
]
})
table.on( 'xhr', function () {
var json = table.ajax.json();
$('#totals').text(json.totals)
});
})
});
NOTE for this to work you must initialize the datatable with $('#location-table').DataTable()and not $('#location-table').dataTable(the difference being the capitalized D)
注意要使其工作,您必须使用$('#location-table').DataTable()和不初始化数据表$('#location-table').dataTable(区别在于大写的 D)

