[Filtering] Hide an empty column

[Filtering] Hide an empty column

ZirionZirion Posts: 5Questions: 2Answers: 0

Hi everyone,
I've got on my company's website a DataTable with a search bar for each column (thanks to the plugin).
That being said, I would like to hide the empty columns.
If, for example, a search gives me projects which don't have any comments on. In this case, the "comments" field would disappear.

The code is fragmented in so many files I couldn't send it here, just ask if you need more infos.
Thx in advance,
Zirion.

Answers

  • colincolin Posts: 12,649Questions: 0Answers: 2,161

    You could do that in initComplete. Iterate through the columns data with column().data() and each() (see the example on that page), and if the data is all empty, call column().visible(),

    Colin

  • ZirionZirion Posts: 5Questions: 2Answers: 0
    "initComplete": function( settings, json ) {
            this.api()
              .columns()
              .data()
              .each(function ( value, index ) {
                console.log(value + "   " + index);
                if (value == null) {
                  this.api().column(index).visible();
                  console.log("Pouf !");
                }
              })
          }
    

    There's something I don't get : how are you supposed to know if value is empty ? It does not seem to be an array, and it makes things more difficult to analyse.

    Here is a screenshot of the result of console.log(value + " " + index)

  • colincolin Posts: 12,649Questions: 0Answers: 2,161

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

Sign In or Register to comment.