isethi
isethi

Reputation: 755

Searching a html table with js but show if no results

Here is an example js to search through and filter the results. http://jsfiddle.net/dfsq/7BUmG/1133/

How is it possible implement something like this?

if(results == null){
   console.log("no results");
}

Upvotes: 0

Views: 919

Answers (3)

charlietfl
charlietfl

Reputation: 171669

Use a variable to store the filter() results and check it's length against all rows

var $hidden = $rows.show().filter(function() {
    text = $(this).text().replace(/\s+/g, ' ');
    return !reg.test(text);
}).hide();

$('#no-results').toggle($hidden.length === $rows.length)

DEMO

Upvotes: 1

Our_Benefactors
Our_Benefactors

Reputation: 3539

Here is a working example, it checks if any rows are visible after searching and hides/shows a "no results" message based on that.

if ($rows.find(':visible').length === 0) {
  $("#noResults").show();
}
else{
  $("#noResults").hide();
}

http://jsfiddle.net/asrabhdn/

Upvotes: 0

Maarten van Tjonger
Maarten van Tjonger

Reputation: 1927

Try:

    if ($rows.find(':visible').length === 0) {
        console.log('no results');
    }

Upvotes: 3

Related Questions