This commit is contained in:
@@ -2,6 +2,23 @@
|
||||
|
||||
import { ref, computed } from 'vue'
|
||||
|
||||
/**
|
||||
* 格式化空值为 "-"
|
||||
* @param value 要格式化的值
|
||||
* @returns 格式化后的值
|
||||
*/
|
||||
const formatEmptyValue = (value: any): any => {
|
||||
// 判断值是否为空
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return '-'
|
||||
}
|
||||
// 如果是数字0,返回0
|
||||
if (value === 0) {
|
||||
return '0'
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// 定义列配置接口
|
||||
export interface ColumnOption {
|
||||
type?: 'selection' | 'expand' | 'index'
|
||||
@@ -76,6 +93,27 @@ export function useCheckedColumns(columnsFactory: () => ColumnOption[]) {
|
||||
const columnMap = new Map<string, ColumnOption>()
|
||||
|
||||
cols.forEach((column) => {
|
||||
// 为普通列添加空值处理
|
||||
if (!column.type || (column.type !== 'selection' && column.type !== 'expand' && column.type !== 'index')) {
|
||||
const originalFormatter = column.formatter
|
||||
|
||||
// 包装原有的 formatter,在其基础上添加空值处理
|
||||
column.formatter = (row: any, ...args: any[]) => {
|
||||
let value
|
||||
|
||||
// 如果有自定义 formatter,先执行
|
||||
if (originalFormatter) {
|
||||
value = originalFormatter(row, ...args)
|
||||
} else {
|
||||
// 没有自定义 formatter,直接取字段值
|
||||
value = row[column.prop as string]
|
||||
}
|
||||
|
||||
// 对结果进行空值处理
|
||||
return formatEmptyValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
if (column.type === 'selection') {
|
||||
columnMap.set(SELECTION_KEY, column)
|
||||
} else if (column.type === 'expand') {
|
||||
|
||||
Reference in New Issue
Block a user