When you click on "Edit", you take a code in JSON format:
{ "idnomes": 15, "strnome": "JOSIVAN SOUSA", "has_uf": [ { "iduf": 1, "struf": "AC", "strdescricao": "Acre", "bolvisualizar": 1 }, { "iduf": 2, "struf": "PA", "strdescricao": "Para", "bolvisualizar": 1 }, { "iduf": 2, "struf": "MA", "strdescricao": "Maranhão", "bolvisualizar": 1 } ] }
Now you need to make a parse
in that code transforming it into a JSON object:
objeto = JSON.parse('{ "idnomes": 15, "strnome": "JOSIVAN SOUSA", "has_uf": [ { "iduf": 1, "struf": "AC", "strdescricao": "Acre", "bolvisualizar": 1 }, { "iduf": 2, "struf": "PA", "strdescricao": "Para", "bolvisualizar": 1 }, { "iduf": 2, "struf": "MA", "strdescricao": "Maranhão", "bolvisualizar": 1 } ] }');
Then get all the checkbox
it has in the modal (change "meumodal" to the id
of your modal):
elems = document.getElementById("meumodal").getElementsByTagName("input");
Then a loop double will check the checkbox
who have value
equal to the acronym on JSON:
for(x=0;x<objeto.has_uf.length;x++){
for(y=0;y<elems.length;y++){
if(elems[y].value == objeto.has_uf[x].struf){
elems[y].checked = true;
}
}
}
Open the example below:
objeto = JSON.parse('{ "idnomes": 15, "strnome": "JOSIVAN SOUSA", "has_uf": [ { "iduf": 1, "struf": "AC", "strdescricao": "Acre", "bolvisualizar": 1 }, { "iduf": 2, "struf": "PA", "strdescricao": "Para", "bolvisualizar": 1 }, { "iduf": 2, "struf": "MA", "strdescricao": "Maranhão", "bolvisualizar": 1 } ] }');
elems = document.getElementById("meumodal").getElementsByTagName("input");
for(x=0;x<objeto.has_uf.length;x++){
for(y=0;y<elems.length;y++){
if(elems[y].value == objeto.has_uf[x].struf){
elems[y].checked = true;
}
}
}
<div id="meumodal">
<input type="checkbox" value="AC" /> AC
<br />
<input type="checkbox" value="DF" /> DF
<br />
<input type="checkbox" value="MA" /> AC
</div>
This "Upgrade" box is a modal?
– Sam
Yes. But it can be another page without problems.
– Josivan Sousa
And this field "Search name", is it dynamic? Type when typing opens other names?
– Sam
No. That’s all it is. Note that in the first image, the search name "JOSIVAN SOUSA" has several states related to it. I want to leave the states related to it checked in the second image
– Josivan Sousa
I get it. Put the HTML of one of these "edit" buttons for us to see what’s in it and know what to do.
– Sam
I think it is possible to do this using Javascript only: when opening the modal, I call a function that will fetch the states within the table row and compare with the checkboxes. The states you have on the line, I mark the corresponding checkbox in the modal.
– Sam
When I click edit, I get the following data:
{
 "idnomes": 15,
 "strnome": "JOSIVAN SOUSA",
 "has_uf": [
 {
 "iduf": 1,
 "struf": "AC",
 "strdescricao": "Acre",
 "bolvisualizar": 1
 },
 {
 "iduf": 2,
 "struf": "PA",
 "strdescricao": "Para",
 "bolvisualizar": 1
 },
 {
 "iduf": 2,
 "struf": "MA",
 "strdescricao": "Maranhão",
 "bolvisualizar": 1
 }
 ]
}
– Josivan Sousa
I’m just having a hard time getting the checkups checked
– Josivan Sousa