ajax - DataTables how to use multiple data from json objects -


so i'm trying call data json file fill table. instead of having lot of json files, i'd keep of data in single file. example, have 2 types of clients (person , company) , want keep them both in same json, different objects, because each 1 goes in different table.

the problem is: can't access data in json file.

i can use if use 2 type of clients in different files (person.json , company.json - works fine).

i did tried these 2 examples, none of them worked me.

so, how can data file? here json , datatable code:

{     "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"             }         ]     } } 

i used jsonlint.com verify, , ok json. , here how i'm trying call data tables.

tablea - client type person

//rest of code goes here... "aocolumns" : [     { "mdata": "person.id" },     { "mdata": "person.nm_cliente" },     { "mdata": "person.dt_nasc" },     { "mdata": "person.info" } ] 

tableb - client type company

//rest of code goes here... "aocolumns" : [     { "mdata": "company.id" },     { "mdata": "company.nm_cliente" },     { "mdata": "company.nm_cnpj" },     { "mdata": "company.nm_cidade" } ] 

i keep getting error "lenght not defined"

can me?

you must refer person , company arrays themselves, not items. updated demonstrate initialization after load of json , how reuse , manipulate options :

//an options object, 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); }); 

original demo showing how inject json -> http://jsfiddle.net/3g5wcyet/

reuseable options demo -> http://jsfiddle.net/bpgvfefd/


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -