如何使用从 GitHub API 获取的数据动态填充我的 table?
How can I dynamically populate my table with fetch data from GitHub API?
我正在学习如何从 API 中获取数据,即 GitHub。我目前有一个 table 像这样:
<!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);
});
}
如何获取数据并在table中默认只显示最少两行存储库?然后我想要实现的是将剩余的存储库保存到下拉选择中,然后动态加载选定的存储库。
下面的片段
获取 Github 存储库
向 table 添加两行(仅限名称)
将其余部分添加到下拉列表
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>
我正在学习如何从 API 中获取数据,即 GitHub。我目前有一个 table 像这样:
<!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);
});
}
如何获取数据并在table中默认只显示最少两行存储库?然后我想要实现的是将剩余的存储库保存到下拉选择中,然后动态加载选定的存储库。
下面的片段
获取 Github 存储库
向 table 添加两行(仅限名称)
将其余部分添加到下拉列表
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>