DataTables 如何使用来自 json 个对象的多个数据
DataTables how to use multiple data from json objects
所以我想做的是从 json
文件中调用一些数据来填充我的 table。但是我不想拥有很多 json
文件,而是想将一些数据保存在一个文件中。例如,我有两种类型的客户(个人和公司),我想将它们都放在相同的 json
中,但作为不同的对象,因为每个都在不同的 table.[=22 中=]
问题是:我无法访问JSON
文件中的数据。
如果我在不同的文件中使用 2 种类型的客户端,我可以正常使用它(person.json 和 company.json - 这很好用)。
我确实尝试了这 2 个示例,但其中 none 个对我有用。
- https://www.datatables.net/examples/ajax/deep.html
- https://www.datatables.net/examples/ajax/objects_subarrays.html
那么,我怎样才能从文件中获取数据呢?这是我的 json 和我的数据 table 代码:
{
"clients": {
"person": [
{
"cd":0,
"id":"C-0010",
"nm_cliente":"Name AAA",
"dt_nasc":"02/11/1990",
"info":"Some basic info"
},
{
"cd":1,
"id":"C-0013",
"nm_cliente":"Name BBB",
"dt_nasc":"02/11/1990",
"info":"Some basic info"
},
{
"cd":2,
"id":"C-0017",
"nm_cliente":"Name CCC",
"dt_nasc":"02/11/1990",
"info":"Some basic info"
}
],
"company": [
{
"cd":0,
"id":"C-0032",
"nm_cliente":"Name Client",
"num_cnpj":"111.222.3333/0001-22",
"nm_cidade":"City AAA"
},
{
"cd":1,
"id":"C-0033",
"nm_cliente":"Client Name",
"num_cnpj":"111.222.3333/0001-22",
"nm_cidade":"City BBB"
},
{
"cd":2,
"id":"C-0035",
"nm_cliente":"jEmpresa teste",
"num_cnpj":"111.222.3333/0001-22",
"nm_cidade":"City CCC"
}
]
}
}
我已经用jsonlint.com
验证过了,我的JSON
一切正常。这就是我如何尝试将这些数据调用到我的 tables.
TableA - 客户类型 Person
//rest of the code goes here...
"aoColumns" : [
{ "mData": "person.id" },
{ "mData": "person.nm_cliente" },
{ "mData": "person.dt_nasc" },
{ "mData": "person.info" }
]
表 B - 客户类型公司
//rest of the code goes here...
"aoColumns" : [
{ "mData": "company.id" },
{ "mData": "company.nm_cliente" },
{ "mData": "company.nm_cnpj" },
{ "mData": "company.nm_cidade" }
]
我不断收到类似 "lenght not defined"
的错误
谁能帮帮我?
您必须引用 person
和 company
数组本身,而不是项目。更新以演示加载 JSON 后的初始化以及如何重用和操作选项:
//an options object, some example settings
var options = {
bPaginate: true,
sPaginationType: "full_numbers",
aLengthMenu: [25,50,100,500]
//etc
}
function initialize(json) {
options.aaData = json.clients.person;
options.aoColumns = [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "dt_nasc" },
{ "mData": "info" }
];
$("#tableA").dataTable(options);
options.aaData = json.clients.company;
options.aoColumns = [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "num_cnpj" },
{ "mData": "nm_cidade" }
];
$("#tableB").dataTable(options);
}
$.getJSON("your.json", function(json) {
initialize(json);
});
显示如何注入 JSON -> http://jsfiddle.net/3g5wcyet/
的原始演示
可重用选项演示 -> http://jsfiddle.net/bpgvfefd/
所以我想做的是从 json
文件中调用一些数据来填充我的 table。但是我不想拥有很多 json
文件,而是想将一些数据保存在一个文件中。例如,我有两种类型的客户(个人和公司),我想将它们都放在相同的 json
中,但作为不同的对象,因为每个都在不同的 table.[=22 中=]
问题是:我无法访问JSON
文件中的数据。
如果我在不同的文件中使用 2 种类型的客户端,我可以正常使用它(person.json 和 company.json - 这很好用)。
我确实尝试了这 2 个示例,但其中 none 个对我有用。
- https://www.datatables.net/examples/ajax/deep.html
- https://www.datatables.net/examples/ajax/objects_subarrays.html
那么,我怎样才能从文件中获取数据呢?这是我的 json 和我的数据 table 代码:
{
"clients": {
"person": [
{
"cd":0,
"id":"C-0010",
"nm_cliente":"Name AAA",
"dt_nasc":"02/11/1990",
"info":"Some basic info"
},
{
"cd":1,
"id":"C-0013",
"nm_cliente":"Name BBB",
"dt_nasc":"02/11/1990",
"info":"Some basic info"
},
{
"cd":2,
"id":"C-0017",
"nm_cliente":"Name CCC",
"dt_nasc":"02/11/1990",
"info":"Some basic info"
}
],
"company": [
{
"cd":0,
"id":"C-0032",
"nm_cliente":"Name Client",
"num_cnpj":"111.222.3333/0001-22",
"nm_cidade":"City AAA"
},
{
"cd":1,
"id":"C-0033",
"nm_cliente":"Client Name",
"num_cnpj":"111.222.3333/0001-22",
"nm_cidade":"City BBB"
},
{
"cd":2,
"id":"C-0035",
"nm_cliente":"jEmpresa teste",
"num_cnpj":"111.222.3333/0001-22",
"nm_cidade":"City CCC"
}
]
}
}
我已经用jsonlint.com
验证过了,我的JSON
一切正常。这就是我如何尝试将这些数据调用到我的 tables.
TableA - 客户类型 Person
//rest of the code goes here...
"aoColumns" : [
{ "mData": "person.id" },
{ "mData": "person.nm_cliente" },
{ "mData": "person.dt_nasc" },
{ "mData": "person.info" }
]
表 B - 客户类型公司
//rest of the code goes here...
"aoColumns" : [
{ "mData": "company.id" },
{ "mData": "company.nm_cliente" },
{ "mData": "company.nm_cnpj" },
{ "mData": "company.nm_cidade" }
]
我不断收到类似 "lenght not defined"
的错误谁能帮帮我?
您必须引用 person
和 company
数组本身,而不是项目。更新以演示加载 JSON 后的初始化以及如何重用和操作选项:
//an options object, some example settings
var options = {
bPaginate: true,
sPaginationType: "full_numbers",
aLengthMenu: [25,50,100,500]
//etc
}
function initialize(json) {
options.aaData = json.clients.person;
options.aoColumns = [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "dt_nasc" },
{ "mData": "info" }
];
$("#tableA").dataTable(options);
options.aaData = json.clients.company;
options.aoColumns = [
{ "mData": "id" },
{ "mData": "nm_cliente" },
{ "mData": "num_cnpj" },
{ "mData": "nm_cidade" }
];
$("#tableB").dataTable(options);
}
$.getJSON("your.json", function(json) {
initialize(json);
});
显示如何注入 JSON -> http://jsfiddle.net/3g5wcyet/
的原始演示可重用选项演示 -> http://jsfiddle.net/bpgvfefd/