1

I have below code for controller,

 public function user_ajaxtest()
 {
    $userlistingx = $this->model_usermanage->viewuserlisting();
    $userlistingy = array(
            array('select' => '<input type="checkbox" name="id[]" value="">')
            );

    $userlisting1['data'] = array_merge($userlistingx, $userlistingy);
    echo json_encode($userlisting1);
}

With this AJAX i am sending user list to DataTables. $userlistx giving me users array and i could able to see everything, But i would like to add checkbox for each row of datatable, For that i had created one more array $userlistingy, and then merge this two array,

With this, i am getting one more row instead of column in datatable,

See below ajax response which i am getting normally without merging array means, only echo json $userlistingx.

data: [{test_id: "8", user_id: "28", usernote: "1 image 1 attachment", department_id: "21",…},…] 0: {test_id: "9", user_id: "29", usernote: "1 image 1 attachment" 1: {test_id: "10", user_id: "30", usernote: "1 image 1 attachment"

But when i merget and send Json i get below,

data: [{test_id: "8", user_id: "28", usernote: "1 image 1 attachment", department_id: "21",…},…] 0: {test_id: "9", user_id: "29", usernote: "1 image 1 attachment" 1: {test_id: "10", user_id: "30", usernote: "1 image 1 attachment" 2: {select: "<input type="checkbox" name="id[]" value="">"}

In short question, How do i add select checkbox array to all my datarows?

1
  • Your controller probably shouldn't be passing HTML. Your view should handle all HTML. Commented Apr 22, 2015 at 4:54

2 Answers 2

0

you can try this

 $userlistingx = $this->model_usermanage->viewuserlisting();
 for($i=0;$i<count($userlistingx);$i++)
  {
      $userlistingy[]='<input type="checkbox" name="id[]" value="">';
  }
$userlisting1['data'] = array_merge($userlistingx, $userlistingy);
echo json_encode($userlisting1);
Sign up to request clarification or add additional context in comments.

Comments

0

There is no need to pass the checkbox in the response. Add them to the table you are using.

Check this out

HTML

<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
    <thead>
        <tr>
            <th>name</th>
            <th>option</th>
        </tr>
    </thead>
</table>

Script

$('#example').dataTable( {
    ajax: "yourpage.php",
    columns: [
        { data: "name" },
        {
            data:   "your_data",
            render: function ( data, type, row ) {
                return '<input type="checkbox" name="name_check" value="">';
            },
        }
    ],

} );

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.