index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <app-view>
  3. <app-table ref="tableRef" :data="routerStore.userRoutes" v-model:columns="columns" row-key="resourceCode">
  4. <template #headerLeft>
  5. <div>
  6. <el-button icon="Plus" @click="showComponent('edit')">新增</el-button>
  7. </div>
  8. <el-button type="primary" :icon="isRowExpansion ? 'FolderOpened' : 'Folder'" @click="tableExpandAll">
  9. <span v-if="isRowExpansion">全部收起</span>
  10. <span v-else>全部展开</span>
  11. </el-button>
  12. </template>
  13. <template #operate="{ row }">
  14. <el-button size="small" icon="Edit" circle @click="showComponent('edit', row)" />
  15. <el-button size="small" icon="Delete" circle @click="showComponent('delete', row)" />
  16. </template>
  17. </app-table>
  18. <component ref="componentRef" v-bind="{ record }" :is="componentMap.get(componentId)"
  19. @closed="closeComponent" v-if="componentId" />
  20. </app-view>
  21. </template>
  22. <script lang="ts" setup>
  23. import { shallowRef, defineAsyncComponent } from 'vue'
  24. import { useComponent } from '@/hooks/component'
  25. import { useRouterStore } from '@/stores'
  26. import AppTable from '@pc/components/base/table/index.vue'
  27. const componentMap = new Map<string, unknown>([
  28. ['edit', defineAsyncComponent(() => import('./components/edit/index.vue'))], // 编辑
  29. ['delete', defineAsyncComponent(() => import('./components/delete/index.vue'))], // 删除
  30. ])
  31. const routerStore = useRouterStore()
  32. const record = shallowRef<Model.MenusRsp>()
  33. const tableRef = shallowRef()
  34. const isRowExpansion = shallowRef(false)
  35. const { componentRef, componentId, openComponent, closeComponent } = useComponent(() => {
  36. routerStore.getUserRoutes()
  37. })
  38. const columns = shallowRef([
  39. { field: 'title', label: '菜单', align: 'left' },
  40. { field: 'resourceCode', label: '代码', },
  41. { field: 'url', label: '路由', },
  42. { field: 'icon', label: '图标', },
  43. { field: 'operate', label: 'common.operate', fixed: 'right' }
  44. ])
  45. const showComponent = (componentName: string, row?: Model.MenusRsp) => {
  46. record.value = row
  47. openComponent(componentName)
  48. }
  49. // 展开或收起表格
  50. const tableExpandAll = () => {
  51. const table = tableRef.value.elTable
  52. isRowExpansion.value = !isRowExpansion.value
  53. const toggleRowExpansionAll = (data: Model.MenusRsp[]) => {
  54. data.forEach((item) => {
  55. table.toggleRowExpansion(item, isRowExpansion.value)
  56. if (item.children) {
  57. toggleRowExpansionAll(item.children)
  58. }
  59. })
  60. }
  61. toggleRowExpansionAll(routerStore.userRoutes)
  62. }
  63. </script>