user9855854
user9855854

Reputation:

Ajax format data on CodeIgniter with datatables

I want to get information from a database and show it in a tab with Datatables and JQuery on CodeIngniter 3. My table in my view :

    <table id="myTable" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Last_name</th>
            </tr>
        </thead>
    </table>

My js script :

$(document).ready(function(){
$('#myTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "order":[],
        "ajax": {
        "url": '<?= base_url('main/getData');?>',
        "type": "POST",
         "data": { '<?php echo $csrf_token_name; ?>' : '<?php echo $csrf_token_hash; ?>' }
        },
    });
})

My controller method :

public function getData()
    {
     $this->security->get_csrf_token_name();
     $this->security->get_csrf_hash();
        $table = $this->main_model->data();
        if (count($table) > 0) 
        {
            foreach ($table as $row) 
            {
                $tab = array();
                $tab["name"] = $row->name;
                $tab["last_name"] = $row->last_name;
                $r_tab[] = $tab;                
            }
            echo json_encode($r_tab);
        }

Thanks a lot :)

Upvotes: 0

Views: 2904

Answers (2)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

You have to add columns option to your datatable code for the remote data to display properly.

$(document).ready(function(){
$('#myTable').DataTable( {
        "processing": true,
        "serverSide": false,
        "order":[],
        "ajax": {
            "url": '<?php echo site_url('main/getData'); ?>',
            "type": "POST",
             "data": { '<?php echo $csrf_token_name; ?>' : '<?php echo $csrf_token_hash; ?>' }
        },
            'columns': [
      { "data": "name", "name": "name"},
      { "data": "last_name", "name": "last_name"},
    ]
    });
});

In your controller method, your JSON response is structured incorrectly. You need to add your records in 'data' element of your output array.

Updated controller method:

public function getData()
    {
     $data['csrf_token_name'] = $this->security->get_csrf_token_name(); // $data has to be passed as second parameter of $this->load->view(..., ....), 
     $data['csrf_token_hash'] = $this->security->get_csrf_hash();

        $table = $this->main_model->data();
        if (count($table) > 0) 
        {
            foreach ($table as $row) 
            {
                $tab = array();
                $tab["name"] = $row->name;
                $tab["last_name"] = $row->last_name;
                $r_tab[] = $tab;                
            }

            $output = array(
                "data" =>  $r_tab
            );

            echo json_encode($output);
        }

Upvotes: 1

Vijay Makwana
Vijay Makwana

Reputation: 921

HTML

<table id="myTable" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Last_name</th>
        </tr>
    </thead>
</table>

JQuery

 $('#user_table').DataTable( {

        "ajax": {
            "url": '<?php echo site_url('main/getData'); ?>',
            "type": "POST",
            "data": { '<?php echo $csrf_token_name; ?>' : '<?php echo $csrf_token_hash; ?>' }
        },
    } );

Controller

public function getData()
{
    $this->security->get_csrf_token_name();
    $this->security->get_csrf_hash();
    $this->datatables->select('*')
         ->from('table_name')

    echo $this->datatables->generate();
}

Upvotes: 0

Related Questions