1
I would like to know how to fill in some inputs through data from a json file with jQuery.
Follow the data in Json:
{"data":[
{"id":"0","lNeg":"","cnpjToma":"02.558.157/1001-62","nDoc":"00121961","nPed":"5100484612","dtEmis":"01/11/2016","dtVenc":"01/01/2017","munic":"SÃO PAULO","codNat":"17.06","opts":"","tipoDoc":"","vIcms":"","vDoc":"","irrf":"","inss":"","iss":"","pis":"","cofins":"","cssl":"","Obs":""},
{"id":"1","lNeg":"","cnpjToma":"20.558.756/1232-01","nDoc":"23463546","nPed":"1234979878","dtEmis":"23/01/2016","dtVenc":"25/10/2017","munic":"CAMPINAS","codNat":"12.01","opts":"","tipoDoc":"","vIcms":"","vDoc":"","irrf":"","inss":"","iss":"","pis":"","cofins":"","cssl":"","Obs":""},
{"id":"2","lNeg":"","cnpjToma":"09.333.000/1000-89","nDoc":"98984776","nPed":"7896563535","dtEmis":"09/05/2016","dtVenc":"19/12/2017","munic":"INDAIATUBA","codNat":"14.56","opts":"","tipoDoc":"","vIcms":"","vDoc":"","irrf":"","inss":"","iss":"","pis":"","cofins":"","cssl":"","Obs":""},
{"id":"3","lNeg":"","cnpjToma":"43.115.201/1203-79","nDoc":"24984333","nPed":"4676563531","dtEmis":"08/03/2016","dtVenc":"10/05/2017","munic":"SALTO","codNat":"01.99","opts":"","tipoDoc":"","vIcms":"","vDoc":"","irrf":"","inss":"","iss":"","pis":"","cofins":"","cssl":"","Obs":""},
{"id":"4","lNeg":"","cnpjToma":"11.551.151/1011-62","nDoc":"10121962","nPed":"9900484743","dtEmis":"11/11/2016","dtVenc":"02/03/2017","munic":"ITU","codNat":"13.06","opts":"","tipoDoc":"","vIcms":"","vDoc":"","irrf":"","inss":"","iss":"","pis":"","cofins":"","cssl":"","Obs":""}
]}
Follows the inputs in html:
<div class="col-md-2">
<label for="cnpjToma">CNPJ tomador:</label>
<input type="text" class="form-control" id="cnpjToma" name="cnpjToma">
</div>
<div class="col-md-2">
<label for="nDoc">N° do docum:</label>
<input type="text" class="form-control" id="nDoc">
</div>
<div class="col-md-2">
<label for="nPed" class="text-danger">N° do pedido:</label>
<input type="text" class="form-control" id="nPed">
</div>
<div class="col-md-2">
<label for="dtEmis">Data de emissão:</label>
<input type="text" class="form-control" id="dtEmis">
</div>
<div class="col-md-2">
<label for="dtVenc">Data de vencim:</label>
<input type="text" class="form-control" id="dtVenc">
</div>
Follow the jQuery:
$(document).ready(function(){
var dados;
$.ajax({
url: "detalhes.json",
success: function(data){
dados = data;
}
});
var populateInputs = function(data){
for (var prop in data){
var val = data[prop];
$("#" + prop).val(val);
};
};
populateInputs(dados[0]);
})
Where is this JSON file? You will need to make a request to the server to read it.
– Woss
In the same root directory of the project, on my machine.
– LeAndrade
The data in
data
form an array. You need to indicate exactly how you want to do this fill. You cannot enter all these data separately and at the same time in these same fields.– Brunno Vianna
@Bruuno Vianna What if in case I remove the clasps would work? Because it would be objects inside the object date.
– LeAndrade
You have a rule problem here. For example, for every cnpjToma you have in the json object, you have only one corresponding in your form. I’ve already prepared a code that solves the issue, but I need more details of how exactly you want to fill in.
– Brunno Vianna
For example: you can take only one id on that object and populate the data. Thus, each field will have a value within an id. Another example: you can scan all ids in date and popular the form. Only this will leave the fields with the data only of the last id, since the previous ones will normally be overwritten.
– Brunno Vianna
The problem is not the structure of the data, but how you want to popular this form tendos several possible data for only a corresponding field. Understands?
– Brunno Vianna
@Brunnovianna Then you would need a condition to show according to the id of Json objects, if id 0 fills the fields with the data of object 0 and so on.
– LeAndrade
Yes, as shown below...
– Luan G. C. Rodrigues
@Leandro See my answer and, any doubt, can call me.
– Brunno Vianna
OK Brunovianna I’ll test here and give you a feedback!
– LeAndrade