Row index bug?

Row index bug?

wannadreamwannadream Posts: 3Questions: 0Answers: 0

Basically, I created a data table with KnockoutJS according to the example here:
https://datatables.net/dev/knockout/

However, I found a big issue regarding row index. Here is some debug info from Chrome:

This is output after I added 3 rows in.

And I use row(2) to check the specific row data.

I am surprised, should it be the first one whose noteId is 636? I doubt this is a bug or it is by design?
Because I cannot find the right index, the dynamic link between data table and KnockoutJS observable array breaks.

Replies

  • wannadreamwannadream Posts: 3Questions: 0Answers: 0

    Sorry for the typo, I used rows(0).

  • kthorngrenkthorngren Posts: 21,368Questions: 26Answers: 4,956

    It does seem like a bug. I've seen this behavior before.. I put a test case together to show what you describe. I added selector-modifier and tried with row(0).data(). Using rows().data() seems to be the only option that follows the current sort order.

    http://live.datatables.net/retapuki/1/edit

      //All data is in the current sort order
      console.log(table.rows().data());
    
      //The row returned is the first row of the table load order - "Tiger Nixon"
      console.log(table.rows(0).data());
      console.log(table.rows(0, {order: 'current'}).data());
      console.log(table.row(0).data());
      console.log(table.row(0, {order: 'current'}).data());
    

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi all,

    Yep, Kevin's examples are all working as expected - no bugs there.

    When nothing is given to rows(), it returns all rows, with the default selector-modifier of current, so they're ordered as you see in the table.

    When you specify a single row, that rowId is the initial load index, so 0 is Tiger Nixon, and always will be, whatever the order.

    That said, I wanted to show that by this example

      console.log(table.rows([0,2], {order: 'current'}).data());
    

    ... and found a bug! Here you would expect Tiger to be second, but he's not, he's first, the data is returned in the order of the indexes, not by the current order as requested. I'll raise this one and report back here when fixed.

    Shout back if you want more explanation on the other cases.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,368Questions: 26Answers: 4,956

    I've been bit by this behavior before and spent a bunch of time finding a way to get a single row based on the current order. It took a lot of digging for me to realize that Colin is right and a single parameter is the row index based on the row-selector.

    Can you add an example to the api row() and rows() API docs to show how to obtain a single row based on the table order? I've done it before but don't remember which page :-)

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Yeah, it's not the most obvious thing. The easiest way to get the row in a given position (for example 33), is to use:

    table.row(':eq(33)').data()
    

    That's using jQuery on the row-selector. An extra example would be worthwhile, I'll add it in.

    Cheers,

    Colin

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin

    What is happening when you specify [0,2] is that the selector is running twice - once for each entry in the array. The order is applied inside the selector, so the outer loop that is building them up has no concept of the order - thus it will always match the order of the items passed in an array.

    To take it turned, consider ['.myClass', 2] - it would select all .myClass rows in current sorting order and then add row index 2 to it.

    That's really not ideal. What we probably need to do is change the selector behaviour so that the order is applied once and once only at the end of the selection. I'm a bit concerned about what that might do to performance - I'm not sure yet.

    Going to shunt this bug to v2 to be fixed as part of that work as it will likely take a bit of time to do.

    Good point about the need for an example on the :eq() selector!

    Allan

  • wannadreamwannadream Posts: 3Questions: 0Answers: 0

    Thanks to all of you.

    :eq() does solve the problem. Maybe it's better to update the sample: https://datatables.net/dev/knockout/

This discussion has been closed.