fix: 修复子类别筛选按钮点击无效问题

- 用containerId判断currentFilter,而非categoryId
- 用previousElementSibling找全部按钮,而非parent.querySelector
- 确保点击子类别后样式正确切换
- 全部按钮和子类别按钮互斥选中状态
This commit is contained in:
2026-04-29 22:58:44 +08:00
parent 6e69f09842
commit 44ad4b6446

View File

@@ -535,27 +535,26 @@
return;
}
// 获取当前筛选值
// 获取当前筛选值 - 根据容器ID判断
let currentFilter = '';
if (categoryId === 'ai-models') currentFilter = modelSubcategoryFilter;
else if (categoryId === 'gpus') currentFilter = gpuSubcategoryFilter;
else if (categoryId === 'cpus') currentFilter = cpuSubcategoryFilter;
else if (categoryId === dynamicCategoryId) currentFilter = dynamicSubcategoryFilter;
if (containerId === 'model-subcategory-filters') currentFilter = modelSubcategoryFilter;
else if (containerId === 'gpu-subcategory-filters') currentFilter = gpuSubcategoryFilter;
else if (containerId === 'cpu-subcategory-filters') currentFilter = cpuSubcategoryFilter;
else if (containerId === 'dynamic-subcategory-filters') currentFilter = dynamicSubcategoryFilter;
// 渲染子类别按钮,不渲染"全部"按钮HTML里已有
// 渲染子类别按钮
container.innerHTML = cat.subcategories.map(sub => `
<button onclick="${filterFunction}('${sub.id}')" class="px-3 py-1 rounded text-sm ${currentFilter === sub.id ? 'bg-indigo-600 text-white' : 'bg-white border text-gray-600 hover:bg-gray-100'}">
<i class="${sub.icon || 'ri-folder-line'} mr-1"></i>${sub.name}
</button>
`).join('');
// 更新"全部"按钮的样式
const parent = container.parentElement;
if (parent) {
const allBtn = parent.querySelector('button:first-of-type');
if (allBtn && currentFilter === '') {
// 更新"全部"按钮的样式 - 它是容器前面的那个按钮
const allBtn = container.previousElementSibling;
if (allBtn && allBtn.tagName === 'BUTTON') {
if (currentFilter === '') {
allBtn.className = 'px-3 py-1 rounded text-sm bg-indigo-600 text-white';
} else if (allBtn) {
} else {
allBtn.className = 'px-3 py-1 rounded text-sm bg-white border text-gray-600 hover:bg-gray-100';
}
}