Commit 0f88d2e8 authored by 水玉婷's avatar 水玉婷
Browse files

feat:修复表格排序bug

parent d54fa34e
...@@ -65,8 +65,7 @@ const handleTableChange = (pagination: any, filters: any, sorter: any) => { ...@@ -65,8 +65,7 @@ const handleTableChange = (pagination: any, filters: any, sorter: any) => {
} }
// 处理排序变化(支持多列排序) // 处理排序变化(支持多列排序)
if (sorter) { if (sorter && (sorter.order || (Array.isArray(sorter) && sorter.some(item => item.order)))) {
// 更新排序状态 // 更新排序状态
sortState.isSorted = true; sortState.isSorted = true;
...@@ -81,19 +80,26 @@ const handleTableChange = (pagination: any, filters: any, sorter: any) => { ...@@ -81,19 +80,26 @@ const handleTableChange = (pagination: any, filters: any, sorter: any) => {
// 应用排序 // 应用排序
if (Array.isArray(sorter)) { if (Array.isArray(sorter)) {
// 多列排序情况:按优先级顺序应用排序 // 多列排序情况:按优先级顺序应用排序
sorter.forEach((sortItem) => { rawData.sort((a, b) => {
if (sortItem.order) { for (const sortItem of sorter) {
rawData.sort((a, b) => { if (sortItem.order) {
const aValue = getSortableValue(a, sortItem.field); const aValue = getSortableValue(a, sortItem.field);
const bValue = getSortableValue(b, sortItem.field); const bValue = getSortableValue(b, sortItem.field);
let result = 0;
if (sortItem.order === 'ascend') { if (sortItem.order === 'ascend') {
return aValue - bValue; result = aValue - bValue;
} else { } else {
return bValue - aValue; result = bValue - aValue;
}
// 如果当前列的比较结果不为0,返回结果
if (result !== 0) {
return result;
} }
}); }
} }
return 0;
}); });
} else if (sorter.order) { } else if (sorter.order) {
// 单列排序情况 // 单列排序情况
...@@ -111,6 +117,10 @@ const handleTableChange = (pagination: any, filters: any, sorter: any) => { ...@@ -111,6 +117,10 @@ const handleTableChange = (pagination: any, filters: any, sorter: any) => {
// 更新排序后的数据 // 更新排序后的数据
sortState.sortedData = rawData; sortState.sortedData = rawData;
} else if (sorter && !sorter.order && !Array.isArray(sorter)) {
// 清除排序状态(用户点击取消排序)
sortState.isSorted = false;
sortState.sortedData = [];
} }
}; };
...@@ -278,17 +288,10 @@ const tableColumns = computed(() => { ...@@ -278,17 +288,10 @@ const tableColumns = computed(() => {
compare: (a: any, b: any) => { compare: (a: any, b: any) => {
// 对于数字字段 // 对于数字字段
if (isNumericColumn(header)) { if (isNumericColumn(header)) {
// 对于indexFields字段,使用存储的原始值进行排序 // 统一使用getSortableValue函数获取可排序的值
if (isIndexField && a[header] && a[header].value !== undefined) { const aValue = getSortableValue(a, header);
const aValue = parseFloat(a[header].value); const bValue = getSortableValue(b, header);
const bValue = parseFloat(b[header].value); return aValue - bValue;
return aValue - bValue;
} else {
// 其他数字字段直接使用当前字段排序
const aValue = parseFloat(a[header]);
const bValue = parseFloat(b[header]);
return aValue - bValue;
}
} else { } else {
// 对于文字字段,使用字符串排序 // 对于文字字段,使用字符串排序
const aValue = getStringValue(a, header); const aValue = getStringValue(a, header);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment