cell().index().row not returning current row index regardless of order modifier

cell().index().row not returning current row index regardless of order modifier

ickybroickybro Posts: 5Questions: 1Answers: 0
edited November 2015 in Free community support

When using the cell() selector to select a single cell, the row index is always the initial index. This seems to be the case with either the default selector-modifier or explicitly setting the order to current.

In this bin, Hope Fuentes is initially on row 37 but after the sort done in the table initialization, Hope Fuentes is on row 13. Index is always returned as 37 and not 13.

Seems like a bug.

data tables live jsbin
http://live.datatables.net/saweyado/1/edit

debugger output
http://debug.datatables.net/ejixew

edit:
This row index is also inaccurate if .index().row is chained to the original cell() method.

This question has accepted answers - jump to:

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    The index will always start at 0 (or 1?) for the first row, then increment down, regardless of the order, if you want to use something that wont do that, I recommend using the rowId

  • ickybroickybro Posts: 5Questions: 1Answers: 0

    hi jLinux, not sure I get you. This is not an off by 1 type of thing. I want to know the current row index, after sorting has been done on the table, rather than the initial row index.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Thats what im saying. Sorting the row will change the indexes, as they will always increment from the top down, no matter how many times you order columns

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    the row index is always the initial index

    This is correct and intentional. If you want to use the current index, use :eq(0) to select the first row.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Oh, then maybe I was wrong.. Sorry. I use rowId for this reason

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Yes, using a DOM ID removes the positional issue entirely.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Which is what I definitely recommend! You can use row().id() and rows().ids()

  • ickybroickybro Posts: 5Questions: 1Answers: 0

    Thanks guys. I'm not sure how row().id or using :eq(0) to select by row index helps me out. I guess it is faster to select a row by an id, or the :eq(0) selector. That is not my problem though. I am selecting the row already in the bin i linked to. Figuring out where the row is in the table after selected is still a problem for me. If my cell was always displayed on the page when I need to know its row, there would be no problem, but the chance is more likely that is paged. Maybe I am missing something in the row().id() spec, seems like it gives a way to select a row; not sure how to figure out current index once selected though. Maybe my initial post was vague, I figured the jsbin would make it more clear. Maybe I am missing something fundamental.

    Thanks for clearing up my misunderstanding of cell().index().row though. I appreciate the hand.

  • ickybroickybro Posts: 5Questions: 1Answers: 0

    Also, I guess this works to get current row index using initial row index:

    var cellInitialRowIndex = cell.index().row;
    var currentRowIndex = table.rows().eq(0).indexOf(cellInitialRowIndex);
    

    changes to bin below:
    http://live.datatables.net/saweyado/2/edit

    thanks again guys.

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    I see - you want to find the row's position in the table - in which case, indexOf`, as you have done is probably the best way of doing it.

    Allan

This discussion has been closed.