0

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 ...

1
  • You could be use fnDrawCallback for this Commented Feb 6, 2015 at 14:29

1 Answer 1

2

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


        ]
    });

};
Sign up to request clarification or add additional context in comments.

16 Comments

dont work , so succes is just an idea, how I can solve this in other ways ?
I need to destoy #tabelaradnici if there is no data and just to show text 'NO data' ...
Can you add it into the render function? Or where your trying to get its length?
I post my full code with html, js ... so this is all ... what about render function ?
how I get : Uncaught ReferenceError: json is not defined
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.