Individual column searching : Filter on checkbox state

Individual column searching : Filter on checkbox state

SchmurtzSchmurtz Posts: 5Questions: 2Answers: 0
edited May 2020 in Free community support

Hi,

I mix different type of searching in a signe table :
Individual column searching (text inputs) -> column 0
Individual column searching (select inputs) -> column 1
Custom Individual column searching (select inputs) -> column 2

And I try to filter the column 3 depending the checkbox filter....

Don't take care to the last function which try to do it (it's probably completely false).

To resume I would like that when you check the checkbox "Agree" to see all the checked row...

Thank you for your help.

http://jsfiddle.net/1rpq3j8x/4/

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    This thread should help, it's asking the same thing.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    This example from this thread shows how to filter on the checkbox checked state using a search plugin.

    Kevin

  • SchmurtzSchmurtz Posts: 5Questions: 2Answers: 0

    Thanks guys, seems helpful.
    During this time I've updated my example with something less messy :
    http://live.datatables.net/yipolina/1/edit

    Still not working with the checkbox but may be I will be able to make something with your example @kthorngren , it is a really different function that the one that I use but it is really similar.

  • SchmurtzSchmurtz Posts: 5Questions: 2Answers: 0
  • tefdattefdat Posts: 42Questions: 7Answers: 3

    With a fair amount of research and trial and error, I solved a similar requirement for myself.
    I use as well a checkbox, which selects the row.
    Trigger is a button for switching between selected and all rows.
    The biggest challenge was to make it include selected rows from **all **pages.

    var cFilteractive;
    ...
    ...
    
    {
        className: "btn_show_selected",
        titleAttr: "toggle between selected rows - shortcut [s]",
        key: "s",
        action: function (e, dt, node, config) {
            //toggle between selected/all rows - this is the easy way, but works unfortunately only
            //with the current/node page - selected rows in other pages are not considered
            //dt.rows({ selected: false }).nodes().to$().toggle();
    
    
            //working way - if custom search is active, could be evaluated as well with lenght
            //of the search array --> $.fn.dataTable.ext.search.length
            //no filter = 5, with custom filter =6
            //anyway, i dont rely on that - i am using a variable for toggling --> cFilteractive
            if (!cFilteractive) {
                $.fn.dataTable.ext.search.push(function (settings, searchData, index, rowData, counter) {
                let selected = dt.rows(index, { selected: true })[0]?.length ? true : false;
                    if (selected) {
                        return true;
                    } else {
                        return false;
                    }
                });
            } else {
                //reset/remove custom filter
                $.fn.dataTable.ext.search.pop();
            }
            //toggle variable
            cFilteractive = !cFilteractive;
            //execute search
            table.draw();
        },
    },
    
  • thegamechangerprothegamechangerpro Posts: 81Questions: 30Answers: 1
    edited January 2023

    @Schmurtz

    Sorry, i know this is an old thread, but the checkbox filtering is EXACTLY what I have been looking for.

    http://live.datatables.net/yipolina/4/edit

    How would you apply this functionality to multiple columns?

    Simply adding additional column indexes into the this.api.columns([ ])... doesn't work correctly. Is it due to multiple items with the same id?

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    @thegamechangerpro

    There are a couple issues I see with that solution:

    1. It manages the plugin to run by pushing and popping between the checked and unchecked plugin. This is ok except it will cause problems if you have other search plugins
    2. It uses var checked = $('#chk_' + dataIndex).prop('checked'); but this variable is not used in the plugin. It is searching the DOM for that ID. Searching the DOM can be slow so its best to avoid in the search plugin.
    3. The unchecked plugin always returns true so all the code executed is unnecessary.
      You want the search plugins to be as efficient as possible especially as the row count increases.

    I would do something more like this:
    http://live.datatables.net/yipolina/175/edit

    It uses assigns the same name to the checkboxes in the column. It uses a global variable to keep track of which columns, by checkbox name, that are checked, ie want to filter by the checked rows.

    It uses a variable checkboxNames to define the checkbox names by column index (key). This can be changed to derive the names from the column classname or title or whatever.

    Kevin

Sign In or Register to comment.