Changing Column Visibility on Multiple Tables

Changing Column Visibility on Multiple Tables

PaddleStatsPaddleStats Posts: 7Questions: 0Answers: 0

I'm using DataTables to display the results of Canoe/Kayak/SUP races that I have collected. I want to give users the ability to customize the display—for instance, displaying a straight list of times, or first grouping by Class or BoatType. The code is working fine when there is a single table on a page, but sometimes there are multiple tables (because there were separate events). While sorting and grouping update fine across all the tables, I can't get column visibility to do the same. I've seen code that supposedly does this, but I can't get it to work. Your thoughts are welcome!

Example of a page: http://live.datatables.net/hicibare/1/edit

Replies

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    I couldn't see in your code where you're trying to change the visibility, but you should just be able to do what you're doing with your search, i.e.

    $('.racetables').DataTable().search($(this).val()).draw();
    

    would be

    $('.racetables').DataTable().column(1).visible(false);
    

    Cheers,

    Colin

  • PaddleStatsPaddleStats Posts: 7Questions: 0Answers: 0

    Colin. Thanks. That's what I thought too (and is the code I'm using). However, it only effects the first table. I'm not sure if this is a bug, but the behavior seems unexpected, since similar code affects all the tables.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Ah, as there's more than one column (two in total), you need to use columns() - see here.

    Colin

  • PaddleStatsPaddleStats Posts: 7Questions: 0Answers: 0

    Yes! Thank you!

  • PaddleStatsPaddleStats Posts: 7Questions: 0Answers: 0

    Although it only makes the columns and not the headers visible....interesting.

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    Although it only makes the columns and not the headers visible.

    Colin's example hides the table headers in both tables. Is this what you are asking or is there a different issue?

    Kevin

  • PaddleStatsPaddleStats Posts: 7Questions: 0Answers: 0

    Colin's example does make the change the visibility of the data in the specified column across all the tables. However, it then throws a fatal error that causes the javascript to stop running. I think the problem is in whatever routine writes the column header/labels. I'm playing around with a simpler version here: http://live.datatables.net/kajaqeso/1/edit which appears to replicate the problem.

    NB The Grouping isn't working in this example because I couldn't figure out how to reference the column for hardcoded data; neither numbers nor strings seemed to work.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    There were several problems with that fiddle:

    1. you weren't getting the value of the selection
    2. you were referencing columns in order() that didn't exist
    3. RowGroup wasn't included

    I've tidied up none here, and that seems to be working as expected.

    Colin

This discussion has been closed.