When using columnControl, is there an event to capture which column(s) are being used for searching?

When using columnControl, is there an event to capture which column(s) are being used for searching?

dbungedbunge Posts: 7Questions: 2Answers: 0

Link to test case: https://live.datatables.net/xatimufo/7/edit

Description of problem: I can use "api.on('search'

Answers

  • dbungedbunge Posts: 7Questions: 2Answers: 0
      api.on('search', (eInfo, eCtxt, el) => {
        let searchCriteria = api.search();
        console.log('searchCriteria', searchCriteria);
      });
    

    This shows what is typed in at the top of the screen.

    I am trying to find out what filters have been applied for specific columns (e.g. "Name contains 'ra' and Position starts with 'en'").

    I have not found an event to hook into. I tried using "api.on('draw')", but it does not seem to have any of the search information.

  • allanallan Posts: 65,207Questions: 1Answers: 10,804 Site admin

    There isn't an event for that I'm afraid - it isn't something that I'd thought of. What's the use case?

    draw should work - check for .column(x).search.fixed('dtcc').

    Allan

  • dbungedbunge Posts: 7Questions: 2Answers: 0

    The use case is so that when somebody viewing the table enters a value into multiple "searchDropdown" fields, it's more readily apparent which field(s) are being used for the filter.

    For instance, if I enter in "London" for Location and enter in "Eng" for position, there is no "search summary" section which shows what I've entered for filtering.

  • dbungedbunge Posts: 7Questions: 2Answers: 0
    edited October 8

    Using ".column(x).search.fixed('dtcc');" for numeric gives odd results of " function(t){return S(t)!=e}" (where the comparison changes based on the dropdown.

    I will look at this again tomorrow.

  • allanallan Posts: 65,207Questions: 1Answers: 10,804 Site admin

    Yes, the search applied by ColumnControl is a function that is then evaluated by DataTables. There isn't really an API to get the value that is being searched on by ColumnControl. If you just want to determine what column's have a ColumnControl search then the dtcc check could be made (truthy).

    That all said, you could consider using state() to get the current state of the table. It does have information about the applied ColumnControl search:

    Checking that in draw might be your best bet for now.

    Allan

  • dbungedbunge Posts: 7Questions: 2Answers: 0

    The following is working for me for both the regular searches and for the searchlist. I've added it in to my "on('info')" handler so that it is added as another span after the span where I am doing the "Sorted by" logic.

    For the searchlist, the "search.fixed('dtcc')" was not working since it uses "dtcc-list"

                let spanFilter = document.createElement('span');
                spanFilter.classList.add('dt-info-filter');
                spanFilter.textContent = 'Filtered by: ';
    
                var showFilter = false;
    
                api.columns().every(function () {
                    let col = this;
                    let crit = col.title();
                    let searchField = (col.search.fixed().length > 0);
                    for (let i = 0; i < col.search.fixed().length; i++) {
                        let srchType = col.search.fixed(i);
                        let srchValue = col.search.fixed(srchType);
                        if (srchValue == null) {
                            searchField = false;
                        }
                    }
    
                    let term = col.search.fixed('dtcc');
                    if (term != null || searchField) {
                        spanFilter.textContent += crit + ',';
                        showFilter = true;
                    }
                });
    
                //cleanup end of text so no trailing space or comma
                spanFilter.textContent = spanFilter.textContent.trim();
                spanFilter.textContent = spanFilter.textContent.replace(/,+$/, '');
                if (showFilter) {
                    el.appendChild(spanFilter);
                }
    
    
  • allanallan Posts: 65,207Questions: 1Answers: 10,804 Site admin

    Nice one :)

    Allan

Sign In or Register to comment.