DataTables列,columnDefs和rowCallback HTML5初始化

问题描述 投票:0回答:1

我目前有一个数据表(版本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- *属性。

用datatables.js实际上可以用html5设置这个选项吗?

javascript jquery datatables datatables-1.10
1个回答
2
投票

首先你需要版本1.10.5,如here所述

从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>

以下是适合您的完整代码段

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>
© www.soinside.com 2019 - 2024. All rights reserved.