javascript - 带有 API 的 Javascript 过滤表
问题描述
所以我试图在从 api 获取后过滤表中的数据,但它没有用。这是我的代码示例,有人可以帮助我使用 javascript 从表中过滤数据。谢谢
JSON占位符
<div class="container">
<div class="row">
<div class="col l3" id="filternav">
<a class="black-text"><h6 id="getUsers">Users</h6></a>
</div>
<div class="col l9" id="output">
<h1 class="center-align">No Data</h1>
</div>
</div>
</div>
</body>
这是我的脚本
<script>
document.getElementById('getUsers').addEventListener('click', getUsers);
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
function getUsers(){
fetch('https://jsonplaceholder.typicode.com/Users')
.then((res) => res.json())
.then((data) => {
let output = '<h4>Users</h4><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">';
data.forEach(function(user){
output += `
<table id="myTable">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
</tr>
`;
});
document.getElementById('output').innerHTML = output;
})
}
</script>
解决方案
您将获得td
每个中的第一个tr
,应该是第二个。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<div class="row">
<div class="col l3" id="filternav">
<a class="black-text">
<h6 id="getUsers">Users</h6>
</a>
</div>
<div class="col l9" id="output">
<h1 class="center-align">No Data</h1>
</div>
</div>
</div>
</body>
<script>
document.getElementById('getUsers').addEventListener('click', getUsers);
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[1]; // use 1 instead of 0
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
function getUsers() {
fetch('https://jsonplaceholder.typicode.com/Users')
.then((res) => res.json())
.then((data) => {
let output =
'<h4>Users</h4><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name"><table id="myTable">';
data.forEach(function (user) {
output +=
`
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
</tr>
`;
});
output += '</table>'
document.getElementById('output').innerHTML = output;
})
}
</script>
</body>
</html>
推荐阅读
- c++ - 在 Visual Studio 2017 中使用库设置 vc++ 项目的正确方法
- c++ - C++ - 为什么是 std::function
无效的? - python - Python - ConnectionRefusedError、urllib3.exceptions.NewConnectionError 和 Colorama
- python - dlib-build@0.1.1 安装失败
- c++ - string to double 或 float 截断小数点
- php - .htaccess 重写会留下损坏的图像
- python - PyQt4:如何使文件对话框弹出并停留?
- c# - 密封类的类实例化的依赖注入
- javascript - 如何在后台获取 YouTube 网址?
- c-strings - 为什么我必须在 C++ 中包含 .c_str() 来查找字符串长度?