最近在使用JeectBoot作为后台框架,在开发过程中发现JVxeTable组件不支持分组列编辑 ,在配置了列的子节点后,是能展现出分组效果的,但是不能编辑。JVxeTable基于VxeTable实现,VxeTable是支持分组表头,并且分组列可编辑的,所以需要修改JVxeTable组件进行适配。

实现步骤

1. 修改JVxeTable组件

在JVxeTable组件中,修改src/components/jeecg/JVxeTable/src/hooks/useColumns.ts文件。 主要修改useColumns方法,如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export function useColumns(props: JVxeTableProps, data: JVxeDataProps, methods: JVxeTableMethods, slots) {
  data.vxeColumns = computed(() => {
    const columns: JVxeColumn[] = [];
    if (isArray(props.columns)) {
      // handle 方法参数
      const args: HandleArgs = { props, slots, data, methods, columns };
      let seqColumn, selectionColumn, expandColumn, dragSortColumn;
      props.columns.forEach((column: JVxeColumn) => {
        // 排除未授权的列 1 = 显示/隐藏; 2 = 禁用
        const auth = methods.getColAuth(column.key);
        if (auth?.type == '1' && !auth.isAuth) {
          return;
        } else if (auth?.type == '2' && !auth.isAuth) {
          column.disabled = true;
        }
        // type 不填,默认为 normal
        if (column.type == null || isEmpty(column.type)) {
          column.type = JVxeTypes.normal;
        }
        const col: JVxeColumn = cloneDeep(column);
        // 处理隐藏列
        if (col.type === JVxeTypes.hidden) {
          return handleInnerColumn(args, col, handleHiddenColumn);
        }
        // 组件未注册,自动设置为 normal
        if (!isRegistered(col.type)) {
          col.type = JVxeTypes.normal;
        }
        fillHandleArgs(args, col, props, data, methods);
        if (col.type === JVxeTypes.rowNumber) {
          seqColumn = col;
          columns.push(col);
        } else if (col.type === JVxeTypes.rowRadio || col.type === JVxeTypes.rowCheckbox) {
          selectionColumn = col;
          columns.push(col);
        } else if (col.type === JVxeTypes.rowExpand) {
          expandColumn = col;
          columns.push(col);
        } else if (col.type === JVxeTypes.rowDragSort) {
          dragSortColumn = col;
          columns.push(col);
        } else {
          col.params = column;
          handlerCol(args);
        }
        //支持分组表头展示-----20240717
        const childrenCol = handleChildren(props, data, args, methods, column, column);
        if (childrenCol) {
          column.children = childrenCol;
        }
      });
      handleInnerColumn(args, seqColumn, handleSeqColumn);
      handleInnerColumn(args, selectionColumn, handleSelectionColumn);
      handleInnerColumn(args, expandColumn, handleExpandColumn);
      handleInnerColumn(args, dragSortColumn, handleDragSortColumn, true);
      customComponentAddStar(columns);
    }
    return columns;
  });
}

这一部分代码主要是将props中的columns参数,转化为VxeTable组件的columns参数,在这里将子节点添加到父节点的children属性中。 以下是处理子节点部分的代码。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function handleChildren(
  props: JVxeTableProps,
  data: JVxeDataProps,
  args: HandleArgs,
  methods: JVxeTableMethods,
  column: JVxeColumn,
  originCol: JVxeColumn
) {
  const col: JVxeColumn = cloneDeep(column);
  if (col.children) {
    const childrenArray: JVxeColumn[] = [];
    col.children?.forEach((child) => {
      if (child.children) {
        const childrenCol = handleChildren(props, data, args, methods, child, originCol);
        if (childrenCol) {
          child.children = childrenCol;
        }
      }
      // type 不填,默认为 normal
      if (child.type == null || isEmpty(child.type)) {
        child.type = JVxeTypes.normal;
      }
      const childCol: JVxeColumn = cloneDeep(child);
      // 处理隐藏列
      if (childCol.type === JVxeTypes.hidden) {
        return handleInnerColumn(args, childCol, handleHiddenColumn);
      }
      // 组件未注册,自动设置为 normal
      if (!isRegistered(childCol.type)) {
        childCol.type = JVxeTypes.normal;
      }
      fillHandleArgs(args, childCol, props, data, methods);
      childCol.params = child;
      handlerCol(args, true);
      childrenArray.push(childCol);
    });
    col.children = childrenArray;
  }
  return col.children;
}
function fillHandleArgs(args: HandleArgs, col: JVxeColumn, props: JVxeTableProps, data: JVxeDataProps, methods: JVxeTableMethods) {
  args.enhanced = getEnhanced(col.type);
  args.col = col;
  args.renderOptions = {
    bordered: props.bordered,
    disabled: props.disabled,
    scrolling: data.scrolling,
    isDisabledRow: methods.isDisabledRow,
    listeners: {
      trigger: (name, event) => methods.trigger(name, event),
      valueChange: (event) => methods.trigger('valueChange', event),
      /** 重新排序行 */
      rowResort: (event) => {
        methods.doSort(event.oldIndex, event.newIndex);
        methods.trigger('dragged', event);
      },
      /** 在当前行下面插入一行 */
      rowInsertDown: (rowIndex) => methods.insertRows({}, rowIndex + 1),
    },
  };
}

