jinlin
2024-02-26 6f0714843341b168573ad0272069f7af2d3d2b87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
126
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<template>
  <div>
    <el-input v-model="text" @click.native="open()" suffix-icon="el-icon-arrow-down" slot="reference" :placeholder="placeholder"/>
    <zt-dialog ref="dialog" :append-to-body="true" :title="title" :hasConfirm="true" @confirm="confirm">
      <el-row>
        <el-col class="tree" :span="14">
          <el-container>
            <el-main>
              <el-input class="input-filter" v-model="filterText" size="small" clearableplaceholder="输入名称进行过滤"></el-input>
              <zt-tree-selector ref="tree" v-model="treeValue" :idField="idField" :textField="textField" :parentIdField="parentIdField"
                :datas="datas" :simple="simple" :multiple="multiple" :leaf-only="leafOnly" :lazy="lazy" :check-strictly="checkStrictly" :expandLevel="expandLevel"
                :disabled-filter="disabledFilter" @selected="onSelected"/>
            </el-main>
          </el-container>
        </el-col>
        <el-col :span="10">
          <el-container>
            <el-main>
              <el-input class="input-selected" v-model="selectedText" size="small" readonly/>
              <div v-for="item in selectedData" :key="item[idField]" style="margin-top: 10px">
                <div>
                  <label style="width: 80%; display: inline-block"> {{item[textField]}}</label>
                  <el-link v-if="multiple" @click="cancelSelected(item)" type="danger">
                    <i class="el-icon-close" title="删除"></i>
                  </el-link>
                </div>
              </div>
            </el-main>
          </el-container>
        </el-col>
      </el-row>
    </zt-dialog>
  </div>
 
</template>
<script>
  import {debounce} from 'lodash'
  export default {
    name: 'ZtTreeDialogSelector',
    components: {},
    props: {
      value: [String, Array],
      title: String,
      idField: {
        type: String,
        default: 'id'
      },
      textField: {
        type: String,
        default: 'name'
      },
      parentIdField: {
        type: String,
        default: 'pid'
      },
      url: String,
      // datas: [String, Array],
      simple: { // 简单模式
        type: Boolean,
        default: false
      },
      multiple: {
        type: Boolean,
        default: false
      },
      leafOnly: { // 是否只能选择叶子节点
        type: Boolean,
        default: false
      },
      checkStrictly: {
        type: Boolean,
        default: false
      },
      lazy: { // 懒加载子节点
        type: Boolean,
        default: false
      },
      expandLevel: { // 懒加载展开层级
        type: Number,
        default: 0
      },
      disabledFilter: Function, // 可选的节点
      disabled: {
        type: Boolean,
        default: false
      },
      placeholder: String
    },
    inject: {
      elForm: {
        default: ''
      }
    },
    data() {
      return {
        datas: [],
        treeValue: this.value,
        filterText: '',
        text: null,
        selectedData: []
      }
    },
    computed: {
      selectedText() {
        return `已选择(${this.selectedData.length})`
      }
    },
    watch: {
      value(val, oldval) { // 传递给tree-selector
        this.treeValue = val
        this.setTextOnetime()
      },
      filterText: debounce(function (val) {
        this.$refs.tree.filter(val)
      }, 1000)
    },
    async mounted() {
      let res = await this.$http.get(this.url, null)
      this.datas = res.data
      this.setTextOnetime()
    },
    methods: {
      open() {
        if (this.disabled || (this.elForm || {}).disabled) {
          // 只读
        } else {
          this.$refs.dialog.open()
        }
      },
      onInputChange(val) {
        this.$refs.tree.filter(val)
      },
      setTextOnetime() { // 组件没有初始化时设置text
        if (!this.treeValue) {
          this.text = ''
        }
        if (!this.$refs.tree) {
          if (this.datas) {
            if (this.treeValue) {
              let values = Array.isArray(this.treeValue) ? this.treeValue : this.treeValue.split(',')
              let dataList = this.flattenTrees(this.datas)
              let texts = []
              dataList.forEach(d => {
                if (values.indexOf(d[this.idField]) >= 0) {
                  texts.push(d[this.textField])
                }
              })
              this.text = texts.join(',')
            } else {
              this.text = ''
            }
          }
        }
      },
      onSelected(data) {
        if (!this.multiple) { // 单选
          if (!data) {
            this.text = ('')
          } else {
            this.selectedData = [data]
            this.text = data[this.textField] // combox设置显示值
          }
        } else { // 多选
          let texts = []
          let values = []
          data.forEach(node => {
            texts.push(node[this.textField])
            values.push(node[this.idField])
          })
          this.selectedData = data
          this.text = texts.join(',')
        }
      },
      cancelSelected(node) {
        this.$refs.tree.cancelSelected(node, false)
      },
      confirm() {
        this.$emit('input', this.treeValue)
        this.$emit('confirm', this.treeValue, this.selectedData)
        this.$refs.dialog.close()
      },
      flattenTrees(trees) { // tree转array
        let result = []
        trees.forEach(item => {
          result.push(item)
          item.children && result.push(...this.flattenTrees(item.children))
        })
        return result
      }
    }
  }
</script>
<style lang="scss">
 
  .tree {
    border-right: 1px solid #DCDFE6;
  }
 
  .input-filter, .input-selected {
    margin-bottom: 15px;
  }
 
  .input-selected input {
    border-top: none;
    border-left: none;
    border-right: none;
    border-radius: 0px;
  }
 
</style>