jeudi 19 janvier 2017

loading data table checkbox if it has in database, checked already

i am doing my school project. I manage to add/delete in database from my data-table checkbox if the value is check then it will add then if uncheck then it will be deleted, Now what i want is , when i go load the data table with checkbox, i want to check already the values that is already in my database. For example, if i have user_id = 102 already in my database, if i load the data-table, i want the name with user_id value of 102 will be automatically check when fetching the data-table checkbox. How can i do it? i am using a codeigniter framework.

Here is my controller in populating my data-table:

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 ajax/javascript:

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
      });
}

and then i put the function into document-ready:

$(document).ready(function() {
 $('#dataTables-docs').dataTable();
  show_docs();

});

what i want is when i will go load my data-table, i want to check all the data-table checkbox if it is already in the database.

Aucun commentaire:

Enregistrer un commentaire