CodeIgniter with DataTables ajax populate

假装没事ソ 提交于 2019-12-25 01:39:12

问题


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

});

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

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.

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

The json I receive from the PHP is the following:

[{"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>"; using the DataTables by ajax.


回答1:


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.

$("#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:

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


来源:https://stackoverflow.com/questions/26387356/codeigniter-with-datatables-ajax-populate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!