How to programmatically get search results in an array

How to programmatically get search results in an array

kbessemerkbessemer Posts: 16Questions: 3Answers: 1

When using Data Tables, if a user types a phrase in the search box, is there a way to programmatically get the results of the search in an array?

Answers

  • kthorngrenkthorngren Posts: 16,778Questions: 25Answers: 3,971

    Use rows().data() with the selector-modifier of { search: 'applied' } to get the filtered row data. Chain toArray() to turn the result into a JS array. For example:

    var data = table.rows( {search: 'applied'} ).data().toArray();
    

    Kevin

  • kbessemerkbessemer Posts: 16Questions: 3Answers: 1

    So this is working, somewhat. The behavior is off. The first time I run my function it returns an empty array, even though there is a search term and search results, but the second time I run the function it returns the proper array with the search results.

  • kthorngrenkthorngren Posts: 16,778Questions: 25Answers: 3,971
    edited August 9

    Without seeing what you are doing its hard to say. Maybe you need to use that code in an event like search. It really depends on what your goal is. Please provide a link to your page or a test case showing use what you are doing so we can offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • kbessemerkbessemer Posts: 16Questions: 3Answers: 1

    I am using ReactJS, not sure if this is causing a problem... I tried using a search event and it isn't updating the variable with the search term

    table.on( 'search.dt', function () {
    searchPhrase = table.search();
    } );

  • kbessemerkbessemer Posts: 16Questions: 3Answers: 1

    Honestly not sure how I would post my code in codepen since the react is JS and HTML combined

  • kthorngrenkthorngren Posts: 16,778Questions: 25Answers: 3,971

    I'm not familiar with ReactJS. Looks like I need to install something from codesandbox.io to run the test case. So I didn't try that yet.

    Here is a running example:
    http://live.datatables.net/ciwakuwu/1/edit

    Kevin

  • kbessemerkbessemer Posts: 16Questions: 3Answers: 1
    edited August 9

    I changed my code to:

    $(document).ready( function () {

    var table = $('#DeviceTable').DataTable();
    
    table.on('search.dt', function () {
      searchPhrase = table.search();
    });
    
    data = table.rows( {search: 'applied'} ).data().toArray();
    

    } );

    The search phrase is being passed to my other function properly... but after the function runs once the searchPhrase variable goes to undefined. And I have to type a phrase in the search box again.

    And the search results are behaving the same in the array, the first time the function runs it is getting a big list of results, the 2nd time I run it, it's getting the correct results array

  • kbessemerkbessemer Posts: 16Questions: 3Answers: 1

    Ok, I managed to get something working for my situation...

    I am not using the table.rows api for search results, that was not working for me.

    But I am using a search event, and updating a redux store variable with the search phrase, then reading that store variable when I need to.

  • kthorngrenkthorngren Posts: 16,778Questions: 25Answers: 3,971

    I am not using the table.rows api for search results, that was not working for me.

    What is the problem? Did you try it in the search event like I suggested? Or are you using it somewhere else? Maybe your code snippet isn't accurate to how/where you are suing it.

    Kevin

  • allanallan Posts: 56,837Questions: 1Answers: 9,033 Site admin

    I am not using the table.rows api for search results, that was not working for me.

    Like Kevin I'm confused by that. The rows() method is the way to do exactly what you want.

    Allan

Sign In or Register to comment.