StanM StanM - 1 year ago 76
Javascript Question

How do I add an index column to Datatable?

I want to add a row number for each row in my data table. I am using plugin from The page which tells how to add the index is

... however I don't know how to actually implement it to make it work. I know extremely little about jquery / javascript which would help in this case. I don't know where to put this code to make it work (if it helps i am also using Ruby on Rails)

The initialization code is:

jQuery ->
sPaginationType: "full_numbers"
bJQueryUI: true

Answer Source

Here is an example from site DataTables row numbers example

$(document).ready(function() {
    $('#staffs').dataTable( {
        sPaginationType: "full_numbers",
        bJQueryUI: true,
        "fnDrawCallback": function ( oSettings ) {
            /* Need to redo the counters if filtered or sorted */
            if ( oSettings.bSorted || oSettings.bFiltered )
                for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
                    $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
        "aoColumnDefs": [
            { "bSortable": false, "aTargets": [ 0 ] }
        "aaSorting": [[ 1, 'asc' ]]
    } );
} );

Regarding your SyntaxError: reserved word "function" on line 4 (in /home/ubuntu/ruby/scoreboard/app/assets/javascripts/' error

take a look at this rails, getting syntax error with coffee script

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download