table.column("#").visible() no longer falsey

table.column("#").visible() no longer falsey

JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

It used to be that

if (table.column("#ID").length > 0 && table.column("#ID").visible()))

Would go down the false when the column didn't exist (it's not added in the initial render depending on permissions) but now the table.column("#ID").visible() is returning the datatable object, which javascript interprets as "yes there is an object" and enters the condition.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    I can't recall off the top of my head why that changed I'm afraid - however, change:

    table.column("#ID").length > 0
    

    to be:

    table.column("#ID").any()
    

    or

    table.column("#ID").count() > 0
    

    and that will correct the expression.

    The issue is that .length is giving a length of 1 due to an empty array, which is what I would actually expect to happen - no items were selected, so it is an empty array of selected items. It sounds like there was a bug in the old versions!

    Allan

  • JammySlayerJammySlayer Posts: 43Questions: 12Answers: 2

    Ah I see... misinterpreted the issue, I'd say .length == 0 feels more correct if the selector returned no columns, pretty sure jquery works to length == 0 when nothing has been found too?

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    The issue is that DataTables' API is multi-table aware. So there is an array of elements, inside the main array, one for each table that the API is operating on. So it actually looks like [ [ ] ] - which is why .length of the first layer gives 1, but really there are no matches. Hence the use of any() or count() which are aware of that structure and account for it.

    Allan

Sign In or Register to comment.