Skip to content

Commit 5a3861b

Browse files
committed
feat(table): 表格的数据列和操作列的字段可以根据权限和业务来控制是否显示
1 parent 7c41c86 commit 5a3861b

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

src/components/Table/src/components/TableAction.vue

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import { useTableContext } from '../hooks/useTableContext';
3838
import { usePermission } from '/@/hooks/web/usePermission';
3939
40+
import { isBoolean, isFunction } from '/@/utils/is';
4041
import { propTypes } from '/@/utils/propTypes';
4142
import { ACTION_COLUMN_FLAG } from '../const';
4243
@@ -63,10 +64,24 @@
6364
}
6465
6566
const { hasPermission } = usePermission();
67+
function isIfShow(action: ActionItem): boolean {
68+
const ifShow = action.ifShow;
69+
70+
let isIfShow = true;
71+
72+
if (isBoolean(ifShow)) {
73+
isIfShow = ifShow;
74+
}
75+
if (isFunction(ifShow)) {
76+
isIfShow = ifShow(action);
77+
}
78+
return isIfShow;
79+
}
80+
6681
const getActions = computed(() => {
6782
return (toRaw(props.actions) || [])
6883
.filter((action) => {
69-
return hasPermission(action.auth);
84+
return hasPermission(action.auth) && isIfShow(action);
7085
})
7186
.map((action) => {
7287
const { popConfirm } = action;
@@ -85,7 +100,7 @@
85100
const getDropdownList = computed(() => {
86101
return (toRaw(props.dropDownActions) || [])
87102
.filter((action) => {
88-
return hasPermission(action.auth);
103+
return hasPermission(action.auth) && isIfShow(action);
89104
})
90105
.map((action, index) => {
91106
const { label, popConfirm } = action;

src/components/Table/src/hooks/useColumns.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { unref, Ref, computed, watch, ref, toRaw } from 'vue';
66

77
import { renderEditCell } from '../components/editable';
88

9+
import { usePermission } from '/@/hooks/web/usePermission';
910
import { useI18n } from '/@/hooks/web/useI18n';
1011

1112
import { isBoolean, isArray, isString, isObject, isFunction } from '/@/utils/is';
@@ -133,31 +134,50 @@ export function useColumns(
133134
return cloneColumns;
134135
});
135136

137+
function isIfShow(column: BasicColumn): boolean {
138+
const ifShow = column.ifShow;
139+
140+
let isIfShow = true;
141+
142+
if (isBoolean(ifShow)) {
143+
isIfShow = ifShow;
144+
}
145+
if (isFunction(ifShow)) {
146+
isIfShow = ifShow(column);
147+
}
148+
return isIfShow;
149+
}
150+
const { hasPermission } = usePermission();
151+
136152
const getViewColumns = computed(() => {
137153
const viewColumns = sortFixedColumn(unref(getColumnsRef));
138154

139155
const columns = cloneDeep(viewColumns);
140-
columns.forEach((column) => {
141-
const { slots, dataIndex, customRender, format, edit, editRow, flag } = column;
142-
143-
if (!slots || !slots?.title) {
144-
column.slots = { title: `header-${dataIndex}`, ...(slots || {}) };
145-
column.customTitle = column.title;
146-
Reflect.deleteProperty(column, 'title');
147-
}
148-
const isDefaultAction = [INDEX_COLUMN_FLAG, ACTION_COLUMN_FLAG].includes(flag!);
149-
if (!customRender && format && !edit && !isDefaultAction) {
150-
column.customRender = ({ text, record, index }) => {
151-
return formatCell(text, format, record, index);
152-
};
153-
}
156+
return columns
157+
.filter((column) => {
158+
return hasPermission(column.auth) && isIfShow(column);
159+
})
160+
.map((column) => {
161+
const { slots, dataIndex, customRender, format, edit, editRow, flag } = column;
162+
163+
if (!slots || !slots?.title) {
164+
column.slots = { title: `header-${dataIndex}`, ...(slots || {}) };
165+
column.customTitle = column.title;
166+
Reflect.deleteProperty(column, 'title');
167+
}
168+
const isDefaultAction = [INDEX_COLUMN_FLAG, ACTION_COLUMN_FLAG].includes(flag!);
169+
if (!customRender && format && !edit && !isDefaultAction) {
170+
column.customRender = ({ text, record, index }) => {
171+
return formatCell(text, format, record, index);
172+
};
173+
}
154174

155-
// edit table
156-
if ((edit || editRow) && !isDefaultAction) {
157-
column.customRender = renderEditCell(column);
158-
}
159-
});
160-
return columns;
175+
// edit table
176+
if ((edit || editRow) && !isDefaultAction) {
177+
column.customRender = renderEditCell(column);
178+
}
179+
return column;
180+
});
161181
});
162182

163183
watch(

src/components/Table/src/types/table.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88

99
import { ComponentType } from './componentType';
1010
import { VueNode } from '/@/utils/propTypes';
11+
import { RoleEnum } from '/@/enums/roleEnum';
1112

1213
export declare type SortOrder = 'ascend' | 'descend';
1314

@@ -421,4 +422,8 @@ export interface BasicColumn extends ColumnProps {
421422
editRule?: boolean | ((text: string, record: Recordable) => Promise<string>);
422423
editValueMap?: (value: any) => string;
423424
onEditRow?: () => void;
425+
// 权限编码控制是否显示
426+
auth?: RoleEnum | RoleEnum[] | string | string[];
427+
// 业务控制是否显示
428+
ifShow?: boolean | ((column: BasicColumn) => boolean);
424429
}

src/components/Table/src/types/tableAction.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export interface ActionItem extends ButtonProps {
88
popConfirm?: PopConfirm;
99
disabled?: boolean;
1010
divider?: boolean;
11-
// Permission code
11+
// 权限编码控制是否显示
1212
auth?: RoleEnum | RoleEnum[] | string | string[];
13+
// 业务控制是否显示
14+
ifShow?: boolean | ((action: ActionItem) => boolean);
1315
}
1416

1517
export interface PopConfirm {

0 commit comments

Comments
 (0)