index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div class="system-dept-container">
  3. <el-card shadow="hover">
  4. <div class="system-dept-search mb15">
  5. <el-form :inline="true">
  6. <el-form-item label="分类名称">
  7. <el-input size="default" v-model="tableData.param.name" placeholder="请输入分类名称" class="w-50" clearable />
  8. </el-form-item>
  9. <el-form-item>
  10. <el-button size="default" type="primary" class="ml10" @click="getCateList">
  11. <el-icon>
  12. <ele-Search />
  13. </el-icon>
  14. 查询
  15. </el-button>
  16. <el-button size="default" type="success" class="ml10" @click="onOpenAdd">
  17. <el-icon>
  18. <ele-FolderAdd />
  19. </el-icon>
  20. 新增分类
  21. </el-button>
  22. </el-form-item>
  23. </el-form>
  24. </div>
  25. <el-table :data="tableData.data" style="width: 100%" row-key="id" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" v-loading="tableData.loading">
  26. <el-table-column prop="name" label="分类名称" show-overflow-tooltip> </el-table-column>
  27. <el-table-column prop="createdAt" label="创建时间" align="center" min-width="180"></el-table-column>
  28. <el-table-column label="操作" align="center" width="140" fixed="right">
  29. <template #default="scope">
  30. <el-button size="small" type="text" @click="onOpenAdd(scope.row)">新增</el-button>
  31. <el-button size="small" text type="warning" @click="onOpenEdit(scope.row)">修改</el-button>
  32. <el-button size="small" text type="danger" @click="onTabelRowDel(scope.row)">删除</el-button>
  33. </template>
  34. </el-table-column>
  35. </el-table>
  36. </el-card>
  37. <EditCate ref="editDeptRef" @getCateList="getCateList" />
  38. </div>
  39. </template>
  40. <script lang="ts">
  41. import { ref, toRefs, reactive, onMounted, defineComponent } from 'vue';
  42. import { ElMessageBox, ElMessage } from 'element-plus';
  43. import EditCate from './component/edit.vue';
  44. import api from '/@/api/device';
  45. // 定义接口来定义对象的类型
  46. interface TableDataRow {
  47. id: number;
  48. parentId: number;
  49. name: string;
  50. status: number;
  51. orderNum: number;
  52. leader: string;
  53. children?: TableDataRow[];
  54. }
  55. interface TableDataState {
  56. tableData: {
  57. data: Array<TableDataRow>;
  58. loading: boolean;
  59. param: {
  60. name: string;
  61. status: number;
  62. };
  63. };
  64. }
  65. export default defineComponent({
  66. name: 'deviceCate',
  67. components: { EditCate },
  68. setup() {
  69. const editDeptRef = ref();
  70. const state = reactive<TableDataState>({
  71. tableData: {
  72. data: [],
  73. loading: false,
  74. param: {
  75. name: '',
  76. status: -1,
  77. },
  78. },
  79. });
  80. // 初始化表格数据
  81. const initTableData = () => {
  82. getCateList();
  83. };
  84. const getCateList = () => {
  85. state.tableData.loading = true;
  86. api.category.getList(state.tableData.param).then((res: any) => {
  87. state.tableData.data = res.category;
  88. }).finally(() => (state.tableData.loading = false));
  89. };
  90. // 打开新增菜单弹窗
  91. const onOpenAdd = (row?: TableDataRow) => {
  92. editDeptRef.value.openDialog(row?.id);
  93. };
  94. // 打开编辑菜单弹窗
  95. const onOpenEdit = (row: TableDataRow) => {
  96. editDeptRef.value.openDialog(row);
  97. };
  98. // 删除当前行
  99. const onTabelRowDel = (row: TableDataRow) => {
  100. ElMessageBox.confirm(`此操作将永久删除分类:${row.name}, 是否继续?`, '提示', {
  101. confirmButtonText: '删除',
  102. cancelButtonText: '取消',
  103. type: 'warning',
  104. }).then(() => {
  105. api.category.del(row.id).then(() => {
  106. ElMessage.success('删除成功');
  107. getCateList();
  108. });
  109. });
  110. };
  111. // 页面加载时
  112. onMounted(() => {
  113. initTableData();
  114. });
  115. return {
  116. editDeptRef,
  117. getCateList,
  118. onOpenAdd,
  119. onOpenEdit,
  120. onTabelRowDel,
  121. ...toRefs(state),
  122. };
  123. },
  124. });
  125. </script>