I work on razor page asp.net core . I face error Datable id=example can't reinitialize data table ?
so How to solve this error please ?
error happen when change select option printer server
I need when change select on select option printer server then hide or remove table with pagination option first ,next,previous,last etc
JavaScript
$(document).ready(function () {
$('#PrintServer-select').change(function () {
var printserverId = $(this).val();
if (printserverId) {
console.log(printserverId);
$.ajax({
url: '',
type: "GET",
dataType: "json",
data: { printserverId: printserverId },
success: function (data) {
console.log(data);
$('#branch-select').empty();
$.each(data, function (i, item) {
//console.log(item);
$('#branch-select').append($('', {
value: item.branchId,
text: item.vBranchDesc
}));
});
var tableShows = $('#example').DataTable({
paging: false
});
// Hide the table using jQuery
$('#example').hide();
$('#example').DataTable();
if ($.fn.dataTable.isDataTable('#example')) {
$('#example').destory();
// The DataTables instance is already initialized
} else {
// Initialize the DataTables plugin
$('#example').DataTable();
}
}
});
}
});
var table = $("#example").dataTable({
"bFilter": false,
"bSort": false,
"paging": true,
"bPaginate": true,
"pagingType": "full_numbers",
"columnDefs": [
{ className: "pad-md-left-p-10 pad-top-bottom-p-10", "targets": [0, 1, 2, 3, 4, 5, 6, 7] }
],
"lengthMenu": [[5,10, 15,20, 25, 35, 50, 100, -1], [5,10, 15,20, 25, 35, 50, 100, "All"]],
"pageLength": 10 // Set the default page length to 10
});
table.destroy();
});error happen on block below
JavaScript
var tableShows = $('#example').DataTable({
paging: false
});
// Hide the table using jQuery
$('#example').hide();
$('#example').DataTable();
if ($.fn.dataTable.isDataTable('#example')) {
$('#example').destory();
// The DataTables instance is already initialized
} else {
// Initialize the DataTables plugin
$('#example').DataTable();
}image for what i need


Amit MohantyRank #112 · 20K reputationPosted Jul 12, 2023, 1:29 AM EDT
Try this
$(document).ready(function () { // Create a variable to store the DataTable instance var table; $('#PrintServer-select').change(function () { var printserverId = $(this).val(); if (printserverId) { console.log(printserverId); $.ajax({ url: '', type: "GET", dataType: "json", data: { printserverId: printserverId }, success: function (data) { console.log(data); $('#branch-select').empty(); $.each(data, function (i, item) { $('#branch-select').append($('', { value: item.branchId, text: item.vBranchDesc })); }); // Destroy the existing DataTable instance if it exists if (table && $.fn.DataTable.isDataTable('#example')) { table.destroy(); } // Hide the table using jQuery $('#example').hide(); // Reinitialize the DataTable table = $('#example').DataTable({ paging: false }); } }); } }); // Initialize the DataTable outside of the change event table = $("#example").DataTable({ "bFilter": false, "bSort": false, "paging": true, "bPaginate": true, "pagingType": "full_numbers", "columnDefs": [ { className: "pad-md-left-p-10 pad-top-bottom-p-10", "targets": [0, 1, 2, 3, 4, 5, 6, 7] } ], "lengthMenu": [[5, 10, 15, 20, 25, 35, 50, 100, -1], [5, 10, 15, 20, 25, 35, 50, 100, "All"]], "pageLength": 10 // Set the default page length to 10 }); // Optional: Destroy the DataTable instance on document unload to prevent memory leaks $(window).on('beforeunload', function () { if (table && $.fn.DataTable.isDataTable('#example')) { table.destroy(); } }); });