办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!--
不兼容也不会兼容一些错误用法
比如: 菜单下放目录 菜单下放菜单
比如: 按钮下放目录 按钮下放菜单 按钮下放按钮
-->
<script setup lang="tsx">
import type { RadioChangeEvent } from 'ant-design-vue';
 
import type { MenuPermissionOption } from './data';
 
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { MenuOption } from '#/api/system/menu/model';
 
import { nextTick, onMounted, ref, shallowRef, watch } from 'vue';
 
import { cloneDeep, findGroupParentIds } from '@vben/utils';
 
import { Alert, Checkbox, RadioGroup, Space } from 'ant-design-vue';
import { uniq } from 'lodash-es';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
 
import { columns, nodeOptions } from './data';
import {
  menusWithPermissions,
  rowAndChildrenChecked,
  setPermissionsChecked,
  setTableChecked,
} from './helper';
import { useFullScreenGuide } from './hook';
 
defineOptions({
  name: 'MenuSelectTable',
  inheritAttrs: false,
});
 
const props = withDefaults(
  defineProps<{
    checkedKeys: (number | string)[];
    defaultExpandAll?: boolean;
    menus: MenuOption[];
  }>(),
  {
    /**
     * 是否默认展开全部
     */
    defaultExpandAll: true,
    /**
     * 注意这里不是双向绑定 需要调用getCheckedKeys实例方法来获取真正选中的节点
     */
    checkedKeys: () => [],
  },
);
 
/**
 * 是否节点关联
 */
const association = defineModel<boolean>('association', {
  default: true,
});
 
const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // checkbox显示的字段
    labelField: 'label',
    // 是否严格模式 即节点不关联
    checkStrictly: !association.value,
  },
  size: 'small',
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {
    enabled: false,
  },
  proxyConfig: {
    enabled: false,
  },
  toolbarConfig: {
    refresh: false,
    custom: false,
  },
  rowConfig: {
    isHover: false,
    isCurrent: false,
    keyField: 'id',
  },
  /**
   * 开启虚拟滚动
   * 数据量小可以选择关闭
   * 如果遇到样式问题(空白、错位 滚动等)可以选择关闭虚拟滚动
   */
  scrollY: {
    enabled: true,
    gt: 0,
  },
  treeConfig: {
    parentField: 'parentId',
    rowField: 'id',
    transform: false,
  },
  // 溢出换行显示
  showOverflow: false,
};
 
/**
 * 用于界面显示选中的数量
 */
const checkedNum = ref(0);
/**
 * 更新选中的数量
 */
function updateCheckedNumber() {
  checkedNum.value = getCheckedKeys().length;
}
 
const [BasicTable, tableApi] = useVbenVxeGrid({
  gridOptions,
  gridEvents: {
    // 勾选事件
    checkboxChange: (params) => {
      // 选中还是取消选中
      const checked = params.checked;
      // 行
      const record = params.row;
      if (association.value) {
        // 节点关联
        // 设置所有子节点选中状态
        rowAndChildrenChecked(record, checked);
      } else {
        // 节点独立
        // 点行会勾选/取消全部权限  点权限不会勾选行
        setPermissionsChecked(record, checked);
      }
      updateCheckedNumber();
    },
    // 全选事件
    checkboxAll: (params) => {
      const records = params.$grid.getData();
      records.forEach((item) => {
        rowAndChildrenChecked(item, params.checked);
      });
      updateCheckedNumber();
    },
  },
});
 
/**
 * 设置表格选中
 * @param menus menu
 * @param keys 选中的key
 * @param triggerOnchange 节点独立情况 不需要触发onChange(false)
 */
function setCheckedByKeys(
  menus: MenuPermissionOption[],
  keys: (number | string)[],
  triggerOnchange: boolean,
) {
  menus.forEach((item) => {
    // 设置行选中
    if (keys.includes(item.id)) {
      tableApi.grid.setCheckboxRow(item, true);
    }
    // 设置权限columns选中
    if (item.permissions && item.permissions.length > 0) {
      // 遍历 设置勾选
      item.permissions.forEach((permission) => {
        if (keys.includes(permission.id)) {
          permission.checked = true;
          // 手动触发onChange来选中 节点独立情况不需要处理
          triggerOnchange && handlePermissionChange(item);
        }
      });
    }
    // 设置children选中
    if (item.children && item.children.length > 0) {
      setCheckedByKeys(item.children as any, keys, triggerOnchange);
    }
  });
}
 
const { FullScreenGuide, openGuide } = useFullScreenGuide();
onMounted(() => {
  /**
   * 加载表格数据 转为指定结构
   */
  watch(
    () => props.menus,
    async (menus) => {
      const clonedMenus = cloneDeep(menus);
      menusWithPermissions(clonedMenus);
      // console.log(clonedMenus);
      await tableApi.grid.loadData(clonedMenus);
      // 展开全部 默认true
      if (props.defaultExpandAll) {
        await nextTick();
        setExpandOrCollapse(true);
      }
    },
  );
 
  /**
   * 节点关联变动 更新表格勾选效果
   */
  watch(association, (value) => {
    tableApi.setGridOptions({
      checkboxConfig: {
        checkStrictly: !value,
      },
    });
  });
 
  /**
   * checkedKeys依赖menus
   * 要注意加载顺序
   * !!!要在外部确保menus先加载!!!
   */
  watch(
    () => props.checkedKeys,
    (value) => {
      const allCheckedKeys = uniq([...value]);
      // 获取表格data 如果checkedKeys在menus的watch之前触发 这里会拿到空 导致勾选异常
      const records = tableApi.grid.getData();
      setCheckedByKeys(records, allCheckedKeys, association.value);
      updateCheckedNumber();
 
      // 全屏引导
      setTimeout(openGuide, 1000);
    },
  );
});
 
