| <script> |
| export default { |
| name: 'ElSelectTree', |
| props: { |
| value: { |
| type: String, |
| require: true, |
| }, |
| list: { |
| |
| type: Array, |
| default: () => { |
| return []; |
| }, |
| }, |
| placeholder: { |
| type: String, |
| default: '请选择', |
| }, |
| }, |
| |
| data() { |
| return { |
| newVal: '', |
| label: '', |
| selectTree: {}, |
| defaultExpandedKeys: [], |
| searchData: [], |
| openSearch: false, |
| }; |
| }, |
| |
| watch: { |
| value: { |
| handler(value) { |
| this.newVal = value; |
| if (value) { |
| this.defaultExpandedKeys = [value]; |
| this.$refs.tree.setCurrentKey(value); |
| const check = this.findLabel(this.list, value); |
| if (check) { |
| this.label = check.label; |
| } |
| } |
| }, |
| deep: true, |
| immediate: true, |
| }, |
| newVal(value) { |
| const check = this.findLabel(this.list, value); |
| if (check) { |
| this.label = check.label; |
| } |
| this.$emit('input', value); |
| }, |
| }, |
| created() { |
| |
| Object.getPrototypeOf(this.$options.components).ElSelect.options.methods.handleFocus = () => {}; |
| }, |
| mounted() {}, |
| methods: { |
| |
| handleNodeClick(data) { |
| this.newVal = data.value; |
| this.label = data.label; |
| this.$refs.elSelect.blur(); |
| this.searchData = []; |
| this.openSearch = false; |
| this.$emit('change', data); |
| }, |
| |
| async filterTree(value) { |
| if (value) { |
| this.openSearch = true; |
| this.searchData = []; |
| await this.findItem(this.list, value); |
| } else { |
| this.openSearch = false; |
| this.searchData = []; |
| } |
| }, |
| |
| findItem(arr, value) { |
| return new Promise(resolve => { |
| for (let i = 0; i < arr.length; i++) { |
| const item = arr[i]; |
| if (item.label.includes(value)) { |
| this.searchData.push(item); |
| } else if (item.children && item.children.length > 0) { |
| this.findItem(item.children, value); |
| } |
| } |
| resolve(true); |
| }); |
| }, |
| |
| findLabel(arr, value) { |
| for (let i = 0; i < arr.length; i++) { |
| const item = arr[i]; |
| if (item.value === value) { |
| return item; |
| } else if (item.children && item.children.length > 0) { |
| const result = this.findLabel(item.children, value); |
| if (result) { |
| return result; |
| } |
| } |
| } |
| }, |
| |
| clear() { |
| this.$refs.tree.setCurrentKey(null); |
| this.label = ''; |
| this.newVal = ''; |
| this.searchData = []; |
| this.openSearch = false; |
| this.$emit('change', {}); |
| }, |
| }, |
| }; |
| </script> |