How can I make column().search() execute a function if no results found?

How can I make column().search() execute a function if no results found?

henrymzhenrymz Posts: 3Questions: 1Answers: 0

I am trying to use the built in .search() function to do some custom searching, and I would like to know if the .search() function have found any results without calling .search().draw()

Is there any way for me to make the .search() function execute something like console.log('no results found')?

Answers

  • henrymzhenrymz Posts: 3Questions: 1Answers: 0
    edited August 2017

    I've tried theTable.column(1, {search: 'personname'}).any(), but it seems to always return true no matter what I search.

  • henrymzhenrymz Posts: 3Questions: 1Answers: 0
    edited August 2017

    Nevermind I figured it out after reading a similar question here:

    https://datatables.net//forums/discussion/comment/76396/#Comment_76396

    otable.column(1).search('sdlkfjsdfj').page.info().recordsDisplay will display how many records it found, 0 for none.

    EDIT: Seems like as per question linked above, user need to call .draw() before a new search can take effect. I don't want to update the table during my custom search, is there any way around this?

    Can I discard my last search, then perform a new search? Is my only solution to do: theTable.search( '' ).columns().search( '' ).draw();? Primarily I don't want to update the table with a .draw(), but also I feel doing 2 blank searches is unnecessarily resource intensive.

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Are you basically trying to find if a column contains some value? If so the filter() method along with count() might be the best way to do it:

    theTable
      .column(1)
      .data()
      .filter( function ( val ) {
        return val === searchTerm;
      } )
      .count();
    

    Allan

This discussion has been closed.