Blame view

ant-design-vue-jeecg/src/views/system/SysCategoryList.vue 10.8 KB
肖超群 authored
1
2
<template>
  <a-card :bordered="false">
肖超群 authored
3
肖超群 authored
4
5
6
7
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('分类字典')">导出</a-button>
肖超群 authored
8
9
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
肖超群 authored
10
11
12
13
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
肖超群 authored
14
15
16
17
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
肖超群 authored
18
        </a-menu>
肖超群 authored
19
20
21
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
肖超群 authored
22
23
24
25
26
27
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
肖超群 authored
28
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
29
        style="font-weight: 600">{{ selectedRowKeys.length }}</a> 项
肖超群 authored
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :expandedRowKeys="expandedRowKeys"
        @change="handleTableChange"
        @expand="handleExpand"
        v-bind="tableProps">
肖超群 authored
45
肖超群 authored
46
47
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
肖超群 authored
48
          <a-divider type="vertical"/>
肖超群 authored
49
50
51
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
肖超群 authored
52
          <a-divider type="vertical"/>
肖超群 authored
53
54
55
56
57
58
59
60
61
62
63
64
          <a @click="handleAddSub(record)">添加下级</a>
        </span>

      </a-table>
    </div>

    <sysCategory-modal ref="modalForm" @ok="modalFormOk"></sysCategory-modal>
  </a-card>
</template>

<script>
肖超群 authored
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {getAction} from '@/api/manage'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import SysCategoryModal from './modules/SysCategoryModal'
import {deleteAction} from '@/api/manage'

export default {
  name: "SysCategoryList",
  mixins: [JeecgListMixin],
  components: {
    SysCategoryModal
  },
  data() {
    return {
      description: '分类字典管理页面',
      // 表头
      columns: [
        {
          title: '分类名称',
          align: "left",
          dataIndex: 'name'
肖超群 authored
85
        },
肖超群 authored
86
87
88
89
        {
          title: '分类编码',
          align: "left",
          dataIndex: 'code'
肖超群 authored
90
        },
肖超群 authored
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        list: "/sys/category/rootList",
        childList: "/sys/category/childList",
        getChildListBatch: "/sys/category/getChildListBatch",
        delete: "/sys/category/delete",
        deleteBatch: "/sys/category/deleteBatch",
        exportXlsUrl: "/sys/category/exportXls",
        importExcelUrl: "sys/category/importExcel",
      },
      expandedRowKeys: [],
      hasChildrenField: "hasChild",
      pidField: "pid",
      dictOptions: {},
      subExpandedKeys: [],
    }
  },
  computed: {
    importExcelUrl() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
    tableProps() {
      let _this = this
      return {
        // 列表项是否可选择
        rowSelection: {
          selectedRowKeys: _this.selectedRowKeys,
          onChange: (selectedRowKeys) => _this.selectedRowKeys = selectedRowKeys
        }
肖超群 authored
126
      }
肖超群 authored
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    }
  },
  methods: {
    loadData(arg) {
      if (arg == 1) {
        this.ipagination.current = 1
      }
      this.loading = true
      let params = this.getQueryParams()
      return new Promise((resolve) => {
        getAction(this.url.list, params).then(res => {
          if (res.success) {
            let result = res.result
            if (Number(result.total) > 0) {
              this.ipagination.total = Number(result.total)
              this.dataSource = this.getDataByResult(res.result.records)
              //update--begin--autor:lvdandan-----date:20201204------for:JT-31 删除成功后默认展开已展开信息
              return this.loadDataByExpandedRows(this.dataSource)
              //update--end--autor:lvdandan-----date:20201204------for:JT-31 删除成功后默认展开已展开信息
            } else {
              this.ipagination.total = 0
              this.dataSource = []
            }
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      })
肖超群 authored
157
    },
肖超群 authored
158
159
160
161
162
163
164
    getDataByResult(result) {
      if (result && result.length > 0) {
        return result.map(item => {
          //判断是否标记了带有子节点
          if (item[this.hasChildrenField] == '1') {
            let loadChild = {id: item.id + '_loadChild', name: 'loading...', isLoading: true}
            item.children = [loadChild]
肖超群 authored
165
          }
肖超群 authored
166
167
          return item
        })
肖超群 authored
168
169
      }
    },
肖超群 authored
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    handleExpand(expanded, record) {
      // 判断是否是展开状态
      if (expanded) {
        this.expandedRowKeys.push(record.id)
        if (record.children.length > 0 && record.children[0].isLoading === true) {
          let params = this.getQueryParams();//查询条件
          params[this.pidField] = record.id
          getAction(this.url.childList, params).then((res) => {
            if (res.success) {
              if (res.result && res.result.length > 0) {
                record.children = this.getDataByResult(res.result)
                this.dataSource = [...this.dataSource]
              } else {
                record.children = ''
                record.hasChildrenField = '0'
肖超群 authored
185
              }
肖超群 authored
186
            } else {
肖超群 authored
187
188
189
190
              this.$message.warning(res.message)
            }
          })
        }
肖超群 authored
191
192
193
194
      } else {
        let keyIndex = this.expandedRowKeys.indexOf(record.id)
        if (keyIndex >= 0) {
          this.expandedRowKeys.splice(keyIndex, 1);
肖超群 authored
195
        }
肖超群 authored
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
      }
    },
    initDictConfig() {
    },
    modalFormOk(formData, arr) {
      if (!formData.id) {
        this.addOk(formData, arr)
      } else {
        this.editOk(formData, this.dataSource)
        this.dataSource = [...this.dataSource]
      }
    },
    editOk(formData, arr) {
      if (arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].id == formData.id) {
            arr[i] = formData
            break
          } else {
            this.editOk(formData, arr[i].children)
肖超群 authored
216
217
          }
        }
肖超群 authored
218
219
220
221
222
223
224
225
226
227
      }
    },
    async addOk(formData, arr) {
      if (!formData[this.pidField]) {
        this.loadData()
      } else {
        this.expandedRowKeys = []
        console.log("22222", arr)
        for (let i of arr) {
          await this.expandTreeNode(i)
肖超群 authored
228
        }
肖超群 authored
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
      }
    },
    expandTreeNode(nodeId) {
      return new Promise((resolve, reject) => {
        this.getFormDataById(nodeId, this.dataSource)
        let row = this.parentFormData
        this.expandedRowKeys.push(nodeId)
        let params = this.getQueryParams();//查询条件
        params[this.pidField] = nodeId
        getAction(this.url.childList, params).then((res) => {
          console.log("11111", res)
          if (res.success) {
            if (res.result && res.result.length > 0) {
              row.children = this.getDataByResult(res.result)
              this.dataSource = [...this.dataSource]
              resolve()
            } else {
              row.children = ''
              row.hasChildrenField = '0'
肖超群 authored
248
249
              reject()
            }
肖超群 authored
250
251
252
          } else {
            reject()
          }
肖超群 authored
253
        })
肖超群 authored
254
255
256
257
258
259
260
261
262
      })
    },
    getFormDataById(id, arr) {
      if (arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].id == id) {
            this.parentFormData = arr[i]
          } else {
            this.getFormDataById(id, arr[i].children)
肖超群 authored
263
264
          }
        }
肖超群 authored
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
      }
    },
    handleAddSub(record) {
      this.subExpandedKeys = [];
      this.getExpandKeysByPid(record.id, this.dataSource, this.dataSource)
      this.$refs.modalForm.subExpandedKeys = this.subExpandedKeys;
      this.$refs.modalForm.title = "添加子分类";
      this.$refs.modalForm.edit({'pid': record.id});
      this.$refs.modalForm.disableSubmit = false;
    },
    handleDelete: function (record) {
      let that = this;
      deleteAction(that.url.delete, {id: record.id}).then((res) => {
        if (res.success) {
          //update--begin--autor:lvdandan-----date:20201204------for:JT-31 删除成功后默认展开已展开信息
          that.loadData();
          //update--end--autor:lvdandan-----date:20201204------for:JT-31 删除成功后默认展开已展开信息
        } else {
          that.$message.warning(res.message);
        }
      });
    },
    // 添加子分类时获取所有父级id
    getExpandKeysByPid(pid, arr, all) {
      if (pid && arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].id == pid) {
            this.subExpandedKeys.push(arr[i].id)
            this.getExpandKeysByPid(arr[i]['pid'], all, all)
肖超群 authored
294
          } else {
肖超群 authored
295
            this.getExpandKeysByPid(pid, arr[i].children, all)
肖超群 authored
296
297
          }
        }
