How to reload table on each button click

How to reload table on each button click

laputatclaputatc Posts: 4Questions: 4Answers: 0

Hi All :smile:

I have success to run datatable from server side as below code

$(document).ready(function () {
        $("#TableCurrentList").hide();
        $('#cmdList').on('click', function () {
            $("#TableCurrentList").hide();
            var url = '@Url.Action("GetElecticList1", "Home")';
            $("#TableCurrentList").show();           
            $("#TableCurrentList").DataTable({
                
                "processing": true, // for show progress bar
                "serverSide": true, // for process server side
                "filter": true, // this is for disable filter (search box)
                "orderMulti": false, // for disable multiple column at once
                "pageLength":13,

                "ajax": {
                    "url": url,
                    "type": "POST",
                    "datatype": "json"
                },
                "columnDefs":
                    [{
                        "targets": [0],
                        "visible": false,
                        "searchable": false
                    },
                        {
                            targets: 1,
                            render: function (data) { return moment(data).format('YYYY/MM/DD HH:mm:ss'); }
                        }
                        
                    ],

                "columns": [
                    { "data": "id", "name": "id", "autoWidth": true },
                    { "data": "RecDateTime", "name": "RecordTime", "autoWidth": true },
                    { "data": "fBrenchMark", "name": "BrenchMark", "autoWidth": true },
                ]
            });
        });

    });

But I click again, I go the initialize error , it seem serverside datatable need to do clear / reload on every click, and I have try but not success, can anyone help ?

Thank you

Mike

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You could try adding destroy to the initialisation arguments.

    Colin

  • tornadofaytornadofay Posts: 4Questions: 1Answers: 0
    edited December 2019
    var tbl = null;
    $('#cmdList').on('click', function () {
         .... // some code here
         if(tbl === null){
               tbl = ("#TableCurrentList").DataTable({ 
                           ..... //datatable code here
                        });
          }
          else{
               tbl.ajax.reload()
          }
    });// cmdList click end
    
This discussion has been closed.