Is column searchability dynamic?

Is column searchability dynamic?

StuartFStuartF Posts: 6Questions: 3Answers: 0

I can set the searchable option on a column when I define it, but when I dynamically hide/show other columns, I'd like to also update the searchable value. I con't want my newly hidden columns to be searchable. Something like below:

applyColumnChanges = function (datatable) {
                var i,
                    hiddenColumns = [];

                for (i = 0; i < scope.columnDefinitions.length; i++) {
                    datatable.column(i).visible(scope.columnDefinitions[i].visible);
                    datatable.column(i).searchable = scope.columnDefinitions[i].visible; //would be nice if this worked
                    if (!scope.columnDefinitions[i].visible) {
                        hiddenColumns.push(scope.columnDefinitions[i].data);
                    }
                }
                return hiddenColumns;
            };

Any idea if this is currently possible?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    Answer ✓

    Currently no - there is no option to dynamically enable and disable the search-ablity (or order-ablity) of a column. However, it is a good suggestion and I'll at it to my futures list!

    Allan

  • StuartFStuartF Posts: 6Questions: 3Answers: 0

    Thanks, Allan. After thinking about this for a bit, it seems to me that it makes more sense to update the search behavior to exclude hidden columns. It's a bit of an edge case for us, but one user has noticed after customizing the visible columns on a report, the search feature behaves oddly. Turns out he was getting "hits" on a column he had just hidden, so he was seeing matching rows that didn't have any visible match to his search.

    I suppose there might be some need to support searching hidden columns, but it's hard to imagine this being anything other than a confusing experience for a user.

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin

    Searching hidden columns can be quite useful for if you are doing a child row type display. For example you might search for a telephone number that is in the child row, but also in a hidden column thus making it searchable. Also the column visibility can be changed dynamically, so data that was searched for and found couldn't just cease to exist if that column is then hidden.

    It is a bit of a rough zone for certain and it does require a little bit of interaction design from the developer (yourself :-) ) to get exactly what you want. In this case you could use columns.searchable for your hidden columns. However, changing the default would break backwards compatibility, so not a chance I want to make at the moment.

    Regards,
    Allan

  • duselguyduselguy Posts: 24Questions: 3Answers: 0
    edited August 2014

    Agreeing with Stuart,

    At least 2 reasons for the customer:

    1. "Turns out he was getting "hits" on a column he had just hidden, so he was seeing matching rows that didn't have any visible match to his search."

    2. The customer hides the column because the column is not interesting for him (including the search function) for some task at the moment.

    Best regards,
    Vladimir

    P.S. The standard scenario:

    1) Trap change column visibility event.

    2) Dynamically exclude/include the column from search.

  • mscdexmscdex Posts: 6Questions: 3Answers: 0
    edited November 2014

    I came across this post having the same problem and I agree with Vladimir and Stuart that this default behavior can be very confusing (especially when coupled with the DataTables search highlighter plugin).

    I needed this feature right now so I took it upon myself to write a plugin (tested only with DataTables 1.10.x) that provides a setting to disable searching of cells in hidden columns. This is a very simple implementation that works as long as you don't have exceptions to the rule (e.g. some columns should never be searchable or always be searchable, however pull requests to add such configurable behavior are welcome).

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin

    DataTables search highlighter plugin

    Nice to know it is being used :-). I haven't had much feedback on that one.

    Nice plug-in! Thanks for sharing it with us - I like it a lot! I think this probably should be something that should go into the core - possibly 1.11 if not before.

    Regards,
    Allan

This discussion has been closed.