import search from './components/search/index.vue' export default { name: "tree", props: { trees: { type: Array, default: () => { return [] } }, //是否开启选中 isCheck: { type: Boolean, default: () => { return false } }, parent:{ type: Boolean, default: () => { return false } }, max:{ type: Number, default: () => { return 2 } }, checkList: { type: Array, default: () => [] }, parentList: { type: Array, default: () => [] }, searchIf: { type: Boolean, default: () => true }, props: { type: Object, default: () => { return { label: 'name', children: 'children', multiple: false, checkStrictly: false,//不关联 } } } }, data() { return { isre: false, tree: this.trees, newNum: 0, oldNum: 0, allData: this.trees, tree_stack: [1], parent_data: this.parentList||[],//选择父辈 searchResult: [], tochild: false, newCheckList: this.checkList, scrollLeft: 999, nodePathArray: [] } }, components: { search }, mounted() { if(this.props.multiple&&this.props.checkStrictly){ if(this.newCheckList.length!=0){ this.checkAllChoose(); return } for(let i = 0; i0) { this.getNodeRoute(this.allData,this.newCheckList[0].id) let arr = this.nodePathArray.reverse() if(arr.length == 0)return this.tree_stack = this.tree_stack.concat(arr); this.tree = this.tree_stack[this.tree_stack.length-1].children; } }, methods: { //多选 async checkboxChange(item, index, bx, qx) { var that = this; if(!this.props.multiple) return; let findIdex = that.newCheckList.findIndex(e=>{return item.id==e.id}); if (findIdex>-1) { //反选 if (that.props.checkStrictly) {//关联子级 if (item.hasChildren) {//用户 that.newCheckList.splice(findIdex,1) } else {//非用户,取消所有下一级 that.getIdBydelete(item.children) } } else { that.newCheckList.splice(findIdex,1) } } else { //选中 if (!item.hasChildren&&that.props.checkStrictly) {//选中下一级 if(qx||bx){//取消下级 await that.getIdBydelete(item.children); item.qx = 0;item.bx = 0; } else{ item.qx = 1;item.bx = 0; await that.chooseChild(item.children,item.id); } that.$forceUpdate() return } // if(item.hasChildren&&this.props.checkStrictly) this.getNodeRoute(this.allData,item.id); that.newCheckList.push({...item}); } }, // 取消下一级的选中 getIdBydelete(arr) { arr.forEach(e=>{ if(e.hasChildren){ for(var i = 0; i 0) { that.tree = item[children]; if (that.tree_stack[0].id == item.id) { } else { that.tree_stack.push(item); } } this.$nextTick(() => { this.scrollLeft += 200; }) if(this.props.checkStrictly) this.checkAllChoose(); this.$forceUpdate() }, //搜索 confirmSearch(val) { this.searchResult = [] this.search(this.allData, val) this.isre = true this.tree_stack.splice(1, 6666) uni.showLoading({ title: '正在查找' }) setTimeout(() => { this.tree = this.searchResult uni.hideLoading() }, 300) }, search(data, keyword) { var that = this let children = that.props.children for (var i = 0, len = data.length; i < len; i++) { if (data[i].name.indexOf(keyword) >= 0) { that.searchResult.push(data[i]) } if (!data[i].hasChildren && data[i][children].length > 0) { that.search(data[i][children], keyword) } } }, checkAllChoose(){ let o = false,t = true; this.tree.forEach((e,i)=>{ console.log(e.hasChildren,"****"); if(!e.hasChildren){ e.qx = o; e.bx = o; let num2 = this.computAllNumber(e.children); if(this.newNum!=0&&this.oldNum!=0){ if(this.newNum==this.oldNum) { e.qx = t;e.bx = o; }else{ e.qx = o;e.bx = t; } } if(this.newNum!=0&&this.oldNum == 0){ this.$set(this.tree[i],'bx',o); this.$set(this.tree[i],'qx',o); } this.$forceUpdate() this.newNum=0 this.oldNum=0 } }) }, computAllNumber(arr) { for(let j = 0; j 2) { that.tree_stack.forEach((item, i) => { if (i > index) { that.tree_stack.splice(i, max) } }) } else if (index == that.tree_stack.length - 1) { } else { that.tree_stack.splice(that.tree_stack.length - 1, 1) } that.tree = item[that.props.children] } if(this.props.checkStrictly) this.checkAllChoose(); this.$forceUpdate() }, backConfirm(){ this.$emit('sendValue',this.newCheckList,'back') } } }