Skip to content

Commit

Permalink
feat: 增加followers和following排序; (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuo authored Apr 14, 2024
1 parent 45e04d3 commit b5f32be
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 28 deletions.
66 changes: 63 additions & 3 deletions template/_partial/users_nav.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
<span class="select">
<a class="<%= type === 'global' ? 'active': '' %>" href="index.html">Global</a>
<a class="<%= type === 'china' ? 'active': '' %>" href="users.china.html">China</a>
<a class="<%= type === 'global' ? 'active': '' %>" href="index.html"
>Global</a
>
<a class="<%= type === 'china' ? 'active': '' %>" href="users.china.html"
>China</a
>
<a class="<%= type === 'org' ? 'active': '' %>" href="org.html">Org</a>
</span>
</span>

<script>
document.addEventListener("DOMContentLoaded", function () {
// 初始变量
let currentSortColumn = 0; // 初始排序列
const ths = Array.from(document.querySelectorAll("table thead tr th"));
let ascendingOrder = true; // 初始排序顺序
// 事件监听器
const headers = document.querySelectorAll("th[data-sort]");
headers.forEach((header) => {
header.addEventListener("click", (e) => {
// 获取点击列的位置
currentSortColumn = ths.findIndex(
(item) => item.dataset.sort === e.target.dataset.sort
);
// 调用排序函数
sortTable();
});
});
// 排序函数
function sortTable() {
const rows = document.querySelectorAll("tbody tr");
const sortedRows = Array.from(rows).sort((rowA, rowB) => {
const valueA = parseInt(
rowA.querySelectorAll("td")[currentSortColumn].innerText
);
const valueB = parseInt(
rowB.querySelectorAll("td")[currentSortColumn].innerText
);
if (ascendingOrder) {
return valueA - valueB;
} else {
return valueB - valueA;
}
});
// 清空表格并重新添加排好序的行
const tbody = document.querySelector("tbody");
tbody.innerHTML = "";
sortedRows.forEach((row) => {
tbody.appendChild(row);
});
// 切换排序顺序
ascendingOrder = !ascendingOrder;
}
});
</script>
<style>
th[data-sort] {
cursor: pointer;
}
</style>
4 changes: 2 additions & 2 deletions template/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<th>login</th>
<th>location</th>
<th>company</th>
<th>followers</th>
<th>following</th>
<th data-sort="followers">followers</th>
<th data-sort="following">following</th>
<th>repos</th>
<th>created at</th>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions template/org.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<th>login</th>
<th>location</th>
<th>company</th>
<th>followers</th>
<th>following</th>
<th data-sort="followers">followers</th>
<th data-sort="following">following</th>
<th>repos</th>
<th>created at</th>
</tr>
Expand Down
42 changes: 21 additions & 21 deletions template/users.china.ejs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<%- include('./_partial/header', { tabCls: 'users', type: 'china', title: 'GitHub China User Ranking' }); %>
<%- include('./_partial/header', { tabCls: 'users', type: 'china', title:
'GitHub China User Ranking' }); %>
<div class="userlist">
<%- include('./_partial/users_nav', { type: 'china' }); %>
<table>
<thead>
<tr>
<th title="avatar">ava</th>
<th>login</th>
<th>location</th>
<th>company</th>
<th>followers</th>
<th>following</th>
<th>repos</th>
<th>created at</th>
</tr>
</thead>
<tbody>
<% USERS_CHINA.forEach(function(user, idx) {%>
<%- include('./_partial/users_list', { user, idx }); %>
<% }); %>
</tbody>
</table>
<table>
<thead>
<tr>
<th title="avatar">ava</th>
<th>login</th>
<th>location</th>
<th>company</th>
<th data-sort="followers">followers</th>
<th data-sort="following">following</th>
<th>repos</th>
<th>created at</th>
</tr>
</thead>
<tbody>
<% USERS_CHINA.forEach(function(user, idx) {%> <%-
include('./_partial/users_list', { user, idx }); %> <% }); %>
</tbody>
</table>
</div>
<%- include('./_partial/footer', { }); %>
<%- include('./_partial/footer', { }); %>

0 comments on commit b5f32be

Please sign in to comment.