// 缓存上次(切换节点关系前)选中的keys
const lastCheckedKeys = shallowRef<(number | string)[]>([]);
/**
 * 节点关联变动 事件
 */
async function handleAssociationChange(e: RadioChangeEvent) {
  lastCheckedKeys.value = getCheckedKeys();
  // 清空全部permissions选中
  const records = tableApi.grid.getData();
  records.forEach((item) => {
    rowAndChildrenChecked(item, false);
  });
  // 需要清空全部勾选
  await tableApi.grid.clearCheckboxRow();
  // 滚动到顶部
  await tableApi.grid.scrollTo(0, 0);
 
  // 节点切换 不同的选中
  setTableChecked(lastCheckedKeys.value, records, tableApi, !e.target.value);
 
  updateCheckedNumber();
}
 
/**
 * 全部展开/折叠
 * @param expand 是否展开
 */
function setExpandOrCollapse(expand: boolean) {
  tableApi.grid?.setAllTreeExpand(expand);
}
 
/**
 * 权限列表 checkbox勾选的事件
 * @param row 行
 */
function handlePermissionChange(row: any) {
  // 节点关联
  if (association.value) {
    const checkedPermissions = row.permissions.filter(
      (item: any) => item.checked === true,
    );
    // 有一条选中 则整个行选中
    if (checkedPermissions.length > 0) {
      tableApi.grid.setCheckboxRow(row, true);
    }
    // 无任何选中 则整个行不选中
    if (checkedPermissions.length === 0) {
      tableApi.grid.setCheckboxRow(row, false);
    }
  }
  // 节点独立 不处理
  updateCheckedNumber();
}
 
/**
 * 获取勾选的key
 * @param records 行记录列表
 * @param addCurrent 是否添加当前行的id
 */
function getKeys(records: MenuPermissionOption[], addCurrent: boolean) {
  const allKeys: (number | string)[] = [];
  records.forEach((item) => {
    // 处理children
    if (item.children && item.children.length > 0) {
      const keys = getKeys(item.children as MenuPermissionOption[], addCurrent);
      allKeys.push(...keys);
    } else {
      // 当前行的id
      addCurrent && allKeys.push(item.id);
      // 当前行权限id 获取已经选中的
      if (item.permissions && item.permissions.length > 0) {
        const ids = item.permissions
          .filter((m) => m.checked === true)
          .map((m) => m.id);
        allKeys.push(...ids);
      }
    }
  });
  return uniq(allKeys);
}
 
/**
 * 获取选中的key
 */
function getCheckedKeys() {
  // 节点关联
  if (association.value) {
    const records = tableApi?.grid?.getCheckboxRecords?.() ?? [];
    // 子节点
    const nodeKeys = getKeys(records, true);
    // 所有父节点
    const parentIds = findGroupParentIds(props.menus, nodeKeys as number[]);
    // 拼接 去重
    const realKeys = uniq([...parentIds, ...nodeKeys]);
    return realKeys;
  }
  // 节点独立
 
  // 勾选的行
  const records = tableApi?.grid?.getCheckboxRecords?.() ?? [];
  // 全部数据 用于获取permissions
  const allRecords = tableApi?.grid?.getData?.() ?? [];
  // 表格已经选中的行ids
  const checkedIds = records.map((item) => item.id);
  // 所有已经勾选权限的ids
  const permissionIds = getKeys(allRecords, false);
  // 合并 去重
  const allIds = uniq([...checkedIds, ...permissionIds]);
  return allIds;
}
 
/**
 * 暴露给外部使用 获取已选中的key
 */
defineExpose({
  getCheckedKeys,
});
</script>
 
<template>
  <div class="flex h-full flex-col" id="menu-select-table">
    <BasicTable>
      <template #toolbar-actions>
        <RadioGroup
          v-model:value="association"
          :options="nodeOptions"
          button-style="solid"
          option-type="button"
          @change="handleAssociationChange"
        />
        <Alert class="mx-2" type="info">
          <template #message>
            <div>
              已选中
              <span class="text-primary mx-1 font-semibold">
                {{ checkedNum }}
              </span>
              个节点
            </div>
          </template>
        </Alert>
      </template>
      <template #toolbar-tools>
        <Space>
          <a-button @click="setExpandOrCollapse(false)">
            {{ $t('pages.common.collapse') }}
          </a-button>
          <a-button @click="setExpandOrCollapse(true)">
            {{ $t('pages.common.expand') }}
          </a-button>
        </Space>
      </template>
      <template #permissions="{ row }">
        <div class="flex flex-wrap gap-x-3 gap-y-1">
          <Checkbox
            v-for="permission in row.permissions"
            :key="permission.id"
            v-model:checked="permission.checked"
            @change="() => handlePermissionChange(row)"
          >
            {{ permission.label }}
          </Checkbox>
        </div>
      </template>
    </BasicTable>
    <!-- 全屏引导 -->
    <FullScreenGuide />
  </div>
</template>
 
<style scoped>
:deep(.ant-alert) {
  padding: 4px 8px;
}
</style>