肖超群 authored
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
      }
    },
    // 根据已展开的行查询数据(用于保存后刷新时异步加载子级的数据)
    loadDataByExpandedRows(dataList) {
      if (this.expandedRowKeys.length > 0) {
        return getAction(this.url.getChildListBatch, {parentIds: this.expandedRowKeys.join(',')}).then(res => {
          if (res.success && res.result.records.length > 0) {
            //已展开的数据批量子节点
            let records = res.result.records
            const listMap = new Map();
            for (let item of records) {
              let pid = item[this.pidField];
              if (this.expandedRowKeys.join(',').includes(pid)) {
                let mapList = listMap.get(pid);
                if (mapList == null) {
                  mapList = [];
肖超群 authored
314
                }
肖超群 authored
315
316
                mapList.push(item);
                listMap.set(pid, mapList);
肖超群 authored
317
              }
肖超群 authored
318
319
320
321
322
323
324
325
326
327
            }
            let childrenMap = listMap;
            let fn = (list) => {
              if (list) {
                list.forEach(data => {
                  if (this.expandedRowKeys.includes(data.id)) {
                    data.children = this.getDataByResult(childrenMap.get(data.id))
                    fn(data.children)
                  }
                })
肖超群 authored
328
329
              }
            }
肖超群 authored
330
331
332
333
334
335
336
337
338
            fn(dataList)
          }
        })
      } else {
        return Promise.resolve()
      }
    },
肖超群 authored
339
  }
肖超群 authored
340
}
肖超群 authored
341
342
</script>
<style scoped>
肖超群 authored
343
@import '~@assets/less/common.less'
肖超群 authored
344
</style>