2

I want to add a row number for each row in my data table. I am using plugin from http://datatables.net The page which tells how to add the index is http://datatables.net/release-datatables/examples/api/counter_column.html

... 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 ->
  $('#staffs').dataTable
    sPaginationType: "full_numbers"
    bJQueryUI: true
    }
1
  • add your datatables initialization code. Commented Nov 5, 2012 at 17:50

3 Answers 3

3

Here is an example from datatables.net 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/staffs.js.coffee)' error

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

Sign up to request clarification or add additional context in comments.

5 Comments

right thats copy/paste from the link I provided (i seen this code already). I just dont know where to put this code to actually make it work
haven't clicked on your link... edited my answer (added your two line of code...)
I have tried just copying and pasting it like that before but I keep getting the error 'SyntaxError: reserved word "function" on line 4 (in /home/ubuntu/ruby/scoreboard/app/assets/javascripts/staffs.js.coffee)'
start by successfully running alert('oki'); , then replace it with the code I posted above
also take a look at this stackoverflow.com/q/9011103/617373 and this stackoverflow.com/q/10188363/617373 accepted answers...
0

jquery is javascript. You need to add the code Daniel pasted between

<script language="javascript">

and

</script>

tags.

1 Comment

thank you, this would work if I was keeping this inside the html file, but what worked for me was the js2coffee converter.
0

I am working with latest dataTable 1.10 and gem rails datatable and ajx for

finding the DataTable row number(Serial Number)

def data outer = [] records.each_with_index do |record, index| outer << [ # comma separated list of the values for each cell of a table row # example: record.attribute, index + 1 + params[:start].to_i, record.company_name, record.id, record.patients.count, record.revenue_total ] end outer end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.