Php 点燃数据表-在屏幕上显示记录
我用表格的方式在我的视图中显示帖子。在过去的4天里,我一直在努力使这项工作正常进行,但我不明白我做错了什么 这是获取数据的功能Php 点燃数据表-在屏幕上显示记录,php,jquery,codeigniter,datatable,Php,Jquery,Codeigniter,Datatable,我用表格的方式在我的视图中显示帖子。在过去的4天里,我一直在努力使这项工作正常进行,但我不明白我做错了什么 这是获取数据的功能 // the Post_model /** * Get page data on datatables */ public function get_datatable() { $this->load->library('datatables'); $this->datatables->select('id, title, s
// the Post_model
/**
* Get page data on datatables
*/
public function get_datatable() {
$this->load->library('datatables');
$this->datatables->select('id, title, slug, sort_description, status');
$this->datatables->from('posts');
return $this->datatables->generate();
}
// the posts controller
/**
* List all posts
*/
public function index() {
$this->data['datatables'] = true;
$this->data['data_url'] = 'admin/posts/data_ajax';
// $this->data['posts'] = $this->post_model->get_datatable();
// dump($this->data['pages']);
// load view
$this->load->view('admin/posts/index', $this->data);
}
public function data_ajax() {
$this->output->enable_profiler(false);
echo $this->post_model->get_datatable();
// echo json_encode($this->post_model->get_datatable());
exit();
}
// the view
<table class="table dataTable table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Url Slug</th>
<th>Sort Description</th>
<th>Status</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Title</th>
<th>Url Slug</th>
<th>Sort Description</th>
<th>Status</th>
</tr>
</tfoot>
<!-- <tbody>
<tr>
<td colspan="5" class="dataTables_empty"></td>
</tr>
</tbody> -->
</table>
<?php if(isset($datatables)): ?>
<?php echo js_tag('js/dataTables/jquery.dataTables.min.js'); ?>
<?php echo js_tag('js/dataTables/dataTables.bootstrap.min.js'); ?>
<script type="text/javascript">
$(document).ready(function() {
$('.dataTable').DataTable({
'bProcessing' : true,
'bServerSide' : true,
'sAjaxSource' : '<?php echo base_url($data_url); ?>',
'sServerMethod' : 'POST',
'fnServerData' : function (sSource, aoData, fnCallback) {
$.ajax({
dataType : 'json',
type : 'post',
url : sSource,
data : aoData,
success : fnCallback,
"columns": [
{ "data": "id" },
{ "data": "title" },
{ "data": "slug" },
{ "data": "sort_description" },
{ "data": "status" }
]
});
}
});
});
</script>
<?php endif; ?>
但问题是我不能在屏幕上显示它们。此外,我还在一个警报框中收到此警告
DataTables警告:table id=DataTables\u table\u 0-为第0行请求的未知参数“0”。有关此错误的详细信息,请参阅http://datatables.net/tn/4
这是我得到的截图
我怎样才能使它正常工作?任何帮助都将不胜感激请尝试此功能
添加Columns参数以将数据与列关联
代码是这样的
$('.dataTable').DataTable({
'bProcessing' : true,
'bServerSide' : true,
'sAjaxSource' : '<?php echo base_url($data_url); ?>',
'sServerMethod' : 'POST',
"columns": [
{ "data": "id" },
{ "data": "title" },
{ "data": "slug" },
{ "data": "date_published" },
{ "data": "status" }
]
'fnServerData' : function (sSource, aoData, fnCallback) {
$.ajax({
dataType : 'json',
type : 'post',
url : sSource,
data : aoData,
success : fnCallback,
});
}
});
$('.dataTable').dataTable({
“bProcessing”:true,
“bServerSide”:true,
“sAjaxSource”:“,
“sServerMethod”:“POST”,
“栏目”:[
{“数据”:“id”},
{“数据”:“标题”},
{“数据”:“slug”},
{“数据”:“发布日期”},
{“数据”:“状态”}
]
“fnServerData”:函数(sSource、aoData、fnServerData回调){
$.ajax({
数据类型:“json”,
键入:“post”,
网址:sSource,
数据:aoData,
成功:,
});
}
});
请参阅,以了解详细信息我也有类似问题。我就是这样解决的: 您必须将列设置为
"columns": [null, null, null, null, null,{}]
不要在Ajax中单独设置。检查来自查询和html表的查询响应中的列数恐怕我不理解您的意思。你能更详细地解释一下吗?你的json响应有5列,html表有6列,这使它们能够正确显示视图。我做到了,但仍然得到相同的结果,无法显示记录。请检查此$('.dataTable')。dataTable()您所说的
是什么意思请参考[datatables column][1],有关详细信息,请参见?这是指向datatables columns参数的链接,存在一些编辑问题,现已更正。希望能有帮助,还是没有运气。。我仍然收到警告,也无法显示记录。。我将更新代码以进行查看,并为您提供一个屏幕截图,以在datatables中而不是在ajax中使用columns选项。代码已编辑。请检查。另外一件事:我如何使collumns动态?
"columns": [null, null, null, null, null,{}]