how order DataTable with one column of strings by row index?
how order DataTable with one column of strings by row index?
The goal is to create a single column dynamic DataTable, for which added rows go to the top of the table.
Two columns is straightforward: the code below modifies the add_row example, to create a two column table, the first column being a counter (from the example) and the second column being two random characters. The "order" option orders the table in descending order using the first column (0,"desc").
Options to accomplish the goal stated above:
1. Is there an order option for ordering on row index? This would eliminate the need for the first column.
2. If not 1. is there a way to hide the 0th column?
Thanks!
$(document).ready(function() {
var t = $('#example').DataTable( {"order": [[ 0, "desc" ]],} );
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var counter = 1;
$('#addRow').on( 'click', function () {
var string = chars.charAt(Math.floor(Math.random() * chars.length));
string += chars.charAt(Math.floor(Math.random() * chars.length));
var rowNode = t.row.add( [
counter +'.1',
string,
] ).draw( false ).node();
counter++;
} );
// Automatically add a first row of data
$('#addRow').click();
} );
This question has an accepted answers - jump to answer
Answers
I figured out above option 2 from the Hidden Columns example. The table is initialized below with the necessary columnDefs:
Thanks for posting back - good to hear you for it working!
Allan