令牌分页

This commit is contained in:
CaIon 2023-11-03 22:47:55 +08:00
parent e06186fe0c
commit 3d87f868a3
2 changed files with 22 additions and 11 deletions

View File

@ -11,10 +11,16 @@ import (
func GetAllTokens(c *gin.Context) { func GetAllTokens(c *gin.Context) {
userId := c.GetInt("id") userId := c.GetInt("id")
p, _ := strconv.Atoi(c.Query("p")) p, _ := strconv.Atoi(c.Query("p"))
size, _ := strconv.Atoi(c.Query("size"))
if p < 0 { if p < 0 {
p = 0 p = 0
} }
tokens, err := model.GetAllUserTokens(userId, p*common.ItemsPerPage, common.ItemsPerPage) if size <= 0 {
size = common.ItemsPerPage
} else if size > 100 {
size = 100
}
tokens, err := model.GetAllUserTokens(userId, p*size, size)
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,

View File

@ -171,10 +171,11 @@ const TokensTable = () => {
}, },
]; ];
const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
const [showEdit, setShowEdit] = useState(false); const [showEdit, setShowEdit] = useState(false);
const [tokens, setTokens] = useState([]); const [tokens, setTokens] = useState([]);
const [selectedKeys, setSelectedKeys] = useState([]); const [selectedKeys, setSelectedKeys] = useState([]);
const [tokenCount, setTokenCount] = useState(ITEMS_PER_PAGE); const [tokenCount, setTokenCount] = useState(pageSize);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [activePage, setActivePage] = useState(1); const [activePage, setActivePage] = useState(1);
const [searchKeyword, setSearchKeyword] = useState(''); const [searchKeyword, setSearchKeyword] = useState('');
@ -194,24 +195,24 @@ const TokensTable = () => {
const setTokensFormat = (tokens) => { const setTokensFormat = (tokens) => {
setTokens(tokens); setTokens(tokens);
if (tokens.length >= ITEMS_PER_PAGE) { if (tokens.length >= pageSize) {
setTokenCount(tokens.length + ITEMS_PER_PAGE); setTokenCount(tokens.length + pageSize);
} else { } else {
setTokenCount(tokens.length); setTokenCount(tokens.length);
} }
} }
let pageData = tokens.slice((activePage - 1) * ITEMS_PER_PAGE, activePage * ITEMS_PER_PAGE); let pageData = tokens.slice((activePage - 1) * pageSize, activePage * pageSize);
const loadTokens = async (startIdx) => { const loadTokens = async (startIdx) => {
setLoading(true); setLoading(true);
const res = await API.get(`/api/token/?p=${startIdx}`); const res = await API.get(`/api/token/?p=${startIdx}&size=${pageSize}`);
const {success, message, data} = res.data; const {success, message, data} = res.data;
if (success) { if (success) {
if (startIdx === 0) { if (startIdx === 0) {
setTokensFormat(data); setTokensFormat(data);
} else { } else {
let newTokens = [...tokens]; let newTokens = [...tokens];
newTokens.splice(startIdx * ITEMS_PER_PAGE, data.length, ...data); newTokens.splice(startIdx * pageSize, data.length, ...data);
setTokensFormat(newTokens); setTokensFormat(newTokens);
} }
} else { } else {
@ -222,7 +223,7 @@ const TokensTable = () => {
const onPaginationChange = (e, {activePage}) => { const onPaginationChange = (e, {activePage}) => {
(async () => { (async () => {
if (activePage === Math.ceil(tokens.length / ITEMS_PER_PAGE) + 1) { if (activePage === Math.ceil(tokens.length / pageSize) + 1) {
// In this case we have to load more data and then append them. // In this case we have to load more data and then append them.
await loadTokens(activePage - 1); await loadTokens(activePage - 1);
} }
@ -327,7 +328,7 @@ const TokensTable = () => {
.catch((reason) => { .catch((reason) => {
showError(reason); showError(reason);
}); });
}, []); }, [pageSize]);
const removeRecord = key => { const removeRecord = key => {
let newDataSource = [...tokens]; let newDataSource = [...tokens];
@ -417,7 +418,7 @@ const TokensTable = () => {
const handlePageChange = page => { const handlePageChange = page => {
setActivePage(page); setActivePage(page);
if (page === Math.ceil(tokens.length / ITEMS_PER_PAGE) + 1) { if (page === Math.ceil(tokens.length / pageSize) + 1) {
// In this case we have to load more data and then append them. // In this case we have to load more data and then append them.
loadTokens(page - 1).then(r => { loadTokens(page - 1).then(r => {
}); });
@ -452,10 +453,14 @@ const TokensTable = () => {
<Table style={{marginTop: 20}} columns={columns} dataSource={pageData} pagination={{ <Table style={{marginTop: 20}} columns={columns} dataSource={pageData} pagination={{
currentPage: activePage, currentPage: activePage,
pageSize: ITEMS_PER_PAGE, pageSize: pageSize,
total: tokenCount, total: tokenCount,
showSizeChanger: true, showSizeChanger: true,
pageSizeOptions: [10, 20, 50, 100], pageSizeOptions: [10, 20, 50, 100],
onPageSizeChange: (size) => {
setPageSize(size);
setActivePage(1);
},
onPageChange: handlePageChange, onPageChange: handlePageChange,
}} loading={loading} rowSelection={rowSelection}> }} loading={loading} rowSelection={rowSelection}>
</Table> </Table>