SearchPanes: How to retrieve filters applied

SearchPanes: How to retrieve filters applied

mbruccombrucco Posts: 7Questions: 3Answers: 0
edited April 2021 in Free community support

Hi,
I've a datatable with searchpane, user can apply more than one filtering.
I need to extract list of selection
criteria applied.

Let me do an example.

User select from searchpane ("City" = "Milano") and ("Dummy2" = "B").

I've a button to submit data to server, but I need to send selection criteria too; so I've to send:
{"data": {"Name": "Maurizio", "City": "Milano", "Dummy1": "A", "Dummy2": "B"},
"criteria": {["Column": "City", "Value": "Milano"], ["Column": "Dummy2", "Value": "B"]} }

Is there a method to extract search criteria?

Thanks in advance.

This question has an accepted answers - jump to answer

Answers

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

    You can do something like this here : http://live.datatables.net/midaduvi/1/edit .

    This is getting the values of the selected rows, which you could then construct your message with,

    Colin

  • mbruccombrucco Posts: 7Questions: 3Answers: 0

    Yes, it works; but only if searchPane are on-screen.

    I've a searchPanes activated from a button; so divs are not visible when I can submit the message.

  • mbruccombrucco Posts: 7Questions: 3Answers: 0

    May be it exists a more elegant solution than I got, below action on button.

    function(e, dt, node, config) {
       var filterCriteria = "";
       var searchPanes = dt.context[0]._searchPanes.s.panes;
       searchPanes.forEach( searchPane => {
          var criteriaField = "['" + searchPane.s.name + "': [";
          var toAdd = false;
          if ( searchPane.selections.length > 0 ) {
              searchPane.selections.forEach( elem => {criteriaField += elem.filter;} );
              toAdd = true;
          }
          criteriaField += "] ]"
          if (toAdd) {
             if ( filterCriteria == "" ) {
                filterCriteria = criteriaField;
             } else {
                filterCriteria += ", " + criteriaField;
             }
          }
       });
       filterCriteria = "{'criteriaSearch': " + filterCriteria + "}"
       console.log(filterCriteria);
    }
    
  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    That'll work. It's going inside the DataTables object, so it may change in the future (tho unlikely), but yep, that'll do it!

    Colin

This discussion has been closed.