对于页面展示内容进行按照规格型号进行排序,同时修改类别下拉框大小
This commit is contained in:
@ -36,12 +36,32 @@ class MaterialBaseService:
|
||||
# 支持搜索公司名
|
||||
MaterialBase.company_name.ilike(f'%{keyword}%')
|
||||
)
|
||||
).limit(20)
|
||||
)
|
||||
|
||||
# [修改1] 增加返回数量限制
|
||||
# 原为 limit(20),现改为 1000,确保前端能获取所有(或足够多)的数据
|
||||
query = query.limit(1000)
|
||||
|
||||
# 获取查询结果对象列表
|
||||
db_items = query.all()
|
||||
|
||||
# [修改2] 规格型号排序逻辑
|
||||
# 要求:只考虑 '/' 前面的内容进行排序
|
||||
# 使用 Python 的 sort 方法,提取 spec_model 中 '/' 前的部分
|
||||
def get_sort_key(item):
|
||||
if not item.spec_model:
|
||||
return ""
|
||||
# 如果包含 '/',取前半部分;否则取整个字符串
|
||||
parts = item.spec_model.split('/')
|
||||
return parts[0] if len(parts) > 0 else item.spec_model
|
||||
|
||||
# 执行排序
|
||||
db_items.sort(key=get_sort_key)
|
||||
|
||||
results = []
|
||||
for item in query.all():
|
||||
for item in db_items:
|
||||
results.append({
|
||||
'id': item.id,
|
||||
'id': item.id, # 必须保留ID供前端逻辑使用,视觉上的隐藏请在前端处理
|
||||
'companyName': item.company_name,
|
||||
'name': item.name,
|
||||
'commonName': item.common_name,
|
||||
@ -116,8 +136,10 @@ class MaterialBaseService:
|
||||
is_active = bool(int(filters['isEnabled']))
|
||||
query = query.filter_by(is_enabled=is_active)
|
||||
|
||||
# 按 ID 倒序排列
|
||||
pagination = query.order_by(MaterialBase.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
# [修改3] 默认排序方式改为按 spec_model 排序
|
||||
# 如果需要更复杂的“/前内容”排序,通常直接按字符串排序也能满足前缀分组的需求
|
||||
pagination = query.order_by(MaterialBase.spec_model.asc()).paginate(page=page, per_page=limit,
|
||||
error_out=False)
|
||||
|
||||
items_list = []
|
||||
for item in pagination.items:
|
||||
|
||||
Reference in New Issue
Block a user