[英]How to get a number of items in the list
I have got a search function and I would like to display no results
, if the query didn't return any items.我有一个搜索功能,如果查询没有返回任何项目,我想不显示no results
。 The problem is, that I am not sure how to get the number or items, that are currently displaying.问题是,我不确定如何获取当前显示的数字或项目。
Here's my function:这是我的功能:
find.addEventListener("keyup", function() {
Array.from(animalList.children).forEach(d => {
if (d.dataset.animalName.toLowerCase().indexOf(this.value.toLowerCase()) != -1) {
d.style.display = "block";
} else {
d.style.display = "none";
}
});
});
You could work with a boolean flag:您可以使用布尔标志:
find.addEventListener("keyup", function() {
let noResults = true;
Array.from(animalList.children).forEach(d => {
if (d.dataset.animalName.toLowerCase().indexOf(this.value.toLowerCase()) != -1) {
d.style.display = "block";
noResults = false;
} else {
d.style.display = "none";
}
});
if (noResults) {
// display no results
}
});
Since you are working with javascript and key event and you told us you want to show some message string upon some list filtering, my suggestion is:由于您正在使用 javascript 和关键事件,并且您告诉我们您想在某些列表过滤时显示一些消息字符串,因此我的建议是:
Show the no result message at function start, then filter and if you get results do the respective functionality.在函数启动时显示无结果消息,然后过滤,如果得到结果,则执行相应的功能。
find.addEventListener("keyup", function() {
// show no result message and then do the other stuff,
// this is a UI event handler after all
Array.from(animalList.children).forEach(d => {
if (d.dataset.animalName.toLowerCase().indexOf(this.value.toLowerCase()) != -1) {
d.style.display = "block";
} else {
d.style.display = "none";
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.