简体   繁体   English

使用DataTables Ajax填充CodeIgniter

[英]CodeIgniter with DataTables ajax populate

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. 由于我要在DataTable显示超过20k〜个项目,因此我想根据一些参数填充它,以免出现较大的滞后。

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

});

If I were not using DataTables , in the traditional way I would just do (to populate the tbody ): 如果我不使用DataTables ,那么按照传统方式,我会做(填充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);
});

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中,但是DataTable的功能(如重新排序,搜索等)停止工作,当我使用这些功能时,它会自动删除我的所有表tbody

So I've searched a little and I found that I should populate the DataTable using the Data attribute . 因此,我进行了一些搜索,发现应该使用Data attribute填充DataTable。

// 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 使用此最后的代码,我收到此错误警告:请求未知

The json I receive from the PHP is the following: 我从PHP收到的json是以下内容:

[{"id":"16","name":"just testing"}, {"id":"16","name":"stackoverflow"}]

Besides the error I'm receiving I wonder how to I set up this tr += " <td><input type='checkbox' class='checkbox'/></td>"; 除了我收到的错误外,我想知道如何设置此tr += " <td><input type='checkbox' class='checkbox'/></td>"; using the DataTables by ajax . 通过ajax使用DataTables

I've just solved it although I have now other problems. 尽管我现在还有其他问题,但我已经解决了。

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. 基本上,我要做的是,除了dataTable的设置之外,还添加了一些其他参数,以便从PHP作为json响应检索信息。

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

And the PHP json response must be something like this: 而且PHP json响应必须是这样的:

$response = array( 'sEcho' => 5,
                   'iTotalRecords' => 5,
                   'iTotalDisplayRecords' => 5,
                   'aaData' => array(array('id' => 1, 'name' => 'stackoverflow'),
                                     array('id' => 2, 'name' => 'google')),
                 ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM