From 6f20e5978d5c93447833208460d34d19d28808fd Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Thu, 16 Apr 2026 14:08:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=86=E9=A1=B5=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=BD=93=E5=89=8D=E7=AD=9B=E9=80=89=E7=9A=84?= =?UTF-8?q?=E6=80=BB=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - API返回 total 字段(筛选后的实际总数) - 新增 count_items 函数计算筛选条件下的总数 - 分页使用API返回的total而不是全局统计 - 解决筛选后分页显示不正确的问题 --- xian_favor/api.py | 16 ++++++++++++---- xian_favor/db.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/xian_favor/api.py b/xian_favor/api.py index 208712c..169b1cb 100644 --- a/xian_favor/api.py +++ b/xian_favor/api.py @@ -25,7 +25,16 @@ def list_items(): limit=int(request.args.get('limit', 50)), offset=int(request.args.get('offset', 0)) ) - return jsonify({'success': True, 'data': items}) + + # 获取符合条件的总数(用于分页) + total = db.count_items( + type=request.args.get('type'), + status=request.args.get('status'), + tag=request.args.get('tag'), + keyword=request.args.get('keyword') + ) + + return jsonify({'success': True, 'data': items, 'total': total}) @app.route('/api/items', methods=['POST']) @@ -1248,7 +1257,7 @@ async function loadItems(page = 1) { if (data.success) { renderItems(data.data); - renderPagination(data.data.length, page); + renderPagination(data.total, page); // 使用API返回的total } } @@ -1293,9 +1302,8 @@ function renderItems(items) { } // 渲染分页 -function renderPagination(itemCount, page) { +function renderPagination(total, page) { const container = document.getElementById('pagination'); - const total = parseInt(document.getElementById('statTotal').textContent); const totalPages = Math.ceil(total / pageSize); if (totalPages <= 1) { diff --git a/xian_favor/db.py b/xian_favor/db.py index 195f2cc..a17eb99 100644 --- a/xian_favor/db.py +++ b/xian_favor/db.py @@ -204,6 +204,41 @@ class Database: return items + def count_items(self, type: str = None, status: str = None, tag: str = None, + keyword: str = None) -> int: + """计算符合条件的条目总数""" + with self.get_conn() as conn: + cursor = conn.cursor() + + query = "SELECT COUNT(DISTINCT i.id) as count FROM items i" + params = [] + conditions = [] + + # 标签过滤需要JOIN + if tag: + query += " JOIN item_tags it ON i.id = it.item_id JOIN tags t ON it.tag_id = t.id" + conditions.append("t.name = ?") + params.append(tag) + + if type: + conditions.append("i.type = ?") + params.append(type) + + if status: + conditions.append("i.status = ?") + params.append(status) + + if keyword: + conditions.append("(i.title LIKE ? OR i.content LIKE ? OR i.note LIKE ?)") + keyword_pattern = f"%{keyword}%" + params.extend([keyword_pattern, keyword_pattern, keyword_pattern]) + + if conditions: + query += " WHERE " + " AND ".join(conditions) + + cursor.execute(query, params) + return cursor.fetchone()['count'] + def update_item(self, item_id: int, **kwargs) -> bool: """更新条目""" allowed_fields = ['type', 'title', 'content', 'url', 'source', 'status', 'priority', 'due_date', 'note']