Blame view

ant-design-vue-jeecg/src/components/jeecg/JTreeTable.vue 4.64 KB
1
2
3
4
5
<template>
  <a-table
    :rowKey="rowKey"
    :columns="columns"
    :dataSource="dataSource"
6
7
8
9
10
    :expandedRowKeys="expandedRowKeys"
    v-bind="tableAttrs"
    v-on="$listeners"
    @expand="handleExpand"
    @expandedRowsChange="expandedRowKeys=$event">
11
12
13
14
15
16

    <template v-for="(slotItem) of slots" :slot="slotItem" slot-scope="text, record, index">
      <slot :name="slotItem" v-bind="{text,record,index}"></slot>
    </template>

  </a-table>
17
18
19
20
21
22
23
24
25
26
27
28
</template>

<script>
  import { getAction } from '@/api/manage'

  export default {
    name: 'JTreeTable',
    props: {
      rowKey: {
        type: String,
        default: 'id'
      },
29
30
31
32
33
34
35
      // 根据什么查询,如果传递 id 就根据 id 查询
      queryKey: {
        type: String,
        default: 'parentId'
      },
      queryParams: {
        type: Object,
36
        default: () => ({})
37
38
39
40
41
42
      },
      // 查询顶级时的值,如果顶级为0,则传0
      topValue: {
        type: String,
        default: null
      },
43
44
45
46
47
48
49
50
51
52
53
54
55
56
      columns: {
        type: Array,
        required: true
      },
      url: {
        type: String,
        required: true
      },
      childrenUrl: {
        type: String,
        default: null
      },
      tableProps: {
        type: Object,
57
58
59
60
61
62
63
64
65
66
67
        default: () => ({})
      },
      /** 是否在创建组件的时候就查询数据 */
      immediateRequest: {
        type: Boolean,
        default: true
      },
      condition:{
        type:String,
        default:'',
        required:false
68
69
70
71
      }
    },
    data() {
      return {
72
73
        dataSource: [],
        expandedRowKeys: []
74
75
76
77
78
79
80
81
82
      }
    },
    computed: {
      getChildrenUrl() {
        if (this.childrenUrl) {
          return this.childrenUrl
        } else {
          return this.url
        }
83
84
85
86
87
88
89
90
91
      },
      slots() {
        let slots = []
        for (let column of this.columns) {
          if (column.scopedSlots && column.scopedSlots.customRender) {
            slots.push(column.scopedSlots.customRender)
          }
        }
        return slots
92
93
94
      },
      tableAttrs() {
        return Object.assign(this.$attrs, this.tableProps)
95
96
97
98
99
100
101
102
      }
    },
    watch: {
      queryParams: {
        deep: true,
        handler() {
          this.loadData()
        }
103
104
105
      }
    },
    created() {
106
      if (this.immediateRequest) this.loadData()
107
108
109
110
    },
    methods: {

      /** 加载数据*/
111
      loadData(id = this.topValue, first = true, url = this.url) {
112
113
114
115
116
117
        this.$emit('requestBefore', { first })

        if (first) {
          this.expandedRowKeys = []
        }
118
119
        let params = Object.assign({}, this.queryParams || {})
        params[this.queryKey] = id
120
121
122
123
        if(this.condition && this.condition.length>0){
          params['condition'] = this.condition
        }
124
        return getAction(url, params).then(res => {
125
126
127
128
129
130
131
132
133
          let list = []
          if (res.result instanceof Array) {
            list = res.result
          } else if (res.result.records instanceof Array) {
            list = res.result.records
          } else {
            throw '返回数据类型不识别'
          }
          let dataSource = list.map(item => {
134
135
            // 判断是否标记了带有子级
            if (item.hasChildren === true) {
136
137
138
139
140
141
              // 查找第一个带有dataIndex的值的列
              let firstColumn
              for (let column of this.columns) {
                firstColumn = column.dataIndex
                if (firstColumn) break
              }
142
              // 定义默认展开时显示的loading子级,实际子级数据只在展开时加载
143
              let loadChild = { id: `${item.id}_loadChild`, [firstColumn]: 'loading...', isLoading: true }
144
145
146
147
148
149
150
              item.children = [loadChild]
            }
            return item
          })
          if (first) {
            this.dataSource = dataSource
          }
151
          this.$emit('requestSuccess', { first, dataSource, res })
152
          return Promise.resolve(dataSource)
153
        }).finally(() => this.$emit('requestFinally', { first }))
154
155
156
157
158
159
160
161
162
163
      },

      /** 点击展开图标时触发 */
      handleExpand(expanded, record) {
        // 判断是否是展开状态
        if (expanded) {
          // 判断子级的首个项的标记是否是“正在加载中”,如果是就加载数据
          if (record.children[0].isLoading === true) {
            this.loadData(record.id, false, this.getChildrenUrl).then(dataSource => {
              // 处理好的数据可直接赋值给children
164
165
166
167
168
              if (dataSource.length === 0) {
                record.children = null
              } else {
                record.children = dataSource
              }
169
170
171
172
173
174
175
176
177
178
179
180
            })
          }
        }
      }

    }
  }
</script>

<style scoped>

</style>