DataTables 使用小结

本文深入探讨DataTables插件的使用技巧,涵盖引入方法、基本配置、服务器分页及搜索功能,适合希望优化表格展示效果的前端开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DataTables 使用小结

使用过一段时间的DataTables , 非常强大的一个table插件。一直在不停的更新。
官网: https://datatables.net/

引入

一个简单的demo

<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/jquery.dataTables.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.js"></script>
</head>
<body>
 <table id="myTable" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            // ....
            </tbody>
    </table>
    <script>
        $(document).ready(function () {
            $('#myTable').DataTable();
        });
    </script>
</body>

DataTables 提供了各种皮肤,根据需要引入。

基本配置

演示了比较常用的设置

<script>
 $('#myTable').DataTable({
                "language": {
                    "lengthMenu": "显示 _MENU_ 条",
                    "zeroRecords": "没有数据",
                    "paginate": { "first": "第一页", "last": "最后一页", "next": "下一页", "previous": "上一页" }
                },
                'dom': '<"top">rt<"bottom"p><"clear">',  // 布局
                'processing': true, // 显示加载提醒
                'serverSide': true, // 服务器分页
                'paging': true,  // 是否分页
                "ordering": false, // 是否排序
                "info": false, // 显示显示info
                'searching': true,  // 是否使用搜索api
                'searchDelay': 350, // 搜索延迟
                'ajax': { 'url': '/api/news/page', 'type': 'post' }, // 请求路径和方式
                "columns": [ // 绑定列
                    // 不绑定值
                    { "data": null }, 
                    // 绑定值,并自定义渲染方式
                    {
                        "data": "image",
                        render: (data, type, full) => {
                            return '<img src="' + getImageUrl(data) + '" width=150 />';
                        }
                    },
                    { "data": "title" },
                    { "data": "intro" },
                    {
                        "data": "status",
                        render: (data, type, full) => {
                            return publishStatus(data);
                        }
                    },
                    {
                        "data": "createDate",
                        render: (data, type, full) => {
                            return dateFormat(data)
                        }
                    },
                    {
                        "data": "publishDate",
                        render: (data, type, full) => {
                            return dateFormat(data)
                        }
                    },
                    {
                        "data": "updateDate",
                        render: (data, type, full) => {
                            return dateFormat(data)
                        }
                    },
                    // 最后一列 操作列-绑定到id,方便操作
                    {
                        "data": "id",
                        render: (data, type, full) => {
                            return createButton(data);
                        }
                    },
                ],
                // 请求后的回调函数
                "fnDrawCallback": function (oSettings) {
                    // 获得 response
                    var json = $.parseJSON(oSettings.jqXHR.responseText);
	                // 设置序号
                    this.api().column(0).nodes().each(function (cell, i) {
                        cell.innerHTML = i + 1;
                    });
                }
            });
            function dateFormat(date) {
                if (!$.isEmptyObject(date) || date == '' || date == null) {
                    return '空';
                }
                return moment(date).format('YYYY-MM-DD HH:mm');
            }
            function createButton(id) {
                return `<div>
                      <button type="button" class="btn btn-success btn-sm" onclick="publishById(` + id + `)">发布</button>
                      <button type="button" class="btn btn-info btn-sm" onclick="editById(` + id + `)">编辑</button>
                      <button type="button" class="btn btn-danger btn-sm" onclick="deleteById(` + id + `)">删除</button>
               </div>`
            };
</script>

服务器分页

前端传递的主要数据 : draw ,start, length

  • draw 第几次请求
  • start 从哪里开始,注意这里不是页码
  • length 一页长度

页码要自己计算

服务器返回的json格式

{
    "draw": 1,
    "recordsTotal": 3, 
    "recordsFiltered": 3,
    "data": [
        {
            "id": 1,
            "title": "xxxxxxxx",
            "content": "yyyyyyy"
        }
        // ....
    ]
}

搜索

datatables的搜索很强大,但是经常需要自定义搜索。
使用搜索API必须设置searching ,而不需要datatables的搜索过滤器,可以通过设置dom将其隐藏。再手动调用搜索API

总结

功能非常强大,官网上有很详细的文档,也有提供非常强大的插件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值