denoise
denoise

Reputation: 1097

DataTables columns, columnDefs and rowCallback HTML5 initialisation

I currently have a datatable (ver 1.10.18) loaded with several options with js, but I need to make my code more reusable and I'm trying to initialise my datatable with html5 data-* attributes.

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

my jQuery code looks like:

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");
        }
    }
});

There are some options in common I would use for every datatable, but it would be great if I can set the columns, columnDefs and rowCallBack directly in my html using the html5 data-* attributes so I can use the same code for different tables, like:

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

I haven't found anywhere how to use the html5-* attributes other than ordering, sorting and page length.

Is setting this options with html5 actually possible with datatables.js ?

Upvotes: 0

Views: 5872

Answers (1)

Recep Karadas
Recep Karadas

Reputation: 689

First you need version 1.10.5 as stated here

As of v1.10.5 DataTables can also use initialisation options read from HTML5 data-* attributes

Then you have to put the data attributes to the table element and not to the column elements.

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

Here is the full snippet for you

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>

Upvotes: 2

Related Questions