Javascript DataTables列、列定义和行回调HTML5初始化

Javascript DataTables列、列定义和行回调HTML5初始化,javascript,jquery,datatables,datatables-1.10,Javascript,Jquery,Datatables,Datatables 1.10,我目前有一个datatable(1.10.18版),其中加载了几个js选项,但我需要使代码更具可重用性,我正在尝试使用html5 data-*属性初始化我的datatable <table id="dataTable" cellspacing="0" width="100%" data-source="ajax.mysource.php"> <thead> <tr> <th>Name</th&g

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

<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>
每个datatable都有一些常用的选项,但如果我可以使用html5 data-*属性直接在html中设置列、列定义和行回调,那么我就可以对不同的表使用相同的代码,比如:

<th data-columns="address" data-column-defs="{targets: 1, width:'150px'}" data-row-callback="function...">Address</th>
地址
除了排序、排序和页面长度之外,我还没有发现如何使用html5-*属性


在datatables.js中使用html5设置此选项是否可行?

首先,如前所述,您需要1.10.5版

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

然后必须将数据属性放在table元素而不是column元素上

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

名称
位置
薪水
开始日期
办公室

首先,您需要如上所述的版本1.10.5

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

然后必须将数据属性放在table元素而不是column元素上

<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版)。您的答案似乎也适用于v1.10.8。但是columnDefs还不受支持,并且在th标记中使用data width支持列宽。很高兴听到这个消息,但是在您最初的帖子中,您说您使用了(1.10.18版)。