locked
Getting error DataTables warning: table id=example- Cannot reinitialise DataTable. RRS feed

  • Question

  • User-1634604574 posted

    i have this error when i search by id

     function ToDatatable() {
    
            var Arr = [];
            var searches = "";
            $.ajax({
                type: "GET",
                url: "/Healthcare_Physician/getColumns",
                data: {
                    id: $("#id").val(),
                    first_name: $("#first_name").val(),
                    designation: $("#designation").val(),
                    department: $("#department").val(),
    
    
                    type: "GET",
    
                },
                success: function (response) {
                    searches += "<tr>";
                    data = $.map(response, function (item, a) {
                        Arr.push({ data: item.variable, title: item.variable });
                        //   searches += "<td><input style='width:100%' type='text' id=" + item.variable + " class='form-control' placeholder='Search " + item.variable + "'/></td>";
                    });
                    //  searches += "</tr>";
                },
                dataType: "json"
            }).done(function () {
                $('#example').append(searches);
                
                var sample = $('#example').DataTable({
                    "language": {
                        "zeroRecords": "Nothing found - sorry",
                        "emptyTable": "No data Available.",
                        "loadingRecords": "Loading...",
                        "processing": "Processing...",
                        "lengthMenu": "Display _MENU_ records per page",
                        // "info": "Showing page _PAGE_ of _PAGES_",
                        "infoFiltered": "(filtered from _MAX_ total records)",
                        // "sDom": 'Rfrtlip',
                        // stateSave: true,
                        //"bDestroy": true,
                        "retrieve": true
                    },
                     
                    "dom": '<"top"f>rt<"bottom"ilp><"clear">',
                    "ajax": {
                        "processing": true,
                        "serverSide": true,
                        "url": "/Healthcare_Physician/ConvertDataTabletoString",
                        "data": {
                            "id": $("#id").val(),
                            "first_name": $("#first_name").val(),
                            "designation": $("#designation").val(),
                            "department": $("#department").val(),
    
    
                            type: "GET",
    
                        },
                        "dataSrc": "",
                        "deferRender": true,
    
    
                    },
                    "columns": Arr,
                    "sortable": true,
                    "bFilter": true,
                    "scrollX": true,
                    "searching": false,
                    "bInfo": false,
                    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    "columnDefs": [
                        @* {
                    "render": function (data, type, row) {
    
                        switch (data) {
                            case 'Submitted': return '<img src="@Url.Content("~/img/blue.png")" style="width:8px;height:8px">&nbsp;&nbsp' + data + ''; break;
                            case 'Draft': return '<img src="@Url.Content("~/img/red.png")" style="width:8px;height:8px">&nbsp;&nbsp' + data + ''; break;
                            case 'Cancelled': return '<img src="@Url.Content("~/img/red.png")" style="width:8px;height:8px">&nbsp;&nbsp' + data + ''; break;
    
                            default: return '';
                        }
                    },
                    "targets": 1,
                },*@
                { "visible": false, "targets": [5, 6] },
    
                {
                    "render": function (data, type, row) {
    
                        return '' + data + '';
                    },
                    "targets": 1,
                         },
    
                    ]
            });
    
            var row;
            for (var i = 0; i < Arr.length; i++) {
    
                row += "<option>" + Arr[i].title + "</option>";
            }
            $("#list1").empty();
            $("#list1").append(row);
        });
    
        //$("#example").off("click", "tr").on("click", "tr", function () {
    
        //    var series = $(this).find("td:eq(1)").text();
        //    window.open("edit_Terms_and_Conditions_Template?series=" + series + "", "_self");
    
        //});
    
    
        var rowlist = [];
        $("#example").off("click", "tr").on("click", "tbody tr", function () {
            // alert($(this).index());
            var series = $(this).find("td:eq(0)").text();
    
            window.open("edit_physician?series=" + series + "", "_self");
    
        });
    
        }
    
    
    
      $("#id").on("keydown", function () {
            setTimeout(function () {
    
                if ($.fn.dataTable.isDataTable('#example')) {
                $('#example').DataTable().destroy();
                $('#example').empty();
                ToDatatable();
    
            }
            else {
                ToDatatable();
    
            }
    
                }, 1000);
        });

    Saturday, February 1, 2020 11:44 AM

All replies

  • User409696431 posted

    Read this: https://datatables.net/manual/tech-notes/3

    "DataTables does not allow initialisation options to be altered at any time other than at initialisation time."  Review your code logic and find where you are not destroying and recreating the table before you change initialization options..

    Monday, February 3, 2020 12:31 AM