javascript - unable to select specific id from JSON string -


so i'm trying build admin page clients informations, , have problem i'm unable select id client show of information in separate page.

basically, in first page site.com/client have small table clients, when click on button visualize, i'll go page site.com/det_client&cliid=3 have information.

this information in json file , each client has unique id. id being send first page second trought url, , i'm getting data jquery. no problem until part. can everthing , proper id client.

the problem starts when need write new table information specific id. tried here (in stackoverflow) solution found giving me errors. code i'm using:

$(document).ready(function() {     var url = "data/c_fisico.json";     var cli; //getting client id url. rest of code in other page      $.getjson( url, function() {         function findid(url, idtolookfor) {             var categoryarray = url.aadata;             (var = 0; < categoryarray.length; i++) {                 if (categoryarray[i].cd == idtolookfor) {                     return(categoryarray[i].id);                 }             }         }     });      var item = findid(url, cli),         table = json.parse(item);      //there more data, made smaller     $.getjson(table, function (response){         var write;         $.each (response, function (index, table) {             write += '<tr><td>cod</td><td>' + table.id + '</td></tr><tr><td>name</td><td>' + table.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + table.cpf + '</td></tr>';         }); //end each         $('#tabletest').html(write);     }); //end getjson  }); 

the error have is: uncaught referenceerror: findid not defined

this example of json:

{     "aadata": [         {             "id":0,             "cd":"c-0001",             "nm_cliente":"john",             "cpf":"000.111.222-33",             "nm_pai":"nome completo",             "nm_mae":"nome completo",             "tel":"00-9966-6699",             "cidade":"city",             "estado":"estate"         },         //rest of code here...     ] } 

so, basically, need information client wich has same id given url, , write on page show details particular client.

you need put code inside callback $.getjson (and don't need function findid). otherwise, need implement callback function (i.e. make request data, async method calls outside function when it's done).

if need load single id, like...

$(document).ready(function() {     var url = "data/c_fisico.json";     var cli; //getting client id url. rest of code in other page      $.getjson( url, function(json_data) {         var categoryarray = json_data.aadata;         (var = 0; < categoryarray.length; i++) {             if (categoryarray[i].cd == cli) {                 writeitem(categoryarray[i]);  //pass matching object writeitem.                 return;             }         }     });  });  function writeitem(item) {     //"item" has been parsed object, it's no longer json string.     var write += '<tr><td>cod</td><td>' + item.cd + '</td></tr><tr><td>name</td><td>' + item.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + item.cpf + '</td></tr>';     $('#mytable').html(write); } 

if need write multiple rows (multiple ids, perhaps via button click) , don't need update data set every call, like...

var my_data = null;  $(document).ready(function() {     var url = "data/c_fisico.json";      //you should add error handling in here, know if json file wasn't available.     $.getjson( url, function(json_data) {         my_data = json_data.aadata;         $("#some_button").attr("disabled", false);  //json data has loaded, enable our button.     });      $("#some_button").attr("disabled", true);  //disable our button user can't click until json loaded.     $("#some_button").on("click", function() {         var id = //figure out id want load here.          writeitem(id);  //call write function.     }); });  function findid(id) {     if (my_data === null) { return null; }  //json hasn't been retrieved yet, can't find anything.      (var = 0; < my_data.length; i++) {         if (my_data[i].cd == id) {             return my_data[i];         }     }      return null;  //the requested id wasn't found. }    function writeitem(id) {     var item = findid(id);     if (item === null) {         //the id wasn't found or json isn't available.         //show error message or whatever you'd like.         return;     }      var write = '<tr><td>cod</td><td>' + item.cd + '</td></tr><tr><td>name</td><td>' + item.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + item.cpf + '</td></tr>';     $('#mytable').append(write);  //appends existing table data. } 

and finally, if need load multiple ids, re-load data source every time, like...

var busy = false;   $(document).ready(function() {     var url = "data/c_fisico.json";      //you should add error handling in here, know if json file wasn't available.     $.getjson( url, function(json_data) {         my_data = json_data.aadata;         $("#some_button").attr("disabled", false);  //json data has loaded, enable our button.     });      $("#some_button").on("click", function() {         var id = //figure out id want load here.          findid(id, writeitem);  //call find function , tell call write function when done.     }); });   function findid(id, callback) {     if (busy) { return; }  //don't call again if we're in process of call.     busy = true;      $.getjson( url, function(json_data) {         var categories = json_data.aadata;         (var = 0; < categories.length; i++) {             if (categories[i].cd == id) {                 //item found, pass object our callback.                 callback(categories[i]);                 busy = false;  //you should set busy = false if getjson call fails.                 return;             }         }          //id wasn't found.  error message.         busy = false;  //you should set busy = false if getjson call fails.     }); }   function writeitem(item) {     //gets called findid callback function.      var write = '<tr><td>cod</td><td>' + item.cd + '</td></tr><tr><td>name</td><td>' + item.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + item.cpf + '</td></tr>';     $('#mytable').append(write);  //appends existing table data. } 

note there error conditions go unhandled examples. i'll leave , implementation!


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -