how order DataTable with one column of strings by row index?

how order DataTable with one column of strings by row index?

demianrdemianr Posts: 4Questions: 2Answers: 1
edited November 2016 in Free community support

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

  • demianrdemianr Posts: 4Questions: 2Answers: 1
    edited November 2016

    I figured out above option 2 from the Hidden Columns example. The table is initialized below with the necessary columnDefs:

        var t = $('#example').DataTable( {
                        "order": [[ 0, "desc" ]],              
                       "columnDefs": [ {
                             "targets": [ 0 ],
                             "visible": false,
                             "searchable": false
                           },
                       ]
        });
    
    
  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin
    Answer ✓

    Thanks for posting back - good to hear you for it working!

    Allan

This discussion has been closed.