How can I add a fixed index to my rows after sorting?

How can I add a fixed index to my rows after sorting?

ddmmatiasddmmatias Posts: 2Questions: 1Answers: 0

I'm using data-tables to create a dashboard of football match stats. Using the same collection of matches I'm creating multiple tables, showing and sorting by different columns to create different "Tops".
As an example could be "Matches with most goals", "Matches with less goals", "Matches with most goals from the home team", etc. All using the same collection.

Sorting gives me the correct order for each table. So I added an Index column to make a "Top" like table. I did using an example I found here:

$(document).ready(function() {

            var t = $('#maxGoalDiff').DataTable( {
                    "pageLength": 3,
                    "bLengthChange": false,
                    "columnDefs": [ {
                        "searchable": false,
                        "orderable": false,
                        "targets": 0,
                    }],
                    "order": [[6, 'desc' ]]
                } );
                t.on( 'order.dt search.dt', function () {
                t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                    cell.innerHTML = i+1;
                } );
            }).draw();

        } );

This works perfect, but what I can't find how to do, is make this index value fixed to each row.
I need this so for example, if I use search box to filter by team or by a specific match, I want to know which position in the top, the filtered result is. Is there a way to this from the table?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    Instead of using the order and search events use the init event. Change 'order.dt search.dt' to 'init.dt' then the index is only built once after the Datatable is initialized.

    Kevin

  • ddmmatiasddmmatias Posts: 2Questions: 1Answers: 0

    @kthorngren couldn't make init.dt draw the indexes but you pointed me in the right direction. Symply by removing "order" event, I prevented the column to be redrawn when I search.

    With that and changing "order" for "orderFixed" I was able to draw the index on ordering, and not changing it when I filter. Thanks!

Sign In or Register to comment.