Function to return values of filters applied using SearchBuilder

Function to return values of filters applied using SearchBuilder

2008jackson2008jackson Posts: 38Questions: 9Answers: 0

Greetings,

I am trying to list down the list of all filters applied using search builder and the query entered into the seach box. Im unable to find an API for that. Please guide.

At present i use the function below to return number of rows on PDF export and i assume a similar case should allow me to list down the search criteria alongside this.

messageTop: function() {
    var table = $('#table_id').DataTable();
    return 'Secondary Filter returned ' + table.rows({search: 'applied'}).count() + ' result(s)';
},

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    You can use searchBuilder.getDetails() to get the filters being applied - this example demonstrates that in action.

    To get the standard search filter, just call search() without any parameters.

    Colin

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0

    Hi colin, calling searchBuilder.getDetails() gives me [object Object].
    I would like to get a list like below.
    'Filter 1' eq 'Query 1'
    'Filter 2' eq 'Query 2'

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @2008jackson ,

    searchBuilder.getDetails() returns an object. Try passing that through JSON.stringify() as the example shows to get it as text.

    If you are still having issues can you please provide a test case. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Thanks,
    Sandy

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0
    edited March 2021

    Hi Sandy,

    JSON.stringify(table.searchBuilder.getDetails()) works perfect to see the JSON string however, this doesn't look good on a PDF.

    Currently the below is returned.
    {"criteria":[{"condition":"contains","data":"Registration","value":["aef"]}],"logic":"AND"}

    Would it be possible to show the filter parameters similar to below?
    'Registration' contains 'aef'

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    Yep, you can just print what you want from the object - the object has all the information you need.

    Colin

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0

    Appreciate the guidance Colin. How would i pick the information from this JSON below since columns Uploaded and Registration are both identified as "data"? Kindly guide.

    {
      "criteria": [
        {
          "condition": "=",
          "data": "Uploaded",
          "value": []
        },
        {
          "condition": "=",
          "data": "Registration",
          "value": [
            "A6EBQ"
          ]
        }
      ],
      "logic": "AND"
    }
    
  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @2008jackson ,

    criteria is an array so you use a number as an index to access the objects that it contains. criteria[0].data will give you "Uploaded" for example.

    Thanks,
    Sandy

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0

    My table has 8 columns and since there is a large possible combinations for search builder inputs, is there an option to pretty print this JSON string?

  • colincolin Posts: 15,143Questions: 1Answers: 2,586
    Answer ✓

    It's just an object containing the data, you can choose to display it anyway you want, with any format you want.

    Colin

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0

    I've managed to get my desired output using the code below. But a new issue is that the PDF export doesn't work until there is an input from the search builder. The PDF export button displays a spinner only. Console throws error "Uncaught TypeError: Cannot read property 'forEach' of undefined at makeQuery".

                var sbfilter = table.searchBuilder.getDetails();
                var query = '';
                function makeQuery(criteria, logic) {
                    criteria.forEach((c, idx, array) => {
                        if (c.criteria) {
                            query += '('
                            makeQuery(c.criteria, c.logic)
                            query += ')'
                        } else {
                            query += '(' + c.data + c.condition + c.value[0] + ')'
                        }
                        if (idx != array.length - 1) {
                            query += logic
                        }
                    })
                }
                makeQuery(sbfilter.criteria, sbfilter.logic);
    

    How do i skip to proceed to PDF export if no search builder input is made?

  • sandysandy Posts: 913Questions: 0Answers: 236
    Answer ✓

    Hi @2008jackson ,

    Just add a check to make sure that criteria is not undefined...

    if(criteria !== undefined){
        criteria.forEach((c, idx, array) => {
           ...
    

    Thanks,
    Sandy

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0

    Worked perfectly sandy. Thankyou. Case can be closed.

This discussion has been closed.