我正在学习如何从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);
});
}
如何获取数据,并且默认情况下在表中仅显示最少两行存储库?然后,我要实现的是将其余存储库保存到下拉选择中,然后动态加载选定的存储库。
答案 0 :(得分:0)
下面的代码段
获取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>