index-tree.stub 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <div>
  3. <el-card class="!border-none mb-4" shadow="never">
  4. <el-form
  5. ref="formRef"
  6. class="mb-[-16px]"
  7. :model="queryParams"
  8. inline
  9. >
  10. {SEARCH_VIEW}
  11. <el-form-item>
  12. <el-button type="primary" @click="getLists">查询</el-button>
  13. <el-button @click="resetParams">重置</el-button>
  14. </el-form-item>
  15. </el-form>
  16. </el-card>
  17. <el-card class="!border-none" shadow="never">
  18. <div>
  19. <el-button v-perms="['{PERMS_ADD}']" type="primary" @click="handleAdd()">
  20. <template #icon>
  21. <icon name="el-icon-Plus" />
  22. </template>
  23. 新增
  24. </el-button>
  25. <el-button @click="handleExpand"> 展开/折叠 </el-button>
  26. </div>
  27. <div class="mt-4">
  28. <el-table
  29. v-loading="loading"
  30. ref="tableRef"
  31. class="mt-4"
  32. size="large"
  33. :data="lists"
  34. row-key="{TREE_ID}"
  35. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  36. >
  37. {LISTS_VIEW}
  38. <el-table-column label="操作" width="160" fixed="right">
  39. <template #default="{ row }">
  40. <el-button
  41. v-perms="['{PERMS_ADD}']"
  42. type="primary"
  43. link
  44. @click="handleAdd(row.{TREE_ID})"
  45. >
  46. 新增
  47. </el-button>
  48. <el-button
  49. v-perms="['{PERMS_EDIT}']"
  50. type="primary"
  51. link
  52. @click="handleEdit(row)"
  53. >
  54. 编辑
  55. </el-button>
  56. <el-button
  57. v-perms="['{PERMS_DELETE}']"
  58. type="danger"
  59. link
  60. @click="handleDelete(row.{PK})"
  61. >
  62. 删除
  63. </el-button>
  64. </template>
  65. </el-table-column>
  66. </el-table>
  67. </div>
  68. </el-card>
  69. <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
  70. </div>
  71. </template>
  72. <script lang="ts" setup name="{SETUP_NAME}Lists">
  73. import { timeFormat } from '@/utils/util'
  74. import { useDictData } from '@/hooks/useDictOptions'
  75. import { api{UPPER_CAMEL_NAME}Lists, api{UPPER_CAMEL_NAME}Delete } from '@/api/{API_DIR}'
  76. import feedback from '@/utils/feedback'
  77. import EditPopup from './edit.vue'
  78. import type { ElTable, FormInstance } from 'element-plus'
  79. const tableRef = shallowRef<InstanceType<typeof ElTable>>()
  80. const formRef = shallowRef<FormInstance>()
  81. const editRef = shallowRef<InstanceType<typeof EditPopup>>()
  82. let isExpand = false
  83. // 是否显示编辑框
  84. const showEdit = ref(false)
  85. const loading = ref(false)
  86. const lists = ref<any[]>([])
  87. // 查询条件
  88. const queryParams = reactive({
  89. {QUERY_PARAMS}
  90. })
  91. const resetParams = () => {
  92. formRef.value?.resetFields()
  93. getLists()
  94. }
  95. const getLists = async () => {
  96. loading.value = true
  97. try {
  98. const data = await api{UPPER_CAMEL_NAME}Lists(queryParams)
  99. lists.value = data.lists
  100. loading.value = false
  101. } catch (error) {
  102. loading.value = false
  103. }
  104. }
  105. // 选中数据
  106. const selectData = ref<any[]>([])
  107. // 表格选择后回调事件
  108. const handleSelectionChange = (val: any[]) => {
  109. selectData.value = val.map(({ {PK} }) => {PK})
  110. }
  111. // 获取字典数据
  112. const { dictData } = useDictData('{DICT_DATA}')
  113. // 添加
  114. const handleAdd = async ({TREE_ID}?: number) => {
  115. showEdit.value = true
  116. await nextTick()
  117. if ({TREE_ID}) {
  118. editRef.value?.setFormData({
  119. {TREE_PID}: {TREE_ID}
  120. })
  121. }
  122. editRef.value?.open('add')
  123. }
  124. // 编辑
  125. const handleEdit = async (data: any) => {
  126. showEdit.value = true
  127. await nextTick()
  128. editRef.value?.open('edit')
  129. editRef.value?.setFormData(data)
  130. }
  131. // 删除
  132. const handleDelete = async ({PK}: number | any[]) => {
  133. await feedback.confirm('确定要删除?')
  134. await api{UPPER_CAMEL_NAME}Delete({ {PK} })
  135. getLists()
  136. }
  137. const handleExpand = () => {
  138. isExpand = !isExpand
  139. toggleExpand(lists.value, isExpand)
  140. }
  141. const toggleExpand = (children: any[], unfold = true) => {
  142. for (const key in children) {
  143. tableRef.value?.toggleRowExpansion(children[key], unfold)
  144. if (children[key].children) {
  145. toggleExpand(children[key].children!, unfold)
  146. }
  147. }
  148. }
  149. getLists()
  150. </script>