javascript - 使用 JQuery 从 API 获取和显示数据
问题描述
所以伙计们,我需要获取以下 API 数据:https ://api.github.com并将其放在 HTML 文件中。
所以这是我正在编写的 JavaScript 代码:
async function getData()
{
//await the response of the fetch call
let response = await fetch('https://api.github.com');
//proceed once the first promise is resolved.
let data = await response.json()
//proceed only when the second promise is resolved
let newData = data.results;
return newData;
}
//call getData function
getData()
.then(function(result){
$.each(result, (index, user) => {
$('#character').append(`
<div class='card'>
<img
src=${user.image}
alt=''
class='round-img'
/>
<h4 class='card-name'>${user.name}</h4>
<input id="collapsible" class="toggle" type="checkbox">
<label for="collapsible" class="lbl-toggle">
<i class='fas fa-chevron-down'> </i>
</label>
</input>
</div>
`)
});
})
因此,我正在获取用户信息,但我需要将其显示在每页用户数量有限的列表中。我怎样才能做到这一点?
解决方案
如果您可以使用外部库,那么 Data Table 是一个很好的库,它将为您提供搜索、过滤、排序和分页功能。您只需要创建表并将该表的 id 传递给数据表库。它将照顾您的要求。您可以设置每页的最大项目数。
https://datatables.net/examples/basic_init/zero_configuration.html
推荐阅读
- r - 尝试从网站读取 excel 文件时出错
- python - numba 并行 njit 编译不适用于 np.isnan()
- asp.net-core - 尝试使用 protobuf-net 通过 gRPC-Web 进行 Azure AD 身份验证
- double-click - 如何从 Konva 的 'dblclick' 事件中获取 Konva 标签的 ID
- javascript - 如何使用 jest 模拟原始 const 值并恢复模拟?
- sml - 为什么我的 sml 代码中出现这些错误?
- docker - 为什么这个卷被安装为只读?
- html - Flexbox 之间不显示空格
- php - 将 id 连接到控制器内的 laravel 请求
- swift - 使用 Firestore 获取数据的问题