Browse Source

Add sorting for ranklist.

master
t123yh 7 years ago
parent
commit
6744d462aa
  1. 12
      modules/user.js
  2. 28
      views/ranklist.ejs

12
modules/user.js

@ -24,13 +24,21 @@ let User = syzoj.model('user');
// Ranklist // Ranklist
app.get('/ranklist', async (req, res) => { app.get('/ranklist', async (req, res) => {
try { try {
const sort = req.query.sort || "id";
const order = req.query.order || "asc";
console.log("SORT ===> " + sort + ", ORDER ===> " + order);
if (!['ac_num', 'rating', 'id', 'username'].includes(sort) || !['asc', 'desc'].includes(order)) {
throw new ErrorMessage('错误的排序参数。');
}
let paginate = syzoj.utils.paginate(await User.count({ is_show: true }), req.query.page, syzoj.config.page.ranklist); let paginate = syzoj.utils.paginate(await User.count({ is_show: true }), req.query.page, syzoj.config.page.ranklist);
let ranklist = await User.query(paginate, { is_show: true }, [['ac_num', 'desc']]); let ranklist = await User.query(paginate, { is_show: true }, [[sort, order]]);
await ranklist.forEachAsync(async x => x.renderInformation()); await ranklist.forEachAsync(async x => x.renderInformation());
res.render('ranklist', { res.render('ranklist', {
ranklist: ranklist, ranklist: ranklist,
paginate: paginate paginate: paginate,
curSort: sort,
curOrder: order === 'asc'
}); });
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);

28
views/ranklist.ejs

@ -1,5 +1,23 @@
<% this.title = '排名' %> <% this.title = '排名' %>
<% include header %> <% include header %>
<%
const getOrderString = function(order) {
return order ? 'asc' : 'desc';
}
const createSortableTitle = function(item, display, defaultOrder) {
const isCurrent = curSort === item;
console.log("IScurrent: ========> " + isCurrent);
const url = syzoj.utils.makeUrl(['ranklist'],
{
sort: item,
order: getOrderString(isCurrent ? (!curOrder) : defaultOrder)
});
const triangle = isCurrent ? `<i class="${curOrder ? "angle up" : "angle down"} icon"></i>` : "";
return `<a href="${url}">${display}${triangle}</a>`;
}
%>
<div class="padding"> <div class="padding">
<form action="<%= syzoj.utils.makeUrl(['find_user']) %>" class="ui mini form" method="get" role="form" style="margin-bottom: 25px; text-align: right; "> <form action="<%= syzoj.utils.makeUrl(['find_user']) %>" class="ui mini form" method="get" role="form" style="margin-bottom: 25px; text-align: right; ">
<div class="ui action left icon input inline" style="width: 180px; margin-right: 77px; "> <div class="ui action left icon input inline" style="width: 180px; margin-right: 77px; ">
@ -10,10 +28,12 @@
<table class="ui very basic center aligned table" style="table-layout: fixed; "> <table class="ui very basic center aligned table" style="table-layout: fixed; ">
<thead> <thead>
<tr> <tr>
<th style="width: 80px; ">#</th> <th style="width: 60px; ">#</th>
<th style="width: 170px; ">用户名</th> <th style="width: 60px; "><%- createSortableTitle('id', 'ID', true) %></th>
<th style="width: 150px; "><%- createSortableTitle('username', '用户名', true) %></th>
<th>个性签名</th> <th>个性签名</th>
<th style="width: 100px; ">通过数量</th> <th style="width: 100px; "><%- createSortableTitle('ac_num', '通过数量', false) %></th>
<th style="width: 100px; "><%- createSortableTitle('rating', 'Rating', false) %></th>
</tr> </tr>
</thead> </thead>
<script> <script>
@ -36,6 +56,7 @@
%> %>
<tr> <tr>
<td><%= i %></td> <td><%= i %></td>
<td><%= user.id %></td>
<td><a href="<%= syzoj.utils.makeUrl(['user', user.id]) %>"><%= user.username %></a><% if (user.nameplate) { %><%- user.nameplate %><% } %></td> <td><a href="<%= syzoj.utils.makeUrl(['user', user.id]) %>"><%= user.username %></a><% if (user.nameplate) { %><%- user.nameplate %><% } %></td>
<td class="font-content"> <td class="font-content">
<script id="user-infomation-script-<%= i %>"> <script id="user-infomation-script-<%= i %>">
@ -53,6 +74,7 @@
</script> </script>
</td> </td>
<td><%= user.ac_num %></td> <td><%= user.ac_num %></td>
<td><%= user.rating %></td>
</tr> </tr>
<% } %> <% } %>
</tbody> </tbody>

Loading…
Cancel
Save