index 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <ContentWrap>
  3. <!-- 搜索工作栏 -->
  4. <el-form
  5. class="-mb-15px"
  6. :model="queryParams"
  7. ref="queryFormRef"
  8. :inline="true"
  9. label-width="68px"
  10. >
  11. <el-form-item label="名字" prop="name">
  12. <el-input
  13. v-model="queryParams.name"
  14. placeholder="请输入名字"
  15. clearable
  16. @keyup.enter="handleQuery"
  17. class="!w-240px"
  18. />
  19. </el-form-item>
  20. <el-form-item>
  21. <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
  22. <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
  23. <el-button
  24. type="primary"
  25. plain
  26. @click="openForm('create')"
  27. v-hasPermi="['infra:category:create']"
  28. >
  29. <Icon icon="ep:plus" class="mr-5px" /> 新增
  30. </el-button>
  31. <el-button
  32. type="success"
  33. plain
  34. @click="handleExport"
  35. :loading="exportLoading"
  36. v-hasPermi="['infra:category:export']"
  37. >
  38. <Icon icon="ep:download" class="mr-5px" /> 导出
  39. </el-button>
  40. <el-button type="danger" plain @click="toggleExpandAll">
  41. <Icon icon="ep:sort" class="mr-5px" /> 展开/折叠
  42. </el-button>
  43. </el-form-item>
  44. </el-form>
  45. </ContentWrap>
  46. <!-- 列表 -->
  47. <ContentWrap>
  48. <el-table
  49. v-loading="loading"
  50. :data="list"
  51. :stripe="true"
  52. :show-overflow-tooltip="true"
  53. row-key="id"
  54. :default-expand-all="isExpandAll"
  55. v-if="refreshTable"
  56. >
  57. <el-table-column label="编号" align="center" prop="id" />
  58. <el-table-column label="名字" align="center" prop="name" />
  59. <el-table-column label="父编号" align="center" prop="parentId" />
  60. <el-table-column label="操作" align="center">
  61. <template #default="scope">
  62. <el-button
  63. link
  64. type="primary"
  65. @click="openForm('update', scope.row.id)"
  66. v-hasPermi="['infra:category:update']"
  67. >
  68. 编辑
  69. </el-button>
  70. <el-button
  71. link
  72. type="danger"
  73. @click="handleDelete(scope.row.id)"
  74. v-hasPermi="['infra:category:delete']"
  75. >
  76. 删除
  77. </el-button>
  78. </template>
  79. </el-table-column>
  80. </el-table>
  81. <!-- 分页 -->
  82. <Pagination
  83. :total="total"
  84. v-model:page="queryParams.pageNo"
  85. v-model:limit="queryParams.pageSize"
  86. @pagination="getList"
  87. />
  88. </ContentWrap>
  89. <!-- 表单弹窗:添加/修改 -->
  90. <CategoryForm ref="formRef" @success="getList" />
  91. </template>
  92. <script setup lang="ts">
  93. import { handleTree } from '@/utils/tree'
  94. import download from '@/utils/download'
  95. import * as CategoryApi from '@/api/infra/demo'
  96. import CategoryForm from './CategoryForm.vue'
  97. defineOptions({ name: 'InfraCategory' })
  98. const message = useMessage() // 消息弹窗
  99. const { t } = useI18n() // 国际化
  100. const loading = ref(true) // 列表的加载中
  101. const list = ref([]) // 列表的数据
  102. const queryParams = reactive({
  103. name: undefined,
  104. })
  105. const queryFormRef = ref() // 搜索的表单
  106. const exportLoading = ref(false) // 导出的加载中
  107. /** 查询列表 */
  108. const getList = async () => {
  109. loading.value = true
  110. try {
  111. const data = await CategoryApi.getCategoryList(queryParams)
  112. list.value = handleTree(data, 'id', 'parentId')
  113. } finally {
  114. loading.value = false
  115. }
  116. }
  117. /** 搜索按钮操作 */
  118. const handleQuery = () => {
  119. queryParams.pageNo = 1
  120. getList()
  121. }
  122. /** 重置按钮操作 */
  123. const resetQuery = () => {
  124. queryFormRef.value.resetFields()
  125. handleQuery()
  126. }
  127. /** 添加/修改操作 */
  128. const formRef = ref()
  129. const openForm = (type: string, id?: number) => {
  130. formRef.value.open(type, id)
  131. }
  132. /** 删除按钮操作 */
  133. const handleDelete = async (id: number) => {
  134. try {
  135. // 删除的二次确认
  136. await message.delConfirm()
  137. // 发起删除
  138. await CategoryApi.deleteCategory(id)
  139. message.success(t('common.delSuccess'))
  140. // 刷新列表
  141. await getList()
  142. } catch {}
  143. }
  144. /** 导出按钮操作 */
  145. const handleExport = async () => {
  146. try {
  147. // 导出的二次确认
  148. await message.exportConfirm()
  149. // 发起导出
  150. exportLoading.value = true
  151. const data = await CategoryApi.exportCategory(queryParams)
  152. download.excel(data, '分类.xls')
  153. } catch {
  154. } finally {
  155. exportLoading.value = false
  156. }
  157. }
  158. /** 展开/折叠操作 */
  159. const isExpandAll = ref(true) // 是否展开,默认全部展开
  160. const refreshTable = ref(true) // 重新渲染表格状态
  161. const toggleExpandAll = async () => {
  162. refreshTable.value = false
  163. isExpandAll.value = !isExpandAll.value
  164. await nextTick()
  165. refreshTable.value = true
  166. }
  167. /** 初始化 **/
  168. onMounted(() => {
  169. getList()
  170. })
  171. </script>