Datables warning: table id: {id} -1

Datables warning: table id: {id} -1

marywellmarywell Posts: 9Questions: 2Answers: 0

Hello devs, I use datatables in almost all my projects where I need to manage a table and never had this issue:

In my current project with datatables I have this alert all the time when the ajax call returns an empty array: this is my js call:

dataTableName = '#datatable-account_index';
  table = $(dataTableName).DataTable({
    "paging": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "info": true,
    "autoWidth": false,
    "responsive": true,
    "ajax":{
      "url":"lib/php/tanto.php",
      "type":"POST",
      "data":{"formname":"cards_sent_data"},
      "dataSrc": function(res){
        var count = res.data.length;
        $('#num_cards_sent').html(count);
        return res.data;
      }
    },
    "columns": [
      { data: 'card_sent_id', class: "text-center", orderable: false, render: function(data, type, row, meta) {
          return (type === 'display' && data !== '') ?
            '<a class="launch_edit" data-id="'+data+'" data-toggle="tooltip" title="View"><i class="fas fa-eye text-warning"></i></a>'
          : data;
        }
      },
      { data: 'sent_date' },
      { data: 'rcp_name' },
      { data: 'rcp_email' },
      { data: 'card_name' },
      { data: 'occasion_name', visible: false },
      { data: 'view_count', visible: false },
      { data: 'sent_status_id', visible: false }

    ],
    "order": [[1, "desc"]],
    //"initComplete": ajaxTable,
    "select":false
  });

the response from the ajax call is a json such as: {"error":1,"data":[]}. The initComplete is commented now as it only activates the tooltip and makes the object click action available in the dom. But for now is not required until I remove the alert. If the "data" array has data the table loads fine. Also after closing the alert, the message "No data available in table" is displayed correctly.

Can you help me? I'm trying few days to fix this alert with no success.

Thanks.

This question has an accepted answers - jump to answer

Answers

  • marywellmarywell Posts: 9Questions: 2Answers: 0

    See screenshoot of the alert. The table is being built on a dev site with no internet access.

  • kthorngrenkthorngren Posts: 21,003Questions: 26Answers: 4,889
    Answer ✓

    If {"error":1,"data":[]} is the response from the server then you will need to debug your server script to determine why its returning 1 in the error object and not returning data.

    Kevin

  • marywellmarywell Posts: 9Questions: 2Answers: 0

    Hi, I removed the "error":1 from the json response and the Alert disapeared... Thanks for that!..
    However, the "error":1 is a variable that I want to use on the dataSrc funtion to launch a toaster such as:

    "dataSrc": function(res){
            if(res.data.error){
                // launch toastr
                return res.data; // []
            } else {
                var count = res.data.length;
                $('#num_cards_sent').html(count);
                return res.data;
             }
          }
    

    Anyway I can use the count variable to launch the toastr and remove the "error" variable from the json response.

    Thanks for your quick response...

  • marywellmarywell Posts: 9Questions: 2Answers: 0

    Solved, thanks...

This discussion has been closed.