问题描述
我正在学习如何从 API(即 GitHub)获取数据.我目前有一张这样的桌子:
<!DOCTYPE html><html><头><title>GitHub API Fetch</title><风格>桌子,th,td {边框:1px纯黑色;边框折叠:折叠;}th,td {填充:5px;}</风格><script src="activity2.js"></script></头><身体>输入有效的 GitHub 用户 ID:<输入类型="文本" id="uid"><button>获取详细信息</button><br><br><table id="gitTable"><头><tr><th>存储库<br>名称:</th><th>时间戳:<br>创建 &<br>更新</th><th>尺寸</th><th>分叉数<br>分叉数</th><th>数量<br><br>open<br>问题</th><th>HTML URL</th><th>语言列表<br>使用的语言和 URL</th><th>下载</th><th>分店</th></tr></头><tbody></tbody></表>请选择第三行:<选择 onchange=""></选择></div><button onclick="">刷新</button></div></身体></html>我正在使用此功能执行提取:
函数库(用户名){fetch(`https://api.github.com/users/${username}/repos`).then((response) => {如果(响应状态!== 200){console.log('看起来有问题.状态码:' + response.status);返回;}response.json().then((数据) => {//用至少 2 个 repos 填充表并将剩余部分保存到选择下拉列表中});}).catch((错误) => {console.log('获取错误:-S', err);});}
如何获取数据并在表中默认仅显示至少两行存储库?我想要实现的是将剩余的存储库保存到下拉选择中,然后动态加载选定的存储库.
解决方案 下面的代码片段
获取 Github 存储库
向表中添加两行(仅限名称)
将其余部分添加到下拉列表中
function repositories(username) {return fetch(`https://api.github.com/users/${username}/repos`).then((响应) => {返回响应.json()}).then(json => {返回 json}).catch((错误) => {console.log('获取错误:-S', err);});}const getRepos = async(用户名) =>{const ret = 等待存储库(用户名)返回 ret}(异步函数(){const repos = await getRepos('gegeke')//现在您在 repos const 中拥有了存储库 - 从这里开始,//你可以使用它//console.log('getRepos:', repos)//解构 repos 数组//rep1 - 第一个元素//rep2 - 第二个元素//repRest - 其余元素const [rep1, rep2, ...repRest] = reposaddTwoRows([rep1, rep2])addSelectOptions(repRest)})();const addTwoRows = (rows) =>{rows.forEach(e => {const tbody = document.querySelector('#gitTable tbody')const tr = document.createElement('tr')tr.innerHTML = rowHtml(e)tbody.appendChild(tr)})}常量 rowHtml = 行 =>{html = ''html += `<td>${row.name}</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>`返回 html}const addSelectOptions = (arr) =>{常量下拉 = document.getElementById('selectDD')dropdown.innerHTML = selectHtml(arr)}const selectHtml = arr =>{return arr.map(e => `<option>${e.name}</option>`).join('')}
表,th,td {边框:1px纯黑色;边框折叠:折叠;}th,td {填充:5px;}
输入有效的 GitHub 用户 ID:<输入类型="文本" id="uid"><button>获取详细信息</button><br><br><table id="gitTable"><头><tr><th>存储库<br>名称:</th><th>时间戳:<br>创建 &<br>更新</th><th>尺寸</th><th>分叉数<br>分叉数</th><th>数量<br><br>open<br>问题</th><th>HTML URL</th><th>语言列表<br>使用的语言和 URL</th><th>下载</th><th>分店</th></tr></头><tbody></tbody></表>请选择第三行:<选择id="selectDD" onchange=""></选择></div><button onclick="">刷新</button></div>I'm learning how to fetch data from an API, namely GitHub. I currently have a table like so:
<!DOCTYPE html>
<html>
<head>
<title>GitHub API Fetch</title>
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 5px;
}
</style>
<script src="activity2.js"></script>
</head>
<body>
Enter a valid GitHub user id:
<input type="text" id="uid">
<button>Get Details</button>
<br>
<br>
<table id="gitTable">
<thead>
<tr>
<th>Repository<br>Name:</th>
<th>Timestamps:<br>Created &<br>Updated</th>
<th>Size</th>
<th>Number<br>of forks</th>
<th>Number<br>of<br>open<br>issues</th>
<th>HTML URL</th>
<th>List of Languages<br>Used and URL</th>
<th>Downloads</th>
<th>Branches</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div>
Please select a third row :
<select onchange="">
</select>
</div>
<div>
<button onclick="">Refresh</button>
</div>
</body>
</html>
And I am performing a fetch with this function:
function repositories(username) {
fetch(`https://api.github.com/users/${username}/repos`).then((response) => {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
}
response.json().then((data) => {
// populate table with a minimum of 2 repos and save remainder into selection dropdown
});
}).catch((err) => {
console.log('Fetch Error :-S', err);
});
}
How can I take the data and only show a minimum of two rows of repositories by default in the table? What I want to achieve then is to then save the remainder of repositories into the dropdown selection, which would then dynamically load the selected repository.
解决方案 The snippet below
fetches the Github repo
adds two rows to the table (names only)
adds the rest to the dropdown
function repositories(username) {
return fetch(`https://api.github.com/users/${username}/repos`)
.then((response) => {
return response.json()
})
.then(json => {
return json
})
.catch((err) => {
console.log('Fetch Error :-S', err);
});
}
const getRepos = async(username) => {
const ret = await repositories(username)
return ret
}
(async function() {
const repos = await getRepos('gegeke')
// now you have the repositories in the repos const - from here,
// you can work with it
// console.log('getRepos:', repos)
// destructuring the repos array
// rep1 - first element
// rep2 - second element
// repRest - rest of elements
const [rep1, rep2, ...repRest] = repos
addTwoRows([rep1, rep2])
addSelectOptions(repRest)
})();
const addTwoRows = (rows) => {
rows.forEach(e => {
const tbody = document.querySelector('#gitTable tbody')
const tr = document.createElement('tr')
tr.innerHTML = rowHtml(e)
tbody.appendChild(tr)
})
}
const rowHtml = row => {
html = ''
html += `<td>${row.name}</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>`
return html
}
const addSelectOptions = (arr) => {
const dropdown = document.getElementById('selectDD')
dropdown.innerHTML = selectHtml(arr)
}
const selectHtml = arr => {
return arr.map(e => `<option>${e.name}</option>`).join('')
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 5px;
}
Enter a valid GitHub user id:
<input type="text" id="uid">
<button>Get Details</button>
<br>
<br>
<table id="gitTable">
<thead>
<tr>
<th>Repository<br>Name:</th>
<th>Timestamps:<br>Created &<br>Updated</th>
<th>Size</th>
<th>Number<br>of forks</th>
<th>Number<br>of<br>open<br>issues</th>
<th>HTML URL</th>
<th>List of Languages<br>Used and URL</th>
<th>Downloads</th>
<th>Branches</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div>
Please select a third row :
<select id="selectDD" onchange="">
</select>
</div>
<div>
<button onclick="">Refresh</button>
</div>
这篇关于如何使用从 GitHub API 获取数据动态填充我的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13
前端开发问题
136
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22
前端开发问题
244
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14
前端开发问题
156
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序




大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)