index.vue 5.6 KB

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