Datatables clear tbody

前端 未结 4 949
一整个雨季
一整个雨季 2021-02-04 04:45

I have a HTML table that I fill with data from an AJAX jQuery call.This table uses the jQuery plugin - datatables for paging, sorting and so on.

The jQuery call gets cal

4条回答
  •  借酒劲吻你
    2021-02-04 05:12

    you can use DataTables function fnClearTable and fnAddData like this

    $("#Dropdownlist").on("change", function () {
        $.ajax({
                type: "POST",
                url: "@Url.Action("Action", "Controller")",
                //contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var oTable = $('#table').dataTable();//get the DataTable
                    oTable.fnClearTable();//clear the DataTable
                    $.each(data, function (key, item) {
                        oTable.fnAddData(["appending ","data","here"]);//add new row
                        //each element in the array is a td
                    });
                    /*no need to destroy and create new DataTable
                    $('#table').dataTable().fnDestroy();
                    $('#table').dataTable({
                        "aoColumns": [
                          { "bSortable": false },
                          null, null, null, null, null
                        ]
                    });
                    */
                }
            })
    });
    

提交回复
热议问题