检查 yajra/laravel table 是否已经初始化的正确方法?

Correct way to check if yajra/laravel table was already initialized?

正在使用 laravel 5.7 / jquery 3 / blade app 我使用 yajra/laravel-datatables-oracle 8 并在模态对话框中打开它,我第二次/第三次打开这个对话框 我收到警告消息:

DataTables warning: table id=get-fee-dt-listing-table - Cannot reinitialise DataTable

在 blade 文件中:

<div class="modal-body">

    <div class="table-responsive dataTables_header">
        <table class="table table-bordered table-striped text-primary" id="get-fee-dt-listing-table">
            <thead>
            <tr>
                <th></th>

在 .js 文件中:

bookingsAndAvailability.prototype.showFeesSelection = function () {
    console.log('showFeesSelection::')
    $("#div_check_in_fees_modal").modal({
        "backdrop": "static",
        "keyboard": true,
        "show": true
    });



    // var dtListingTable= document.getElementById('get-fee-dt-listing-table')
    // dtListingTable  = null // If to uncomment these 2 lines - they do not help

    $("#get-fee-dt-listing-table").html('') // // If to uncomment these 2 lines - they do not help

    oTable = $('#get-fee-dt-listing-table').DataTable({
        processing: true,
        language: {
            "processing": "Loading fees..."
        },

我用命令关闭模态:

$("#div_check_in_fees_modal").modal('hide');

检查 table 是否已经初始化的正确方法是什么?

谢谢!

您可以为此使用 $.fn.dataTable.isDataTable()

来自文档:

This method provides the ability to check if a table node is already a DataTable or not. This can be useful to ensure that you don't re-initialise a table that is already a DataTable.

Please note that this is a static function and is accessed through the $.fn.dataTable object, not an API instance. It can be accessed at any time, even before any DataTables have been created on the page.

if ( ! $.fn.DataTable.isDataTable( '#example' ) ) {
  $('#example').dataTable();
}

在你的情况下它会是这样的:

if (! $.fn.DataTable.isDataTable('#get-fee-dt-listing-table')) {
    oTable = $('#get-fee-dt-listing-table').DataTable({
        processing: true,
        language: {
            "processing": "Loading fees..."
        },
    ...
}