Get 2025 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

datatable order for column


Im using the datatable from the template angular of metronic v8, how can i capture the event of the user clicking on table column name?

this is how i generate the table:

generateTable()
{
this.datatableConfig = {
serverSide: true,
processing: true,
autoWidth: true,
lengthChange: true,
pageLength: 10,
lengthMenu: [10,25,50,100],
ajax: (dataTablesParameters: any, callback) => {
this.userService.list(dataTablesParameters.start / dataTablesParameters.length + 1, dataTablesParameters.length, this.filterParams).subscribe(resp => {
console.log("QUI" ,resp);
const userList = {
draw: dataTablesParameters.draw,
recordsTotal: resp.total,
recordsFiltered: resp.total,
data: resp.data
}
callback(userList);
});
},
columns: [
{
title: this.translate.instant("USER.USERNAME"),
data: 'username',
orderData: [0],
orderSequence: ['asc', 'desc'],
type: 'string',
searchable: true,
render: function (data, type, full) {
return data || '';
}
},
{
title: this.translate.instant("USER.FULLNAME"),
data: 'fullname',
render: function (data, type, row) {
return data || '';
},
orderData: [1],
searchable: true,
orderSequence: ['asc', 'desc'],
type: 'string',
},
{
title: this.translate.instant("USER.EMAIL"),
data: 'email',
render: function (data, type, row) {
return data || '';
},
orderData: [2],
searchable: true,
orderSequence: ['asc', 'desc'],
type: 'string',
}
],
createdRow: function (row, data, dataIndex) {
$('td:eq(0)', row).addClass('d-flex align-items-center');
},
};
this.table = $('#userTable').DataTable(this.datatableConfig)
}


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi Giacomo Cardillo,

Can you try using order.dt for capturing the event? Note the .dt suffix, which is required for DataTables events.

Use this.table.order() to get the current sort state:

order[0][0] gives the column index.
order[0][1] provides the sort direction (asc or desc).

Make sure that this.table is properly initialized and points to the correct DataTable instance.

Verify there are no issues by checking the browser console for errors related to the table.

Let me know how it works



Hi Giacomo Cardillo

Sorry for the delay. To capture the event of a user clicking on a table column header in your DataTable, you can use the order event provided by DataTables. This event is triggered whenever the table's sorting order is changed, including when a user clicks on a column header.

https://datatables.net/reference/event/order


// Add order event listener
this.table.on("order", () => {
const order = this.table.order(); // Get current order state
console.log("Column index:", order[0][0]); // Column index
console.log("Sort direction:", order[0][1]); // Sort direction ("asc" or "desc")
});



no its not working, the event is not caught


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(