dert detg
dert detg

Reputation: 303

DataTable - Cannot read property 'length' of null

I have datatable:

function drawRadnici() {

                $('#tableradnici').dataTable({ 

                    "ajax": {
               "url": 'track_radnici.php',
               "type": 'POST',
               "data": {ajdi:ajdi},
               },
                    paging: false,
                    //"dom":' <"search"f><"top"l>rt<"bottom"ip><"clear">',
                    // end ,
                    "columns": [ {
                            "data": "datum"}, {
                            "data": "radnik"},{
                            "data": "radnih_sati"},{
                            "data": "cena"},{
                            "data": "ukupno"},{
                            "data": "ID"}
                    ],

                    "columnDefs": [

                            {
                        "targets": 5,
                        "data": "ID",
                        "render": function(data, type, full, meta) {
                            // return data; 
                            return '<i value="'+data+'" class="fa fa-times"></i>';
                        }
                            }


    ]
                });

            };

and work fine, but when I have empty JSON data from database then I get:

Uncaught TypeError: Cannot read property 'length' of null

MY json when I havent data is:

{data:null}

WHat I was try to do - I try to add success function to ajax:

"ajax": {
               "url": 'track_radnici.php',
               "type": 'POST',
               "data": {ajdi:ajdi},
"success": function (data) {
if (data == null) {
$('#tabeleradnici').html('No result');
}
}
               },

so I try when I dont have data to add just text to #tabeleradnici ... but this also dont work ...

Upvotes: 0

Views: 6002

Answers (1)

Simon Staton
Simon Staton

Reputation: 4505

You can check if its null and make it empty so .length is still valid:

"success": function (data) {
    data = data || [];
}

Update:

function drawRadnici() {

    $('#tableradnici').dataTable({

        "ajax": {
            "url": 'track_radnici.php',
            "type": 'POST',
            "dataSrc": function (json) {
                if(!json.data){
            $('#tabeleradnici').html('No result');
            json.data = [];
        }
                return json.data;
            },     
            "data": {
                ajdi: ajdi
            }
        },
        paging: false,
        //"dom":' <"search"f><"top"l>rt<"bottom"ip><"clear">',
        // end ,
        "columns": [{
            "data": "datum"
        }, {
            "data": "radnik"
        }, {
            "data": "radnih_sati"
        }, {
            "data": "cena"
        }, {
            "data": "ukupno"
        }, {
            "data": "ID"
        }],

        "columnDefs": [

            {
                "targets": 5,
                "data": "ID",
                "render": function(data, type, full, meta) {
                    // return data; 
                    return '<i value="' + data + '" class="fa fa-times"></i>';
                }
            }


        ]
    });

};

Upvotes: 2

Related Questions