Problems with hidden columns and filtering

Problems with hidden columns and filtering

silkspinsilkspin Posts: 141Questions: 32Answers: 5

I have a test case http://live.datatables.net/guxojigu/1/edit based on my datatable and this post http://mail.datatables.net/forums/discussion/54088/targeting-a-column-filter-if-a-column-before-is-not-visible#latest which doesn't seem to work with more complex combinations.

What I would like to happen is, as soon as a column is hidden (using colvis) it resets the filter on that particular column and when it's made visible again the previous value has been cleared. Currently there is confusion when toggling multiple columns with unpredictable results. As a last resort I would even be happy if hiding just one column cleared and reset the values of all filters. Hopefully there is a way for me to keep both filtering and column visibility functionality.

I've done 3 tests to demonstrate how it reacts to different scenarios...

TEST 1 - Expected Result
* Position > Director = 4 entries
* Hide “Position” (entries goes back to 57 as required)
* Show “Position” (filter is blank as required).

TEST 2 - Unexpected Result
* Position > Director = 4 entries
* Office > Edinburgh = 2 entries
* Hide “Office” and the “Position” filter becomes blank but still shows 4 entries.
* Hide “Position” and goes back to 57 entries.
* Show “Office” and then “Position” and entries is still 57

TEST 3 - Unexpected Result
* Salary > $4080 = 6 entries
* Office > London = 2 entries
* Hide “Office” and the “Salary” filter still show $4080 and shows 6 entries.
* Hide “Salary” and shows 57 entries again.
* Show “Office” and London shows again but still shows the full 57 entries.
* Show “Salary” and $4080 shows again with the full 57 entries.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    I think the problem you are running into is the hidden columns aren't in the DOM but your idx value isn't taking this into consideration. Use column().index() with the parameter visible to get the visible index. I would loop through all the columns and if the search value is empty and the visible column index exists then clear the input. For example:
    http://live.datatables.net/guxojigu/2/edit

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    I've been trying to get this working but I've had no luck. I did some testing by changing the jquery that should clear the select value to $('#example thead tr:eq('+idx+') td:eq(1) select').css("background-color", "yellow"); so I could see what was happening and the results were not what I was expecting. Columns 1 and 2 <thead><td> did sometimes go yellow but other columns didn't. I've used console.log and the looping through columns does seem to be working.

    I've thought of another way to handle this and wondered if it's possible? I currently have a button to clear the search box and filters (code below). This does clear and reset the filters, even on the hidden columns, but when the column is shown again the select filter still shows the last value, although the filter isn't applied on this. Is it possible to loop through the hidden columns and set the select value back to val(""); and attach this function to the 'Clear Filters' button?

    buttons: [
            'colvis',
            {
              text: 'Clear Filters',
              action: function ( e, dt, node, config ) {
                table.search("").columns().search("").draw();
                $("thead select").val("");
              }
            }
           ],
    
  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    Answer ✓

    Actually I think the :eq() values are swapped. I think it should be this:
    $('#example thead tr:eq(1) td:eq('+idx+') select').val('');

    The idx should be the td or th not applied to the tr. Updated example:
    http://live.datatables.net/guxojigu/6/edit

    Is it possible to loop through the hidden columns and set the select value back to val("");

    No because they are no tin the DOM.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    That was it Kevin. Thanks for spotting that. I've done quite a lot of testing and everything seems to be OK from the combination of actions I've tried. I started having problems when I merged the cascading version of my filters, but thanks to your advice regarding column().index() and using the parameter visible I've hopefully managed to fix them.

This discussion has been closed.