简体   繁体   English

在 Jquery 搜索过滤器中找不到结果时如何显示默认消息

[英]How to display default message when no results found on Jquery search filter

I'm currently building a page where a search field acts as a filter.我目前正在构建一个页面,其中搜索字段充当过滤器。 It works perfectly fine and shows data against related word, but there is one issue that I'd like to solve.它工作得很好,并根据相关词显示数据,但我想解决一个问题。 When the entered string or other words is not found in all the existing the page remains blank.当在所有现有页面中找不到输入的字符串或其他单词时,页面将保持空白。

How could I display a default message on my page when no results are found by the filter?当过滤器没有找到结果时,如何在我的页面上显示默认消息? Something like a simple <p> explaining that no results were found .类似于简单的<p>解释no results were found

The idea is to display it only once as long as the string is not found.这个想法是只要找不到字符串就只显示一次。

$('#search_field').on('keyup', function() {
  var value = $(this).val();
  var patt = new RegExp(value, "i");

  $('#userFind').find('tr').each(function() {
    var $table = $(this);

    if (!($table.find('td').text().search(patt) >= 0)) {
      $table.not('.t_head').hide();
    }
    
    if (($table.find('td').text().search(patt) >= 0)) {
      $(this).show();
    }
  });
});

This is untested since you haven't provided any table to your question.这是未经测试的,因为您没有为您的问题提供任何表格。

After you have looped though all tr, then check if any is visible.循环遍历所有 tr 后,检查是否可见。 If not then append a tr with custom message and remove it and new search.如果没有,则附加一个带有自定义消息的 tr 并将其删除并进行新搜索。

$('#search_field').on('keyup', function() {
  var value = $(this).val();
  // console.log(value);
  var patt = new RegExp(value, "i");
  $(".noRecord").remove();
  $('#userFind').find('tr').each(function() {
    var $table = $(this);

    if (!($table.find('td').text().search(patt) >= 0)) {
      $table.not('.t_head').hide();

    } else {
      $(this).show();
    }

  });
  
  if ($('#userFind tr:visible').length == 0) {
    $('#userFind tbody').append("<tr class='noRecord'><td>No records found.</td></tr>")
  }

});

Assuming you have a div:假设你有一个 div:

<div id="zeroHits">no results were found</div>

You can hide/show the #zeroHits div as follows:您可以隐藏/显示#zeroHits div,如下所示:

$('#search_field').on('keyup', function() {
  var value = $(this).val();
  var patt = new RegExp(value, "i");
  var zeroHits = true;
  $('#userFind').find('tr').each(function() {
    var $table = $(this);

    if (!($table.find('td').text().search(patt) >= 0)) {
      $table.not('.t_head').hide();
    }
    
    if (($table.find('td').text().search(patt) >= 0)) {
      $(this).show();
      zeroHits = false;
    }
  });
  if(zeroHits) {
    $('#zeroHits').show();
  } else {
    $('#zeroHits').hide();
  }
});

Try this untested code试试这个未经测试的代码

post your HTML and I can test发布您的 HTML,我可以测试

const $rows = $('#userFind tbody tr'); // only tbody rows
$('#search_field').on('keyup', function() {
  var value = $(this).val();
  // console.log(value);
  var patt = new RegExp(value, "i");
  $rows.each(function() {
    const found = $(this).find('td').filter(function() {
      return $(this).text().search(patt) != -1
    }).length > 0
    $(this).toggle(found);
  });
  $("#notFound").toggle($rows.filter(":visible").length === 0)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM