【问题标题】:DataTables columns, columnDefs and rowCallback HTML5 initialisationDataTables 列、columnDefs 和 rowCallback HTML5 初始化
【发布时间】:2019-04-12 08:22:13
【问题描述】:

我目前有一个数据表(1.10.18 版),其中加载了几个使用 js 的选项,但我需要使我的代码更具可重用性,并且我正在尝试使用 html5 data-* 属性初始化我的数据表。

<table id="dataTable" cellspacing="0" width="100%" data-source="ajax.mysource.php">
    <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th><i class="fas fa-low-vision"></i></th>
        </tr>
    </thead>
</table>

我的 jQuery 代码如下所示:

var dataTable = $('#dataTable').DataTable({
    processing: true,
    serverSide: true,
    ajax: $('#dataTable').data('source'),
    columns: [
        { 'data': 'name' },
        { 'data': 'address' },
        { 'data': 'visible' }
    ],
    order: [[ 1, 'asc' ], [ 0, 'asc' ]],
    responsive: true,
    nowrap: true,
    pageLength: 15,
    lengthChange: false,
    select: 'single',
    columnDefs: [
        {   targets: 0, width: "110px" },
        {   targets: 1, width: "150px" },
        {   targets: 1, render: $.fn.dataTable.render.ellipsis(80) },
        { targets: 2, render: $.fn.dataTable.render.visibilityIcon() }
    ],
    rowCallback: function(row, data, index) {
        if (data.visible == "0") {
            $(row).addClass("notVisible");
        }
    }
});

对于每个数据表,我都会使用一些共同的选项,但如果我可以使用 html5 data-* 属性直接在我的 html 中设置列​​、columnDefs 和 rowCallBack,那就太好了,这样我就可以使用相同的代码不同的表,例如:

<th data-columns="address" data-column-defs="{targets: 1, width:'150px'}" data-row-callback="function...">Address</th>

除了排序、排序和页面长度之外,我还没有找到如何使用 html5-* 属性。

用 html5 设置这个选项真的可以用 datatables.js 吗?

【问题讨论】:

    标签: javascript jquery datatables datatables-1.10


    【解决方案1】:

    首先,您需要 here 所述的 1.10.5 版

    从 v1.10.5 开始,DataTables 还可以使用从 HTML5 data-* 属性读取的初始化选项

    然后你必须把数据属性放到表格元素而不是列元素。

    <table  id="example"
    data-column-defs='[{"targets": 0, "width": "200px"}]' 
    data-page-length='2'
    data-class="dataTable" 
    data-order='[[ 1, "asc" ]]'
    data-columns='[{"data": "name"}, {"data": "position"}]'
    >
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Salary</th>
                <th>Start Date</th>
                <th>office</th>
            </tr>
        </thead>
    
    </table>
    

    这是给你的完整的sn-p

    var data = [
        {
            "name":       "Tiger Nixon",
            "position":   "System Architect",
            "salary":     "$3,120",
            "start_date": "2011/04/25",
            "office":     "Edinburgh"
        },
        {
            "name":       "Garrett Winters",
            "position":   "Director",
            "salary":     "$5,300",
            "start_date": "2011/07/25",
            "office":     "Edinburgh"
        },
        {
            "name":       "Jane Doe",
            "position":   "SW Architect",
            "salary":     "$5,300",
            "start_date": "2011/07/25",
            "office":     "Edinburgh"
        },
        {
            "name":       "John Doe",
            "position":   "SW Developer",
            "salary":     "$5,300",
            "start_date": "2011/07/25",
            "office":     "Edinburgh"
        }
    ];
    var oTable = $('#example').dataTable({
    	data: data
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <script src="https://cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
    <table  id="example"
    data-column-defs='[{"targets": 0, "width": "200px"}]' 
    data-page-length='2'
    data-class="dataTable" 
    data-order='[[ 1, "asc" ]]'
    data-columns='[{"data": "name"}, {"data": "position"}]'
    >
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Salary</th>
                <th>Start Date</th>
                <th>office</th>
            </tr>
        </thead>
        
    </table>

    【讨论】:

    • 您的答案似乎也适用于 v1.10.8。但是尚不支持 columnDefs,并且使用 th 标签中的 data-width 支持列宽。
    • 很高兴听到,但在您原来的帖子中您说您使用(版本 1.10.18)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多