这样修改后,VxeTable就会读取到子节点并进行渲染。

2. 修改内置组件

JvxeTable提供了很多内置组件供我们使用,但是目前只支持单级节点。 在我一番摸索后,发现只要是需要取节点属性的组件,就都需要修改。 比如JVxeSelectCell,下面的代码是修改后的。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
export default defineComponent({
    name: 'JVxeSelectCell',
    components: { LoadingOutlined },
    props: useJVxeCompProps(),
    setup(props: JVxeComponent.Props) {
      const { innerValue, cellProps, row, column, originColumn, scrolling, handleChangeCommon, handleBlurCommon } = useJVxeComponent(props);
      const loading = ref(false);
      // 异步加载的options(用于多级联动)
      const asyncOptions = ref<any[] | null>(null);
      let finalColumn = originColumn;
      const columnKey = column.value.key || column.value.field;
      if (columnKey !== originColumn.value.key && originColumn.value.children) {
        const { options, allowSearch, allowInput } = originColumn.value.children.find((child) => child.key === columnKey) ?? {};
        finalColumn = column;
        finalColumn.value.options = options;
        finalColumn.value.allowSearch = allowSearch;
        finalColumn.value.allowInput = allowInput;
      }
      // 下拉框 props
      const selectProps = computed(() => {
        let selProps = {
          ...cellProps.value,
          allowClear: true,
          autofocus: true,
          defaultOpen: !scrolling.value,
          style: { width: '100%' },
          filterOption: handleSelectFilterOption,
          onBlur: handleBlur,
          onChange: handleChange,
        };
        // 判断select是否允许输入
        let { allowSearch, allowInput } = finalColumn.value;
        if (allowInput === true || allowSearch === true) {
          selProps['showSearch'] = true;
          selProps['onSearch'] = handleSearchSelect;
        }
        return selProps;
      });
      // 下拉选项
      const selectOptions = computed(() => {
        if (asyncOptions.value) {
          return asyncOptions.value;
        }
        let { linkage } = props.renderOptions;
        if (linkage) {
          let { getLinkageOptionsSibling, config } = linkage;
          let res = getLinkageOptionsSibling(row.value, finalColumn.value, config, true);
          // 当返回Promise时,说明是多级联动
          if (res instanceof Promise) {
            loading.value = true;
            res
              .then((opt) => {
                asyncOptions.value = opt;
                loading.value = false;
              })
              .catch((e) => {
                console.error(e);
                loading.value = false;
              });
          } else {
            asyncOptions.value = null;
            return res;
          }
        }
        return finalColumn.value.options;
      });

      // --------- created ---------

      // 多选、搜索type
      let multipleTypes = [JVxeTypes.selectMultiple, 'list_multi'];
      let searchTypes = [JVxeTypes.selectSearch, 'sel_search'];
      if (multipleTypes.includes(props.type)) {
        // 处理多选
        let props = finalColumn.value.props || {};
        props['mode'] = 'multiple';
        props['maxTagCount'] = 1;
        finalColumn.value.allowSearch = true;
        finalColumn.value.props = props;
      } else if (searchTypes.includes(props.type)) {
        // 处理搜索
        finalColumn.value.allowSearch = true;
      }

      /** 处理 change 事件 */
      function handleChange(value) {
        // 处理下级联动
        let linkage = props.renderOptions.linkage;
        if (linkage) {
          linkage.handleLinkageSelectChange(row.value, finalColumn.value, linkage.config, value);
        }
        handleChangeCommon(value);
      }

      /** 处理blur失去焦点事件 */
      function handleBlur(value) {
        let { allowInput, options } = finalColumn.value;
        if (allowInput === true) {
          // 删除无用的因搜索(用户输入)而创建的项
          if (typeof value === 'string') {
            let indexes: number[] = [];
            options.forEach((option, index) => {
              if (option.value.toLocaleString() === value.toLocaleString()) {
                delete option.searchAdd;
              } else if (option.searchAdd === true) {
                indexes.push(index);
              }
            });
            // 翻转删除数组中的项
            for (let index of indexes.reverse()) {
              options.splice(index, 1);
            }
          }
        }
        handleBlurCommon(value);
      }

      /** 用于搜索下拉框中的内容 */
      function handleSelectFilterOption(input, option) {
        let { allowSearch, allowInput } = finalColumn.value;
        if (allowSearch === true || allowInput === true) {
          if (option.title == null) return false;
          return option.title.toLowerCase().indexOf(input.toLowerCase()) >= 0;
        }
        return true;
      }

      /** select 搜索时的事件,用于动态添加options */
      function handleSearchSelect(value) {
        let { allowSearch, allowInput, options } = finalColumn.value;
        if (allowSearch !== true && allowInput === true) {
          // 是否找到了对应的项,找不到则添加这一项
          let flag = false;
          for (let option of options) {
            if (option.value.toLocaleString() === value.toLocaleString()) {
              flag = true;
              break;
            }
          }
          if (!flag && !!value) {
            // searchAdd 是否是通过搜索添加的
            options.push({ title: value, value: value, searchAdd: true });
          }
        }
      }

      return {
        loading,
        innerValue,
        selectProps,
        selectOptions,
        handleChange,
        handleBlur,
      };
    },
    // 【组件增强】注释详见:JVxeComponent.Enhanced
    enhanced: {
      aopEvents: {
        editActived({ $event, row, column }) {
          dispatchEvent({
            $event,
            row,
            column,
            props: this.props,
            instance: this,
            className: '.ant-select .ant-select-selection-search-input',
            isClick: false,
            handler: (el) => el.focus(),
          });
        },
      },
      translate: {
        enabled: true,
        async handler(value, ctx) {
          let { props, context } = ctx!;
          let { row, column, originColumn } = context;
          let finalColumn = originColumn;
          if (column.value.key != originColumn.value.key && originColumn.value.children) {
            finalColumn = column;
            finalColumn.value.options = originColumn.value.children.find((child) => child.key == column.value.key)?.options;
          }
          let options;
          let linkage = props?.renderOptions.linkage;
          // 判断是否是多级联动,如果是就通过接口异步翻译
          if (linkage) {
            let { getLinkageOptionsSibling, config } = linkage;
            let linkageOptions = getLinkageOptionsSibling(row.value, finalColumn.value, config, true);
            options = isPromise(linkageOptions) ? await linkageOptions : linkageOptions;
          } else if (isPromise(finalColumn.value.optionsPromise)) {
            options = await finalColumn.value.optionsPromise;
          } else {
            options = finalColumn.value.options;
          }
          return filterDictText(options, value);
        },
      },
      getValue(value) {
        if (Array.isArray(value)) {
          return value.join(',');
        } else {
          return value;
        }
      },
      setValue(value, ctx) {
        let { context } = ctx!;
        let { originColumn, column } = context;
        let finalColumn = originColumn;
        if (column.value.key != originColumn.value.key && originColumn.value.children) {
          finalColumn = column;
          finalColumn.value.options = originColumn.value.children.find((child) => child.key == column.value.key)?.options;
        }
        // 判断是否是多选
        if ((finalColumn.value.props || {})['mode'] === 'multiple') {
          finalColumn.value.props['maxTagCount'] = 1;
        }
        if (value != null && value !== '') {
          if (typeof value === 'string') {
            return value === '' ? [] : value.split(',');
          }
          return value;
        } else {
          return undefined;
        }
      },
    } as JVxeComponent.EnhancedPartial,
  });

