123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- 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 '-1'
- }
- },
- 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 {
- isEnd: false,
- categoryCode: '',
- pageIndex: 1,
- 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; i<this.tree.length;i++){
- this.$set(this.tree[i],'bx',0)
- this.$set(this.tree[i],'qx',0)
- }
- }
- if(!this.props.multiple&&this.newCheckList.length>0) {
- 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: {
- getList() {
- this.pageIndex += 1
- this.$http.Material({
- categoryCode: this.categoryCode,
- pageIndex: this.pageIndex,
- pageSize: 20
- }).then(res => {
- console.log(res)
- if(res.code === 0) {
- if(res.result.isLastPage) {
- this.loadStatus = "nomore"
- }else {
- this.loadStatus = "loadmore"
- }
- let array = res.result.list
- array.forEach(item => {
- item.children = []
- item.user = true
- })
- this.tree.push(...array)
- }
- })
- },
-
- 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.user) {
- that.newCheckList.splice(findIdex,1)
- } else {
- that.getIdBydelete(item.children)
- }
- } else {
- that.newCheckList.splice(findIdex,1)
- }
- } else {
- if (!item.user&&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.$emit('sendValue', that.newCheckList);
- that.$forceUpdate()
- return
- }
-
- that.newCheckList.push({...item});
- }
- that.$emit('sendValue', that.newCheckList)
- },
-
- getIdBydelete(arr) {
- arr.forEach(e=>{
- if(e.user){
- for(var i = 0; i<this.newCheckList.length;i++){
- if(e.id == this.newCheckList[i].id) {
- this.newCheckList.splice(i,1)
- break;
- }
- }
- }
- if(!e.user){
- this.getIdBydelete(e.children)
- }
- })
- },
-
-
-
-
-
-
-
-
-
-
-
- chooseChild(arr,pid) {
- let that = this;
- for (var i = 0, len = arr.length; i < len; i++) {
- let item = arr[i];
- if(item.user) {
- that.newCheckList.push({...item,tree_stackId:pid})
- }
- if (!item.user) {
- this.chooseChild(item.children,item.id)
- }
- }
- },
-
-
- getNodeRoute(tree, targetId) {
- for (let index = 0; index < tree.length; index++) {
- if (tree[index].children) {
- let endRecursiveLoop = this.getNodeRoute(tree[index].children, targetId)
- if (endRecursiveLoop) {
- this.nodePathArray.push(tree[index])
- return true
- }
- }
- if (tree[index].id === targetId) {
- return true
- }
- }
- },
-
-
- checkbox(item, index) {
- var that = this;
- that.newCheckList = []
- that.newCheckList.push(that.tree[index])
- that.$emit('sendValue', that.newCheckList)
- },
-
- toChildren(item) {
- if(item.isEnd){
- this.isEnd = true
- this.categoryCode = item.value
- this.$http.Material({
- categoryCode: item.value,
- pageIndex: this.pageIndex || 1,
- pageSize: 20
- }).then(res => {
- if(res.code === 0) {
- if(res.result.isLastPage) {
- this.loadStatus = "nomore"
- }else {
- this.loadStatus = "loadmore"
- }
- this.tree = res.result.list
- this.tree.forEach(item => {
- item.children = []
- item.user = true
- })
- }
- })
- } else {
- if(item.user) return
- var that = this;
- this.tochild = true;
- let children = that.props.children;
- if (!item.user && item[children].length > 0) {
- that.tree = item[children];
- if (that.tree_stack[0].id == item.id) {
- that.tree_stack.push(item);
- } else {
- console.log('??????')
- that.tree_stack.push(item);
- console.log(that.tree_stack)
- }
- }
- this.$nextTick(() => {
- this.scrollLeft += 200;
- })
- if(this.props.checkStrictly) this.checkAllChoose();
- this.$forceUpdate()
- }
- },
-
- confirmSearch(val) {
-
-
- this.isre = true
- this.tree_stack.splice(1, 6666)
-
-
-
-
-
-
-
- this.pageIndex = 1
- this.$http.Material({
- key: val,
- pageIndex: this.pageIndex,
- pageSize: 20
- }).then(res => {
- if(res.code === 0) {
- this.tree = res.result.list
- this.tree.forEach(item => {
- item.children = []
- item.user = true
- })
- }
- })
- },
- 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].user && data[i][children].length > 0) {
- that.search(data[i][children], keyword)
- }
- }
- },
-
- checkAllChoose(){
- let o = false,t = true;
- this.tree.forEach((e,i)=>{
- if(!e.user){
- 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<arr.length;j++){
- var e = arr[j];
- if(arr[j].user) {this.newNum ++;}
- this.checkSum(e.id)
- if(!e.user){
- this.computAllNumber(e.children)
- }
- }
- },
-
- checkSum(id){
- for(let i = 0; i<this.newCheckList.length;i++){
- if(id == this.newCheckList[i].id) {
- this.oldNum++;
- break
- }
- }
- },
-
-
- backTree(item, index) {
- this.newCheckList = []
- this.pageIndex = 1
-
- this.isEnd = false
- let that = this,max = 66666;
- if (index == -1) {
- that.tree = that.allData
- that.tree_stack.splice(1, max)
- that.isre = false
- that.$refs.sea.clears()
- } else if (index == -2) {
- that.tree = that.searchResult
- that.tree_stack.splice(1, max)
- } else {
- if (that.tree_stack.length - index > 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(){
-
- if(this.newCheckList[0]) {
- this.$emit('sendValue',this.newCheckList,'back')
- }
- }
- }
- }
|