我想在表中填充数据表值和数据库值。下面是我的代码清单,一切似乎都没问题但是返回了错误" TypeError:aData未定义"所以没有创建数据表
<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"}
]
答案 0 :(得分:1)
当我这样做时,我不得不定义列
发送示例数组;
Array
(
[0] => stdClass Object
(
[id] => 6
[username] => "Lab23"
)
[1] => stdClass Object
(
[id] => 11
[username] => "MaryMM"
)
)
因此,如果您的json编码数组的键值为 id 且用户名,则数据表中将包含以下段。
$(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"}
]
});
});