Reload table with the same properties and with the updated json data

Reload table with the same properties and with the updated json data

whatacarwhatacar Posts: 7Questions: 3Answers: 1

I am newbie here and I am reloading the table using destroy() method...I know, it's a bad approach. Can you, please, recommend me the appropriate method (approach) of doing this. Your answer or any hint on this would be highly appreciated. Thank you in advance in any case.

$(document).ready(function () {
            $('#sbmt').on('click', function () {
                var table
                var content = { "s": $("#s").val(), "a": $("#s").val() }
                if (!$.fn.dataTable.isDataTable('#example')) {
                    table = $('#example').DataTable({
                        "ajax": {
                            "url": 'json_out.asp',                            
                            "type": 'POST',
                            "data": content,
                            "dataSrc": "",
                            "deferRender": true
                        },
                        "columns": [
                            { "data": "product_id" },
                            { "data": "material_num" }                            
                        ]
                    });                   
                }
                else {                    
                    table = $('#example').DataTable({
                        destroy: true,
                        "ajax": {
                            "url": 'json_out.asp',                           
                            "type": 'POST',
                            "data": content,
                            "dataSrc": "",
                            "deferRender": true
                            
                        },
                        "columns": [
                            { "data": "product_id" },
                            { "data": "material_num" }                            
                        ]                         
                    });
            }
        } ); 
        } ); 

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓
  • whatacarwhatacar Posts: 7Questions: 3Answers: 1

    thank you bindrid for your for your support :-). Here is the working code using ajax.reload().

    $(document).ready(function () {       
            var table
            $('#sbmt').click(function () {            
                if (!$.fn.dataTable.isDataTable('#example')) {      
                    table = $('#example').DataTable({                   
                "ajax": {
                    "url": 'json_out.asp',
                    "type": 'POST',
                    "data": function (d) {d.s = document.getElementById("s").value, d.a = document.getElementById("datepicker").value;},
                    "dataSrc": "",
                    "deferRender": true
                },
                       "columns": [
                    { "data": "product_id" },
                    { "data": "material_num", render: $.fn.dataTable.render.number(',', '.', 0, '$') },
                    { "data": "material_description" },
                    { "data": "promo_date" }
                ]
            })
            $('#example').show();      
                }
                else {
                    table.ajax.reload();                        
            }
           } );
        }); 
    
This discussion has been closed.