Compare commits

...
3 Commits
Author SHA1 Message Date
hz4th_coder 6dabc4d0b7 优化内容库分页
- 默认每页20条
- 列表上部也添加分页控件,方便翻页
2026-07-14 00:54:14 +08:00
hz4th_coder 981b3c9c5c 修复内容库分页:返回总数而非当前页数量
- 新增 get_articles_count 函数获取总数
- 列表API返回总数用于分页计算
2026-07-14 00:50:11 +08:00
hz4th_coder 127654a558 修复搜索分页功能
- 后端搜索API支持offset参数,返回总数用于分页
- 前端传递offset参数实现正确分页
2026-07-14 00:40:40 +08:00
5 changed files with 50 additions and 12 deletions
+8
View File
@@ -198,6 +198,14 @@ class Database:
cursor.execute('DELETE FROM articles WHERE id = ?', (article_id,)) cursor.execute('DELETE FROM articles WHERE id = ?', (article_id,))
conn.commit() conn.commit()
return cursor.rowcount > 0 return cursor.rowcount > 0
def get_articles_count(self):
"""获取文章总数"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) as count FROM articles')
row = cursor.fetchone()
return row['count'] if row else 0
# ========== 待处理产品操作 ========== # ========== 待处理产品操作 ==========
def add_pending_product(self, product_name, category=None, subcategory=None, priority=0, source='manual'): def add_pending_product(self, product_name, category=None, subcategory=None, priority=0, source='manual'):
+11 -6
View File
@@ -14,6 +14,7 @@ def list_articles():
offset = request.args.get('offset', 0, type=int) offset = request.args.get('offset', 0, type=int)
articles = db.get_all_articles(limit=limit, offset=offset) articles = db.get_all_articles(limit=limit, offset=offset)
total_count = db.get_articles_count() # 获取总数
# 解析JSON字段 # 解析JSON字段
for article in articles: for article in articles:
@@ -23,7 +24,7 @@ def list_articles():
return jsonify({ return jsonify({
'success': True, 'success': True,
'articles': articles, 'articles': articles,
'count': len(articles) 'count': total_count # 返回总数
}) })
@bp.route('/search', methods=['GET']) @bp.route('/search', methods=['GET'])
@@ -31,16 +32,20 @@ def search_articles():
"""搜索文章""" """搜索文章"""
keyword = request.args.get('q', '') keyword = request.args.get('q', '')
category = request.args.get('category') category = request.args.get('category')
limit = request.args.get('limit', type=int) # 支持limit参数 limit = request.args.get('limit', type=int)
offset = request.args.get('offset', 0, type=int)
if not keyword: if not keyword:
return jsonify({'error': '请提供搜索关键词'}), 400 return jsonify({'error': '请提供搜索关键词'}), 400
articles = db.search_articles(keyword, category) all_articles = db.search_articles(keyword, category)
total_count = len(all_articles)
# 如果有limit参数,截取 # 分页截取
if limit: if limit:
articles = articles[:limit] articles = all_articles[offset:offset + limit]
else:
articles = all_articles
# 解析JSON字段 # 解析JSON字段
for article in articles: for article in articles:
@@ -50,7 +55,7 @@ def search_articles():
return jsonify({ return jsonify({
'success': True, 'success': True,
'articles': articles, 'articles': articles,
'count': len(articles) 'count': total_count # 返回总数,用于分页
}) })
@bp.route('/<int:article_id>', methods=['GET']) @bp.route('/<int:article_id>', methods=['GET'])
+13
View File
@@ -263,6 +263,19 @@
color: #4caf50; color: #4caf50;
} }
/* 上部分页 */
.pagination-top {
display: flex;
align-items: center;
gap: 10px;
margin-left: auto;
}
.pagination-top span {
font-size: 14px;
color: #666;
}
.article-summary { .article-summary {
font-size: 14px; font-size: 14px;
color: #666; color: #666;
+10 -4
View File
@@ -4,7 +4,7 @@ const API_BASE = '';
// 状态 // 状态
let articles = []; let articles = [];
let currentPage = 1; let currentPage = 1;
let pageSize = 100; let pageSize = 20; // 默认每页20条
let totalCount = 0; let totalCount = 0;
let selectedIds = new Set(); let selectedIds = new Set();
let currentArticleId = null; let currentArticleId = null;
@@ -49,10 +49,10 @@ async function loadArticles() {
}); });
if (keyword) { if (keyword) {
// 搜索时也传递 limit 参数 // 搜索时也传递 limit 和 offset 参数
let searchUrl = `${API_BASE}/api/articles/search?q=${encodeURIComponent(keyword)}`; let searchUrl = `${API_BASE}/api/articles/search?q=${encodeURIComponent(keyword)}`;
if (category) searchUrl += `&category=${encodeURIComponent(category)}`; if (category) searchUrl += `&category=${encodeURIComponent(category)}`;
searchUrl += `&limit=${pageSize}`; searchUrl += `&limit=${pageSize}&offset=${(currentPage - 1) * pageSize}`;
const response = await fetch(searchUrl); const response = await fetch(searchUrl);
const data = await response.json(); const data = await response.json();
@@ -157,8 +157,14 @@ function displayArticles() {
// 更新分页 // 更新分页
function updatePagination() { function updatePagination() {
const totalPages = Math.ceil(totalCount / pageSize); const totalPages = Math.ceil(totalCount / pageSize);
document.getElementById('page-info').textContent = `${currentPage} / ${totalPages || 1}`;
// 更新上部分页
document.getElementById('page-info-top').textContent = `${currentPage} / ${totalPages || 1}`;
document.getElementById('prev-btn-top').disabled = currentPage <= 1;
document.getElementById('next-btn-top').disabled = currentPage >= totalPages;
// 更新下部分页
document.getElementById('page-info').textContent = `${currentPage} / ${totalPages || 1}`;
document.getElementById('prev-btn').disabled = currentPage <= 1; document.getElementById('prev-btn').disabled = currentPage <= 1;
document.getElementById('next-btn').disabled = currentPage >= totalPages; document.getElementById('next-btn').disabled = currentPage >= totalPages;
} }
+8 -2
View File
@@ -36,9 +36,9 @@
<option value="">全部分类</option> <option value="">全部分类</option>
</select> </select>
<select id="page-size" class="page-size-select" onchange="changePageSize()"> <select id="page-size" class="page-size-select" onchange="changePageSize()">
<option value="20">每页 20 条</option> <option value="20" selected>每页 20 条</option>
<option value="50">每页 50 条</option> <option value="50">每页 50 条</option>
<option value="100" selected>每页 100 条</option> <option value="100">每页 100 条</option>
<option value="200">每页 200 条</option> <option value="200">每页 200 条</option>
<option value="500">每页 500 条</option> <option value="500">每页 500 条</option>
</select> </select>
@@ -69,6 +69,12 @@
<i class="ri-delete-bin-line"></i> 批量删除 <i class="ri-delete-bin-line"></i> 批量删除
</button> </button>
</div> </div>
<!-- 上部分页 -->
<div class="pagination-top" id="pagination-top">
<button onclick="prevPage()" class="btn btn-secondary btn-sm" id="prev-btn-top">上一页</button>
<span id="page-info-top">第 1 页</span>
<button onclick="nextPage()" class="btn btn-secondary btn-sm" id="next-btn-top">下一页</button>
</div>
</div> </div>
<div class="articles-body"> <div class="articles-body">
<div id="articles-list" class="articles-list"> <div id="articles-list" class="articles-list">