Commit 5a3861b9cfc79da3297f8ddd045b88f0daca0ada

Authored by zuihou
1 parent 7c41c867

feat(table): 表格的数据列和操作列的字段可以根据权限和业务来控制是否显示

src/components/Table/src/components/TableAction.vue
... ... @@ -37,6 +37,7 @@
37 37 import { useTableContext } from '../hooks/useTableContext';
38 38 import { usePermission } from '/@/hooks/web/usePermission';
39 39  
  40 + import { isBoolean, isFunction } from '/@/utils/is';
40 41 import { propTypes } from '/@/utils/propTypes';
41 42 import { ACTION_COLUMN_FLAG } from '../const';
42 43  
... ... @@ -63,10 +64,24 @@
63 64 }
64 65  
65 66 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 +
66 81 const getActions = computed(() => {
67 82 return (toRaw(props.actions) || [])
68 83 .filter((action) => {
69   - return hasPermission(action.auth);
  84 + return hasPermission(action.auth) && isIfShow(action);
70 85 })
71 86 .map((action) => {
72 87 const { popConfirm } = action;
... ... @@ -85,7 +100,7 @@
85 100 const getDropdownList = computed(() => {
86 101 return (toRaw(props.dropDownActions) || [])
87 102 .filter((action) => {
88   - return hasPermission(action.auth);
  103 + return hasPermission(action.auth) && isIfShow(action);
89 104 })
90 105 .map((action, index) => {
91 106 const { label, popConfirm } = action;
... ...
src/components/Table/src/hooks/useColumns.ts
... ... @@ -6,6 +6,7 @@ import { unref, Ref, computed, watch, ref, toRaw } from 'vue';
6 6  
7 7 import { renderEditCell } from '../components/editable';
8 8  
  9 +import { usePermission } from '/@/hooks/web/usePermission';
9 10 import { useI18n } from '/@/hooks/web/useI18n';
10 11  
11 12 import { isBoolean, isArray, isString, isObject, isFunction } from '/@/utils/is';
... ... @@ -133,31 +134,50 @@ export function useColumns(
133 134 return cloneColumns;
134 135 });
135 136  
  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 +
136 152 const getViewColumns = computed(() => {
137 153 const viewColumns = sortFixedColumn(unref(getColumnsRef));
138 154  
139 155 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 + }
154 174  
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 + });
161 181 });
162 182  
163 183 watch(
... ...
src/components/Table/src/types/table.ts
... ... @@ -8,6 +8,7 @@ import type {
8 8  
9 9 import { ComponentType } from './componentType';
10 10 import { VueNode } from '/@/utils/propTypes';
  11 +import { RoleEnum } from '/@/enums/roleEnum';
11 12  
12 13 export declare type SortOrder = 'ascend' | 'descend';
13 14  
... ... @@ -421,4 +422,8 @@ export interface BasicColumn extends ColumnProps {
421 422 editRule?: boolean | ((text: string, record: Recordable) => Promise<string>);
422 423 editValueMap?: (value: any) => string;
423 424 onEditRow?: () => void;
  425 + // 权限编码控制是否显示
  426 + auth?: RoleEnum | RoleEnum[] | string | string[];
  427 + // 业务控制是否显示
  428 + ifShow?: boolean | ((column: BasicColumn) => boolean);
424 429 }
... ...
src/components/Table/src/types/tableAction.ts
... ... @@ -8,8 +8,10 @@ export interface ActionItem extends ButtonProps {
8 8 popConfirm?: PopConfirm;
9 9 disabled?: boolean;
10 10 divider?: boolean;
11   - // Permission code
  11 + // 权限编码控制是否显示
12 12 auth?: RoleEnum | RoleEnum[] | string | string[];
  13 + // 业务控制是否显示
  14 + ifShow?: boolean | ((action: ActionItem) => boolean);
13 15 }
14 16  
15 17 export interface PopConfirm {
... ...