I’m trying to use “createdCell” to sort the column, I want to create a dynamic “data-order” attribute…
I have this HTML:
<code><table id="datatable_promotions" class="table table-striped w-100">
<thead>
<tr>
<th>ID-BricoIO</th>
<th>ID-Volantino</th>
<th>Titolo</th>
<th>Codice</th>
<th>Data inizio</th>
<th>Data fine</th>
<th>Modifica</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID-BricoIO</th>
<th>ID-Volantino</th>
<th>Titolo</th>
<th>Codice</th>
<th>Data inizio</th>
<th>Data fine</th>
<th>Modifica</th>
</tr>
</tfoot>
</table>
</code>
<code><table id="datatable_promotions" class="table table-striped w-100">
<thead>
<tr>
<th>ID-BricoIO</th>
<th>ID-Volantino</th>
<th>Titolo</th>
<th>Codice</th>
<th>Data inizio</th>
<th>Data fine</th>
<th>Modifica</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID-BricoIO</th>
<th>ID-Volantino</th>
<th>Titolo</th>
<th>Codice</th>
<th>Data inizio</th>
<th>Data fine</th>
<th>Modifica</th>
</tr>
</tfoot>
</table>
</code>
<table id="datatable_promotions" class="table table-striped w-100">
<thead>
<tr>
<th>ID-BricoIO</th>
<th>ID-Volantino</th>
<th>Titolo</th>
<th>Codice</th>
<th>Data inizio</th>
<th>Data fine</th>
<th>Modifica</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID-BricoIO</th>
<th>ID-Volantino</th>
<th>Titolo</th>
<th>Codice</th>
<th>Data inizio</th>
<th>Data fine</th>
<th>Modifica</th>
</tr>
</tfoot>
</table>
And this JS:
<code>datatable_promotions = $('#datatable_promotions').DataTable({
paging: true,
lengthMenu: [10, 25, 50, 75, 100],
pageLength: 25,
searching: true,
order: [
[3, 'desc'],
],
ordering: true,
columns: [
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'promo_id',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'id_volantino',
render: function (data, type, row) {
if (data === null) {
return '*****';
}
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'title',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'code',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: false,
type: 'string',
data: 'date_start',
render: function (data, type, row) {
return row.date_start_formatted;
},
createdCell: function (cell, cellData, rowData, rowIndex, colIndex) {
$(cell).attr('data-order', rowData.date_start);
//$(cell).attr('data-sort', rowData.date_start);
},
},
{
visible: true,
orderable: true,
searchable: false,
type: 'string',
data: 'date_end',
render: function (data, type, row) {
return row.date_end_formatted;
},
},
{
visible: true,
orderable: false,
searchable: false,
type: 'string',
data: 'id',
render: function (data, type, row) {
return '<div class="text-center"><button class="btn btn-sm btn-primary fw-bold">Gestisci</button></div>';
},
},
],
});
</code>
<code>datatable_promotions = $('#datatable_promotions').DataTable({
paging: true,
lengthMenu: [10, 25, 50, 75, 100],
pageLength: 25,
searching: true,
order: [
[3, 'desc'],
],
ordering: true,
columns: [
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'promo_id',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'id_volantino',
render: function (data, type, row) {
if (data === null) {
return '*****';
}
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'title',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'code',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: false,
type: 'string',
data: 'date_start',
render: function (data, type, row) {
return row.date_start_formatted;
},
createdCell: function (cell, cellData, rowData, rowIndex, colIndex) {
$(cell).attr('data-order', rowData.date_start);
//$(cell).attr('data-sort', rowData.date_start);
},
},
{
visible: true,
orderable: true,
searchable: false,
type: 'string',
data: 'date_end',
render: function (data, type, row) {
return row.date_end_formatted;
},
},
{
visible: true,
orderable: false,
searchable: false,
type: 'string',
data: 'id',
render: function (data, type, row) {
return '<div class="text-center"><button class="btn btn-sm btn-primary fw-bold">Gestisci</button></div>';
},
},
],
});
</code>
datatable_promotions = $('#datatable_promotions').DataTable({
paging: true,
lengthMenu: [10, 25, 50, 75, 100],
pageLength: 25,
searching: true,
order: [
[3, 'desc'],
],
ordering: true,
columns: [
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'promo_id',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'id_volantino',
render: function (data, type, row) {
if (data === null) {
return '*****';
}
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'title',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: true,
type: 'string',
data: 'code',
render: function (data, type, row) {
return data;
},
},
{
visible: true,
orderable: true,
searchable: false,
type: 'string',
data: 'date_start',
render: function (data, type, row) {
return row.date_start_formatted;
},
createdCell: function (cell, cellData, rowData, rowIndex, colIndex) {
$(cell).attr('data-order', rowData.date_start);
//$(cell).attr('data-sort', rowData.date_start);
},
},
{
visible: true,
orderable: true,
searchable: false,
type: 'string',
data: 'date_end',
render: function (data, type, row) {
return row.date_end_formatted;
},
},
{
visible: true,
orderable: false,
searchable: false,
type: 'string',
data: 'id',
render: function (data, type, row) {
return '<div class="text-center"><button class="btn btn-sm btn-primary fw-bold">Gestisci</button></div>';
},
},
],
});
And I update the table with:
<code>datatable_promotions.clear().rows.add(data).draw(false);
</code>
<code>datatable_promotions.clear().rows.add(data).draw(false);
</code>
datatable_promotions.clear().rows.add(data).draw(false);
I’m interested in the fact that at render time, the text shown in a specific column is a date in d/m/Y format, while to maintain the ordering I would need data-order=”date(‘Y-m-d’)”.