gpt4 book ai didi

javascript - 如何使用从 GitHub API 获取的数据动态填充我的表?

转载 作者:行者123 更新时间:2023-12-02 22:30:41 27 4
gpt4 key购买 nike

我正在学习如何从 API(即 GitHub)获取数据。我目前有一个像这样的表:

<!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>

我正在使用此函数执行获取:

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);
});
}

如何获取数据并在表格中默认只显示至少两行存储库?我想要实现的是将其余存储库保存到下拉选择中,然后动态加载所选存储库。

最佳答案

下面的代码片段

  • 获取 Github 存储库

  • 向表中添加两行(仅名称)

  • 将其余内容添加到下拉列表中

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>

关于javascript - 如何使用从 GitHub API 获取的数据动态填充我的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58911313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com