0

我想用表中的数据库值填充数据表。下面是我的代码清单,一切似乎都正常,但返回错误“TypeError:aData is undefined”,因此未创建数据表

<script type="text/javascript">
$(document).ready(function() {
    $('#listax').dataTable({
        "bProcessing": true,
        "bLengthChange": true,
        "bFilter": true,
        "bSort": true,
        "bInfo": true,
        "bAutoWidth": true,
        "bServerSide": true,
        "sServerMethod": "POST",
        "sAjaxSource": "<?php echo base_url(); ?>ajax_live/getUsers",
        "aoColumns": [
            null,
            null //put as many null values as your columns
        ]
    });
});
</script>

<table id="listax"></table>

服务器端

 function getUsers(){
     $this->db->select('id,username');
     $query = $this->db->get('user');
     $data = $query->result();
     echo json_encode($data); 
 }

这是json encode返回的数据

[
    {"id":"6","username":"Lab23"},
    {"id":"11","username":"MaryMM"}
]
4

1 回答 1

1

完成此操作后,我必须定义列。
被发回的示例数组;

Array
(
    [0] => stdClass Object
        (
            [id] => 6
            [username] => "Lab23"
        )
    [1] => stdClass Object
        (
            [id] => 11
            [username] => "MaryMM"
        )
)

因此,如果您的 json 编码数组具有idusername的键值,则数据表中将包含以下段。

$(document).ready(function() {
    $('#listax').dataTable({
        "bProcessing": true,
        "bLengthChange": true,
        "bFilter": true,
        "bSort": true,
        "bInfo": true,
        "bAutoWidth": true,
        "bServerSide": true,
        "sServerMethod": "POST",
        "sAjaxSource": "<?php echo base_url(); ?>ajax_live/getUsers",
        "aoColumns":[
            {"mDataProp":"id"},
            {"mDataProp":"username"}
        ]
    });
});
于 2013-04-09T09:49:02.807 回答