feat: 产品列表页排序
- 为ProductType添加sort字段用于排序 - 产品列表页项目按照sort升序排序
This commit is contained in:
@ -8,14 +8,14 @@
|
||||
<div class="products-container">
|
||||
<el-collapse v-model="activeNames" class="product-collapse">
|
||||
<el-collapse-item
|
||||
v-for="(group, type) in groupedProducts"
|
||||
:key="type"
|
||||
:title="type || '未分类'"
|
||||
:name="type || 'no-category'"
|
||||
v-for="[key, value] in groupedProducts"
|
||||
:key="key"
|
||||
:title="key || '未分类'"
|
||||
:name="key || 'no-category'"
|
||||
>
|
||||
<div class="group-list">
|
||||
<product-card
|
||||
v-for="product in group"
|
||||
v-for="product in value.data"
|
||||
:key="product.id"
|
||||
:slug="product.id.toString()"
|
||||
:image-url="getImageUrl(product.cover.toString())"
|
||||
@ -51,25 +51,23 @@
|
||||
productsRaw.value.map((item) => toProductListView(item))
|
||||
);
|
||||
|
||||
logger.debug('products: ', products.value);
|
||||
|
||||
// 按类型分组
|
||||
// 兼容 product_type 既可能为对象也可能为字符串
|
||||
const groupedProducts = computed(() => {
|
||||
const groups: Record<string, ProductListView[]> = {};
|
||||
const groups: Record<string, { data: ProductListView[]; sort: number }> =
|
||||
{};
|
||||
for (const prod of products.value) {
|
||||
let typeKey = '';
|
||||
if (typeof prod.product_type === 'string') {
|
||||
typeKey = prod.product_type;
|
||||
} else if (
|
||||
prod.product_type &&
|
||||
typeof prod.product_type === 'object' &&
|
||||
'name' in prod.product_type
|
||||
) {
|
||||
typeKey = prod.product_type || '';
|
||||
const typeKey = prod.product_type?.name ?? '';
|
||||
if (!groups[typeKey]) {
|
||||
groups[typeKey] = { data: [], sort: prod.product_type?.sort ?? 999 };
|
||||
}
|
||||
if (!groups[typeKey]) groups[typeKey] = [];
|
||||
groups[typeKey]?.push(prod);
|
||||
groups[typeKey]?.data.push(prod);
|
||||
}
|
||||
return groups;
|
||||
const sortedGroups = Object.entries(groups)
|
||||
.sort(([, a], [, b]) => a.sort - b.sort)
|
||||
.map(([key, value]) => [key, value]);
|
||||
return sortedGroups;
|
||||
});
|
||||
|
||||
watch(groupedProducts, () => {
|
||||
|
||||
Reference in New Issue
Block a user