Mark checkbox from bd result

Asked

Viewed 39 times

0

I have a table that brings data from comic’s cameras and through it I check the cameras and write their id in the same column divided by ','. I wanted to know how I do for when I go to edit bring the data of the cameras and mark the ones that were registered in the bank.

code of how to load table cameras

Function loads_cameras(){

    var url = "../api/cameras/return.php?condominio_id="+$("#condominio_id").val();
    $.post(url, function( data ) {
        var temp = '';

        temp = "<thead><tr><th onclick=\"selall2();\">Selecionar</th><th>ID</th><th>Descrição</th><th>Tipo</th></tr></thead><tbody>";

        $.each(data,function(index,value){

            temp+="<tr>"+
                                "<td><input type=\"checkbox\" id=\"user-"+value.id+"\"  name=\"user-"+value.id+"\" class=\"scheck2\"/></td>"+
                                "<td>"+value.id+"</td>"+
                                "<td>"+value.descricao+"</td>"+
                                "<td>"+value.tipo+"</td>"+
                        "</tr>";
        });

        temp+= "</tbody>";

        if (table2 != false){
            table2.destroy();
        }
     $('#tabelacameras').html(temp);
     setTimeout(function(){
            if ( $.fn.dataTable.isDataTable( '#tabelacameras' ) ) {
            table2 = $('#tabelacameras').DataTable();
            }else {
                table2 = $('#tabelacameras').DataTable( {
                    paging: false,
                } );
            }
     },800);

    },"json");

}

1 answer

0


I finally found the answer

and my editing Function was like this:

function editpopup(id){
        $("#myModal").modal('show');
        carregar_cameras_condominio();
        $.each(global_ar,function(index,value){
            if (value.id == id){
                $("#id").val(value.id);
                $("#descricao").val(value.descricao);
                $("#cameras").val(value.cameras);
                var xcam = value.cameras.split(',');
                var xa = value.tipos.split(',');
                setTimeout(function(){

                    for (var i = 0; i < xcam.length; i++) {

                      $("#user-"+xcam[i]).prop("checked",true);
                    }

                },1000);
                for (var i = 0; i < xa.length; i++) {
                $("#user-"+xa[i]).prop("checked",true);
            }
        }       
        });
    }

I use setTimeout to allow time to load the camera list and only then mark the checkbox as true.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.