javascript - 检查 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');
检查表是否已经初始化的正确方法是什么?
谢谢!
解决方案
你可以用$.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..."
},
...
}
推荐阅读
- azure-cosmosdb - CosmosDB 每项 ttl 设置抛出异常:无法反序列化当前 JSON 对象
- python - Ubuntu 16.04 - 启动时崩溃 spyder4 - 无法导入名称“NUMERIC_NUMPY_TYPES”
- python - KFold 函数 TypeError:此 dtype 不允许缩减操作“argmax”
- html - 做s 将使用上下文传递给子元素
- c# - 上传到 Web 应用程序时,如何将上传的文件(例如 Excel、Word、PDF)从 Apple 格式转换为 Windows?
- github-actions - 更新主分支时,功能分支上的 Github Action 触发作业
- javascript - Firestore 文档上传后无法路由到仪表板
- c# - iTextSharp - 打印时 PDF 分辨率变为奇怪的大小
- vba - MS Access VBA:Word 到 PDF 转换 - 另存为弹出窗口
- angular - 如何调整角度完整日历中的双图标?