首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataTables dataTables_wrapper没有加载

DataTables dataTables_wrapper没有加载
EN

Stack Overflow用户
提问于 2020-09-28 14:10:48
回答 2查看 1.5K关注 0票数 4

在我的WordPress v5.5.1中,我有一个定制的post类型存档页面来显示所有的帖子。对于这个特性,我们希望使用DataTable。已按以下方式将DataTable文件排队:

代码语言:javascript
复制
wp_deregister_script('jquery'); // deregistered default jQuery
wp_enqueue_script('jq', 'https://code.jquery.com/jquery-3.5.1.min.js', array(), null, true); // for Bootstrap
// DATATABLES
wp_enqueue_script('js', 'https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js', array('jq'), null, true);
wp_enqueue_script('wp-js', get_template_directory_uri() . '/js/scripts.js', array('jq'), null, true);
wp_enqueue_style('css', 'https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css');

在正在加载的scripts.js文件中,我启用了DataTables,如下所示:

代码语言:javascript
复制
$(document).ready(function () {
    $('#songs').DataTable();
});

下面是HTML表:

代码语言:javascript
复制
<table id="songs" class="table dataTable">
    <thead>
        <tr>
            <th>Songs</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Song 1</td>
        </tr>
        <tr>
            <td>Song 2</td>
        </tr>
        <tr>
            <td>Song 3</td>
        </tr>
        <tr>
            <td>Song 4</td>
        </tr>
    <tbody>
</table>

DataTable css文件和js文件正在加载并应用样式。

然而,dataTables_wrapper 并没有加载,在这里我们可以对表内容、分页和搜索栏进行排序。我只看到普通的HTML表。

我在JSFiddle中运行了这段代码并运行良好(https://jsfiddle.net/0burvh1y/)。

我尝试过禁用所有插件并使用默认的WordPress jquery,但是没有运气。

EN

回答 2

Stack Overflow用户

发布于 2020-10-08 08:20:40

我使用了下面的dataTable CDN URL,DataTables dataTables_wrapper现在正在加载:

代码语言:javascript
复制
wp_enqueue_script('js', 'https://cdn.datatables.net/v/bs4/dt-1.10.22/datatables.min.js', array('jq'), null, true);
wp_enqueue_style('css', 'https://cdn.datatables.net/v/bs4/dt-1.10.22/datatables.min.css');
票数 3
EN

Stack Overflow用户

发布于 2020-10-08 10:15:59

就我个人而言,我会创建函数并将它们与以下操作联系起来:

代码语言:javascript
复制
function add_datatables_scripts() {
  wp_register_script('datatables', 'https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js', array('jquery'), true);
  wp_enqueue_script('datatables');

  wp_register_script('datatables_bootstrap', 'https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js', array('jquery'), true);
  wp_enqueue_script('datatables_bootstrap');
}
 
function add_datatables_style() {
  wp_register_style('bootstrap_style', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
  wp_enqueue_style('bootstrap_style');

  wp_register_style('datatables_style', 'https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css');
  wp_enqueue_style('datatables_style');
}

add_action('wp_enqueue_scripts', 'add_datatables_scripts');
add_action('wp_enqueue_scripts', 'add_datatables_style');
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64103884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档