column(0).cells() returns all cells in table, not just those in the first column

column(0).cells() returns all cells in table, not just those in the first column

jladburyjladbury Posts: 43Questions: 12Answers: 0

Link to test case: https://live.datatables.net/vagenuta/2/edit

Description of problem: I want to update all the cells in the first column of a table. I select that column with column(0), then chain cells(). In a 3 row, 6 column table as in my test case I would expect 3 cells to be returned - those in the first column. However, all table cells are returned.

Is this a bug, or have I misunderstood something?

A subsidiary question, in the light of the fact that internal row indices are not the same as the displayed row positions, is whether I should rely on column(0) always returning the first column in the table. (In my situation, columns are never reordered).

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin
    Answer ✓

    Not a bug. cells() is a top level selector, so the fact that you have called column() beforehand is meaningless.

    Instead, use cells() with a row and column selector:

    table.cells('*', 0)
    

    Get's the cells from all rows and column index 0. Updated example.

    is whether I should rely on column(0) always returning the first column in the table

    Yes. Regardless of column visibility, column(0) will return the first column in the table. See the docs here.

    Allan

  • jladburyjladbury Posts: 43Questions: 12Answers: 0

    Thanks Allan. May I suggest you add a comment to the docs at https://datatables.net/reference/api/columns()? Not that they are wrong, but I was slightly led up the garden by the sentences

    . . . its chained methods providing the ability to get work with the column, such as getting its data or toggling its visibility.

    and

    Returns:
    DataTables.ApiDataTables API instance with selected columns in the result set.

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin
    edited March 26

    Thanks for the feedback. Its really important that I get feedback so I can improve the docs.

    its chained methods providing the ability to get work with the column, such as getting its data or toggling its visibility.

    I think that is correct though is it not? columns([0, 1]).data(), or columns(1).visible(false) for example?

    The same is true of the second statement, that is correct. What I perhaps need to be clearer on is that the selector methods (.cells(), .rows(), .columns()) are top level and don't take into account what has previously been selected. For example:

    table.cells().rows().column(0).visible(false);
    

    would do exactly the same as:

    table.column(0).visible(false);
    

    Allan

  • jladburyjladbury Posts: 43Questions: 12Answers: 0

    Hi Allan, I agree! Thanks for taking the feedback.

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin
    Answer ✓

    I've added:

    This method is a top level selector. That means that, regardless of what might have already been selected by the API instance in the call chain, this method will always return the selected [...], without consideration for what might have been selected previously.

    in this commit to the top level selectors, which will hopefully help clarify things.

    Allan

  • jladburyjladbury Posts: 43Questions: 12Answers: 0

    Perfect!

Sign In or Register to comment.