带有数据表的CodeIgniter上的Ajax格式数据

时间:2018-05-27 21:27:25

标签: php ajax codeigniter datatables

我想从数据库中获取信息,并在CodeIngniter 3上使用Datatables和JQuery的选项卡中显示它。 我认为我的表格是:

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

我的js脚本:

$(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; ?>' }
        },
    });
})

我的控制器方法:

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

非常感谢:)

2 个答案:

答案 0 :(得分:1)

您必须在数据表代码中添加columns选项才能正确显示远程数据。

$(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"},
    ]
    });
});

在您的控制器方法中,您的JSON响应结构不正确。您需要在“数据”中添加记录。输出数组的元素。

更新了控制器方法:

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

答案 1 :(得分:0)

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; ?>' }
        },
    } );

控制器

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

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