我加了一个finalColumn变量,用于保存最终的column,当column有子节点时,就从childen去查找匹配的列,然后赋值给finalColumn。 其他的组件也这样修改就行。

示例列配置信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[
        {
            "headerAlign": "center",
            "defaultValue": "2024-07-23",
            "width": 120,
            "placeholder": "请选择",
            "title": "日期",
            "type": "date",
            "key": "date"
        },
        {
            "headerAlign": "center",
            "width": 120,
            "title": "时间",
            "type": "time",
            "key": "time"
        },
        {
            "headerAlign": "center",
            "children": [
                {
                    "headerAlign": "center",
                    "width": 100,
                    "title": "体温",
                    "type": "input-number",
                    "key": "VitalSign_temperature"
                },
                {
                    "headerAlign": "center",
                    "width": 100,
                    "title": "呼吸",
                    "type": "input-number",
                    "key": "VitalSign_respiratory"
                }
            ],
            "width": 200,
            "title": "生命体征",
            "type": "normal",
            "key": "VitalSign"
        },
        {
            "headerAlign": "center",
            "children": [
                {
                    "headerAlign": "center",
                    "children": [
                        {
                            "headerAlign": "center",
                            "width": 75,
                            "title": "反射",
                            "type": "input-number",
                            "key": "pupil_LeftEye_reflex"
                        },
                        {
                            "headerAlign": "center",
                            "width": 75,
                            "title": "直径",
                            "type": "input-number",
                            "key": "pupil_LeftEye_diameter"
                        }
                    ],
                    "width": 150,
                    "title": "左眼",
                    "type": "normal",
                    "key": "pupil_LeftEye"
                },
                {
                    "headerAlign": "center",
                    "children": [
                        {
                            "headerAlign": "center",
                            "width": 75,
                            "title": "反射",
                            "type": "input-number",
                            "key": "pupil_Right_reflex"
                        },
                        {
                            "headerAlign": "center",
                            "width": 75,
                            "title": "直径",
                            "type": "input-number",
                            "key": "pupil_Right_diameter"
                        }
                    ],
                    "width": 150,
                    "title": "右眼",
                    "type": "normal",
                    "key": "pupil_Right"
                }
            ],
            "width": 300,
            "title": "瞳孔",
            "type": "normal",
            "key": "pupil"
        }
    ]

效果

image-20210831165947503