CodeIgniter与DataTables ajax填充 [英] CodeIgniter with DataTables ajax populate
问题描述
由于我有超过20k〜的项目显示在 DataTable
中,我想根据一些参数填充它,以避免大的滞后。
Since I have more than 20k~ items to show in the DataTable
I want to populate it according to some parameters to avoid big lag.
// Clears the DataTable to avoid duplication of items
$('#contacts-table > tbody').empty();
var category_id = 5; // Just for tests
$.post('ajax_getAll', {category_id: category_id}, function(response){
// I retrieve the 'response' as json_encoded
var json = JSON.parse(response);
});
如果我没有使用 DataTables
我要做的传统方式(填充 tbody
):
If I were not using DataTables
, in the traditional way I would just do (to populate the tbody
):
$.each(json, function(index, item){
var tr = "<tr>";
tr += " <td><input type='checkbox' class='checkbox'/></td>";
tr += " <td>" + item.id + "</td>";
tr += " <td>" + item.name + "</td>";
tr += "</tr>";
$('#contacts-table').prepend(tr);
});
但是,使用这段代码,虽然项目被检索并成功插入到DataTable中, DataTable的功能(如重新排序,搜索等)停止工作,当我使用这些功能时,它会自动删除我所有的表 tbody
。
But, with this piece of code, although the items are retrieved and inserted successfully into the DataTable, the functions of DataTable (like re-order, search etc) stop working and when I use those functions it automatically deletes all my table tbody
.
所以我搜索了一点,我发现我应该使用数据属性
填充DataTable。
So I've searched a little and I found that I should populate the DataTable using the Data attribute
.
// Since I've initialized the DataTable in the load of the jQuery
// I must destroy it so I can re-load it again
$('#contacts-table').DataTable().destroy();
$('#contacts-table').DataTable({
'bPaginate': true,
'bLengthChange': false,
'bFilter': true,
'bInfo': true,
'bAutoWidth': true,
"iDisplayLength": 30,
'aoColumnDefs': [{
'bSortable': false,
'aTargets': ['nosorting']
}],
"aaSorting": [],
'data': json, // Here I'm trying to pass the values without any success
});
使用最后一个代码,我收到此错误警告:请求未知
With this last code, I receive this error Warning: Request unknown
我从PHP收到的json如下: / p>
The json I receive from the PHP is the following:
[{"id":"16","name":"just testing"}, {"id":"16","name":"stackoverflow"}]
除了我收到的错误,我想知道如何我设置了这个 tr + =< td>< input type ='checkbox'class ='checkbox'/>< / td>;
DataTables
by ajax
。
Besides the error I'm receiving I wonder how to I set up this tr += " <td><input type='checkbox' class='checkbox'/></td>";
using the DataTables
by ajax
.
推荐答案
我刚刚解决了,虽然我现在有其他问题。
I've just solved it although I have now other problems.
基本上,我要做的是除了dataTable的设置添加一些更多参数,以便从PHP中检索信息作为json响应。
Basically, what I have to do is, besides the settings of dataTable add some more parameters in order to retrieve info from the PHP as json response.
$("#contacts-table").dataTable({
// All the properties you want...then in the end
'serverSide': true,
'ajax': {
'url': 'ajax_getAll',
'type': 'POST',
},
'columns': [
{ 'data': 'id' },
{ 'data': 'name' },
]
});
PHP json响应必须是这样的:
And the PHP json response must be something like this:
$response = array( 'sEcho' => 5,
'iTotalRecords' => 5,
'iTotalDisplayRecords' => 5,
'aaData' => array(array('id' => 1, 'name' => 'stackoverflow'),
array('id' => 2, 'name' => 'google')),
);
这篇关于CodeIgniter与DataTables ajax填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!