columnDefs visible targets from array

columnDefs visible targets from array

DanaDana Posts: 28Questions: 15Answers: 1

I have a datatable for which the columns that should be invisible are from an array invisibleItems

 "columnDefs": [
                { "visible": false, "targets": invisibleItems }

but the thing is that all my columns are visible when I'm opening the page. The invisibleItems array comes from a cookie so every time I'm opening the page it should "remember" the invisible columns. It is possible to add the invisible columns from an array?
I've also tried invisibleItems.join() to make the array as a string but same thing.

Replies

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

    Hi Dana,

    The most useful thing here would be send us the contents of the invisibleItems array. As you can see in this example here, it should be an integer array.

    On a different note, DataTables does offer a stateSave ability, so the table returns to its previous state when the page is reload. In its simplest form it doesn't need any config, it just needs to be enabled. It might be worth seeing if this could also be used for you.

    Cheers,

    Colin

  • DanaDana Posts: 28Questions: 15Answers: 1

    Hi Colin,

    Thanks for your answer. Can you please show me an example of the targets coming from an array? Perhaps I'm doing something wrong.
    var invisibleItems = [0, 7, 8, 9, 10, 11, 12]

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

    Hi again,

    Here's the example, modified: http://live.datatables.net/kucudiqi/1/edit

      var invisibleItems = [1,2];
      var table = $('#example').DataTable({
        columnDefs: [{
          visible: false, targets: invisibleItems
        }]
      });
    

    Have you checked for console errors? Are there at least 13 columns in your table?
    If that's all fine, could you create a live example like mine above, or provide all the code around the DataTables initialisation at least.

    Cheers,

    Colin

  • DanaDana Posts: 28Questions: 15Answers: 1

    @colin Thanks for your example! It worked now! Thanks a lot!

This discussion has been closed.