jeudi 19 janvier 2017

how to condition data table checkbox if check/uncheck

i dont know how to condition the checkbox inside the datatable. I want to condition if the checkbox in data table is check, i will insert it in my database, and if it is uncheck , i will delete it in database. I am using a codeigniter framework.

Here is my controller:

public function getalldocs() {
        $listdocs = $this->Admin_model->getdoctors();
        $data = array();
        foreach ($listdocs as $docs) {
            $row = array();  
            $row[] = $docs->user_fname;
            $row[] = $docs->user_mname;
            $row[] = $docs->user_lname;
            $row[] = '<input name="user_id[]" value="'.$docs->user_id.'" type="checkbox">';

            $data[] = $row;
        }
        $output = array(   
            "data" => $data,
        );
        echo json_encode($output);
    }

here is my javascript, i manage to alert the value of user_id when i click the checkbox but i dont know how to condition it, wether it was check or checked and uncheck again. Here it is:

function show_docs() {
    $("#dataTables-docs").dataTable().fnDestroy();

    table =  $('#dataTables-docs').DataTable({ 
      "ajax": {
              "url": "<?php echo site_url('admin_controls/getalldocs')?>",
              "type": "POST",
          },
          responsive: true,
          className: 'select-checkbox',
          'bInfo': false,
          'paging': false
      });
}

$('#dataTables-docs tbody').on('click', 'input[type="checkbox"]', function(e){

  var user_id = $(this).val();
  alert(user_id);

  });

Here is my view:

 <table id="dataTables-docs"  class="table table-striped table-bordered table-hover dataTable dtr-inline" role="grid" style="width: 100%;" width="100%" aria-describedby="dataTables-material">
                    <thead>
                        <tr> 
                            <th>First Name</th>
                            <th>Middle Name</th>
                            <th>Last Name</th>                                               
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
    </tbody>
</table>

Aucun commentaire:

Enregistrer un commentaire