Datatable with Ajax data source problem

Datatable with Ajax data source problem

YasmeenYasmeen Posts: 2Questions: 1Answers: 0

I'm working with Datatables Ajax data source option, but it always shows "No data available in table". I'm using Codeigniter framework.

Javascript:

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.8/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.8/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#showTable').DataTable( {
     "sDom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col-sm-6'p>>",
     "sPaginationType": "bootstrap",
     "oLanguage": {
         "sLengthMenu": "_MENU_ records per page",
         "oPaginate": {
             "sPrevious": "Prev",
             "sNext": "Next"
         }
     },
     "aoColumnDefs": [{
         'bSortable': false,
         'aTargets': [0]
     }],
     "ajax": "<?php echo base_url().'publications/get'; ?>",
     "columns": [
         { "data": 'firstColumn' },
         { "data": "secondColumn" },
         { "data": "id"},
         { "data": "title" },
         { "data": "newspaperName" },
         { "data": "adValue" },
         { "data": "PRValue" },
         { "data": "impression" },
         { "data": "date" },
         <?php if ($this->admin_ion_auth->in_group(array('clients'))) { ?>
         { "data": "circulation" },
         { "data": "positiveNegative" }
         <?php } //if clients ?>
         <?php if (!$this->admin_ion_auth->in_group(array('clients'))) { ?>
         { "data": "addingDate" },
         { "data": "addUsername" },
         { "data": "actionButtons" }
         <?php } //if clients ?>
     ],
     "deferRender": true
 });
});
</script>

And AJAX object is like:

{"data":[{"id":"55","newspaperId":"4480","clientId":"1","date":"2015-08-13","pageNumber":"5","height":"5","width":"5","impression":"0","adValue":"0","PRValue":"0","title":"eewd","translation":"","categoryId":"2","productId":"0","articleBrandingId":"1","headlineBrandingId":"2","positiveNegative":"negative","picture":"yes","mentionedIn":"","writerId":"0","addingDate":"2015-08-13","addUserId":"1","lastModifiedDate":null,"modifyUserId":"0","addUsername":"admin","image":"Yes","newspaperName":0,"circulation":"218541","mediaType":"Newspaper","issueFrequency":"Daily","language":"Arabic","group":"A","categoryName":"cat11","productName":"","writerName":"","articleBranding":"Branded","headlineBranding":"Unbranded Headline","firstColumn":0,"secondColumn":0,"actionButtons":0},{"id":"54","newspaperId":"4478","clientId":"1","date":"2015-08-13","pageNumber":"5","height":"5","width":"5","impression":"4000000","adValue":"3250","PRValue":"11375","title":"kkkk","translation":"","categoryId":"2","productId":"0","articleBrandingId":"1","headlineBrandingId":"1","positiveNegative":"positive","picture":"no","mentionedIn":"","writerId":"0","addingDate":"2015-08-13","addUserId":"1","lastModifiedDate":null,"modifyUserId":"0","addUsername":"admin","image":"No","newspaperName":0,"circulation":"1000000","mediaType":"Newspaper","issueFrequency":"Weekly","language":"Arabic","group":"A","categoryName":"cat11","productName":"","writerName":"","articleBranding":"Branded","headlineBranding":"Branded Headline","firstColumn":0,"secondColumn":0,"actionButtons":0}]}

PHP:

public function get() {
    $jsonResponse = array('data' => array());
    $returnedData = $this->publication_model->get(array('clientId' => $this->clientId));
    if(!empty($returnedData)) {           
        foreach ($returnedData as $item) {              
            array_push($jsonResponse['data'], $item);
        }
    }
    echo json_encode($jsonResponse);
}

Also while debugging in firefox, no errors occur, but when checking with Fiddler Web Debugger, the function get() in the controller publications is never called.

Answers

  • YasmeenYasmeen Posts: 2Questions: 1Answers: 0

    Well, it was my fault. I was using Datatables v1.9.4, and when I wanted to use AJAX data source, I switched to v1.10.8 without changing the options. First, I moved this line to page's footer

    <script src="https://cdn.datatables.net/1.10.8/js/jquery.dataTables.min.js"></script>
    

    and changed options to this:

    $('#showTable').DataTable( {
        "language": {
            "lengthMenu": "_MENU_ records per page",
            "infoFiltered": "(filtered from _MAX_ total records)",
            "paginate": {
                "previous": "Prev",
                "next": "Next"
            }
        },
        "columnDefs": [{
            "targets": [ 0 , -1 ], //first and last column only
            "searchable": false
        }],
        "ajax": "<?php echo base_url().'publications/get'; ?>",
        "deferRender": true,
        "columns": [
            { "data": 'firstColumn' },
            { "data": "secondColumn" },
            { "data": "id"},
            { "data": "title" },
            { "data": "newspaperName" },
            { "data": "adValue" },
            { "data": "PRValue" },
            { "data": "impression" },
            { "data": "date" },
            <?php if ($this->admin_ion_auth->in_group(array('clients'))) { ?>
            { "data": "circulation" },
            { "data": "positiveNegative" }
            <?php } //if clients ?>
            <?php if (!$this->admin_ion_auth->in_group(array('clients'))) { ?>
            { "data": "addingDate" },
            { "data": "addUsername" },
            { "data": "actionButtons" }
            <?php } //if clients ?>
        ]
    });
    

    It works now :)

This discussion has been closed.