首页 > 解决方案 > 检查 yajra/laravel 表是否已经初始化的正确方法?

问题描述

在使用 laravel 5.7 / jquery 3 / Blade 应用程序时,我使用 yajra/laravel-datatables-oracle 8 并在模态对话框中打开它,我第二次/第三次打开此对话框时收到警告消息:

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

在刀片文件中:

<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');

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

谢谢!

标签: javascriptlaravel-5yajra-datatable

解决方案


你可以用$.fn.dataTable.isDataTable()这个。

从文档:

此方法提供了检查表节点是否已经是 DataTable 的能力。这对于确保您不会重新初始化已经是 DataTable 的表很有用。

请注意,这是一个静态函数,通过 $.fn.dataTable 对象访问,而不是 API 实例。它可以随时访问,甚至在页面上创建任何数据表之前。

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..."
        },
    ...
}

推荐阅读