0

Hi I am wondering if it's possible to do something like selecting a row within a datatable and have a button "delete" with a method in the controller.

My delete button is to remove the row from the database itself and refresh the page.

This is what my view contains:

 <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){
              $('#datatables').DataTable();
          });
        </script>

    <table id= "datatables" class = "table">
        <thead>
          <tr>

            <th> Patient Name </th>
            <th> Patient`enter code here` ID </th>
          </tr>
        </thead>

        <tbody class = "list">
          <?php foreach ($patients as $patient): ?>
            <tr>
              <td><?=$patient->first_name ?></td>
              <td><?=$patient->patientID ?></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>

(delete button) echo anchor('something/delete', 'Delete', 'class= "some class"');

What i want to do is:

Get the id of the selected row in the datatable and pass it to another page in a controller for processing.

Is that possible?

4
  • Something like: <?php echo anchor('something/delete/' . $patient->patientID, 'Delete', 'class= "some class"'); ?> ? Commented Sep 16, 2015 at 15:39
  • exactly. But the thing is how would i know what row was selected in the datatable. Is that possible? Commented Sep 16, 2015 at 15:52
  • Put that button in foreach loop and make one for every row. That you are asking? Commented Sep 16, 2015 at 15:54
  • I can do that, but what I'm planning is to do a checkbox for each row then pressing the delete button will delete all the checked row. I personally think that looks better. Commented Sep 16, 2015 at 16:00

1 Answer 1

1

since you are using DataTables plugin. when defining your columns, add a column for checkboxes, like so:

        { 
            "data": null,
            "defaultContent": "", 
            'class':'user_chkbox', 
            "searchable": false,
            "render": function ( data, type, full, meta ) {

                var checkbox = "<input type='checkbox' name='user_ids[]' value='" + data.id + "' />";

                return checkbox ;


            }
        },

you can get the list of id's to delete by accessing the 'user_ids' post data.

$user_ids = $this->input->post('user_ids');

you can then in your submit handler function:

$this->db->where_in('id',$user_ids);
$this->db->delete('user_table');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.