code.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import search from './components/search/index.vue'
  2. export default {
  3. name: "tree",
  4. props: {
  5. trees: {
  6. type: Array,
  7. default: () => {
  8. return []
  9. }
  10. },
  11. //是否开启选中
  12. isCheck: {
  13. type: Boolean,
  14. default: () => {
  15. return false
  16. }
  17. },
  18. parent:{
  19. type: Boolean,
  20. default: () => {
  21. return false
  22. }
  23. },
  24. max:{
  25. type: Number,
  26. default: () => {
  27. return 2
  28. }
  29. },
  30. checkList: {
  31. type: Array,
  32. default: () => []
  33. },
  34. parentList: {
  35. type: Array,
  36. default: () => []
  37. },
  38. searchIf: {
  39. type: Boolean,
  40. default: () => true
  41. },
  42. props: {
  43. type: Object,
  44. default: () => {
  45. return {
  46. label: 'name',
  47. children: 'children',
  48. multiple: false,
  49. checkStrictly: false,//不关联
  50. }
  51. }
  52. }
  53. },
  54. data() {
  55. return {
  56. isre: false,
  57. tree: this.trees,
  58. newNum: 0,
  59. oldNum: 0,
  60. allData: this.trees,
  61. tree_stack: [1],
  62. parent_data: this.parentList||[],//选择父辈
  63. searchResult: [],
  64. tochild: false,
  65. newCheckList: this.checkList,
  66. scrollLeft: 999,
  67. nodePathArray: []
  68. }
  69. },
  70. components: {
  71. search
  72. },
  73. mounted() {
  74. if(this.props.multiple&&this.props.checkStrictly){
  75. if(this.newCheckList.length!=0){
  76. this.checkAllChoose();
  77. return
  78. }
  79. for(let i = 0; i<this.tree.length;i++){
  80. this.$set(this.tree[i],'bx',0)
  81. this.$set(this.tree[i],'qx',0)
  82. }
  83. }
  84. if(!this.props.multiple&&this.newCheckList.length>0) {
  85. this.getNodeRoute(this.allData,this.newCheckList[0].id)
  86. let arr = this.nodePathArray.reverse()
  87. if(arr.length == 0)return
  88. this.tree_stack = this.tree_stack.concat(arr);
  89. this.tree = this.tree_stack[this.tree_stack.length-1].children;
  90. }
  91. },
  92. methods: {
  93. //多选
  94. async checkboxChange(item, index, bx, qx) {
  95. var that = this;
  96. if(!this.props.multiple) return;
  97. let findIdex = that.newCheckList.findIndex(e=>{return item.id==e.id});
  98. if (findIdex>-1) { //反选
  99. if (that.props.checkStrictly) {//关联子级
  100. if (item.hasChildren) {//用户
  101. that.newCheckList.splice(findIdex,1)
  102. } else {//非用户,取消所有下一级
  103. that.getIdBydelete(item.children)
  104. }
  105. } else {
  106. that.newCheckList.splice(findIdex,1)
  107. }
  108. } else { //选中
  109. if (!item.hasChildren&&that.props.checkStrictly) {//选中下一级
  110. if(qx||bx){//取消下级
  111. await that.getIdBydelete(item.children);
  112. item.qx = 0;item.bx = 0;
  113. }
  114. else{
  115. item.qx = 1;item.bx = 0;
  116. await that.chooseChild(item.children,item.id);
  117. }
  118. that.$forceUpdate()
  119. return
  120. }
  121. // if(item.hasChildren&&this.props.checkStrictly) this.getNodeRoute(this.allData,item.id);
  122. that.newCheckList.push({...item});
  123. }
  124. },
  125. // 取消下一级的选中
  126. getIdBydelete(arr) {
  127. arr.forEach(e=>{
  128. if(e.hasChildren){
  129. for(var i = 0; i<this.newCheckList.length;i++){
  130. if(e.id == this.newCheckList[i].id) {
  131. this.newCheckList.splice(i,1)
  132. break;
  133. }
  134. }
  135. }
  136. if(!e.hasChildren){
  137. this.getIdBydelete(e.children)
  138. }
  139. })
  140. },
  141. //取消父级
  142. // deleteParent(id){
  143. // for(var i = 0; i<this.parent_data.length;i++){
  144. // if(id == this.parent_data[i].id) {
  145. // this.parent_data.splice(i,1)
  146. // break;
  147. // }
  148. // }
  149. // },
  150. // 关联下一级,选中
  151. chooseChild(arr,pid) {
  152. let that = this;
  153. for (var i = 0, len = arr.length; i < len; i++) {
  154. let item = arr[i];
  155. if(item.hasChildren) {
  156. that.newCheckList.push({...item,tree_stackId:pid})
  157. }
  158. if (!item.hasChildren) {
  159. this.chooseChild(item.children,item.id)
  160. }
  161. }
  162. },
  163. // (tree为目标树,targetId为目标节点id)
  164. getNodeRoute(tree, targetId) {
  165. for (let index = 0; index < tree.length; index++) {
  166. if (tree[index].children) {
  167. let endRecursiveLoop = this.getNodeRoute(tree[index].children, targetId)
  168. if (endRecursiveLoop) {
  169. this.nodePathArray.push(tree[index])
  170. return true
  171. }
  172. }
  173. if (tree[index].id === targetId) {
  174. return true
  175. }
  176. }
  177. },
  178. //单选
  179. checkbox(item, index) {
  180. var that = this;
  181. that.newCheckList = []
  182. that.newCheckList.push(that.tree[index])
  183. },
  184. //到下一级
  185. toChildren(item) {
  186. if(item.hasChildren) return
  187. var that = this;
  188. this.tochild = true;
  189. let children = that.props.children;
  190. if (!item.hasChildren && item[children].length > 0) {
  191. that.tree = item[children];
  192. if (that.tree_stack[0].id == item.id) {
  193. } else {
  194. that.tree_stack.push(item);
  195. }
  196. }
  197. this.$nextTick(() => {
  198. this.scrollLeft += 200;
  199. })
  200. if(this.props.checkStrictly) this.checkAllChoose();
  201. this.$forceUpdate()
  202. },
  203. //搜索
  204. confirmSearch(val) {
  205. this.searchResult = []
  206. this.search(this.allData, val)
  207. this.isre = true
  208. this.tree_stack.splice(1, 6666)
  209. uni.showLoading({
  210. title: '正在查找'
  211. })
  212. setTimeout(() => {
  213. this.tree = this.searchResult
  214. uni.hideLoading()
  215. }, 300)
  216. },
  217. search(data, keyword) {
  218. var that = this
  219. let children = that.props.children
  220. for (var i = 0, len = data.length; i < len; i++) {
  221. if (data[i].name.indexOf(keyword) >= 0) {
  222. that.searchResult.push(data[i])
  223. }
  224. if (!data[i].hasChildren && data[i][children].length > 0) {
  225. that.search(data[i][children], keyword)
  226. }
  227. }
  228. },
  229. checkAllChoose(){
  230. let o = false,t = true;
  231. this.tree.forEach((e,i)=>{
  232. console.log(e.hasChildren,"****");
  233. if(!e.hasChildren){
  234. e.qx = o;
  235. e.bx = o;
  236. let num2 = this.computAllNumber(e.children);
  237. if(this.newNum!=0&&this.oldNum!=0){
  238. if(this.newNum==this.oldNum) {
  239. e.qx = t;e.bx = o;
  240. }else{
  241. e.qx = o;e.bx = t;
  242. }
  243. }
  244. if(this.newNum!=0&&this.oldNum == 0){
  245. this.$set(this.tree[i],'bx',o); this.$set(this.tree[i],'qx',o);
  246. }
  247. this.$forceUpdate()
  248. this.newNum=0
  249. this.oldNum=0
  250. }
  251. })
  252. },
  253. computAllNumber(arr) {
  254. for(let j = 0; j<arr.length;j++){
  255. var e = arr[j];
  256. if(arr[j].hasChildren) {this.newNum ++;}
  257. this.checkSum(e.id)
  258. if(!e.hasChildren){
  259. this.computAllNumber(e.children)
  260. }
  261. }
  262. },
  263. checkSum(id){
  264. for(let i = 0; i<this.newCheckList.length;i++){
  265. if(id == this.newCheckList[i].id) {
  266. this.oldNum++;
  267. break
  268. }
  269. }
  270. },
  271. //返回其它层
  272. backTree(item, index) {
  273. let that = this,max = this.max;
  274. if (index == -1) {
  275. that.tree = that.allData
  276. that.tree_stack.splice(1, max)
  277. that.isre = false
  278. that.$refs.sea.clears()
  279. } else if (index == -2) {
  280. that.tree = that.searchResult
  281. that.tree_stack.splice(1, max)
  282. } else {
  283. if (that.tree_stack.length - index > 2) {
  284. that.tree_stack.forEach((item, i) => {
  285. if (i > index) {
  286. that.tree_stack.splice(i, max)
  287. }
  288. })
  289. } else if (index == that.tree_stack.length - 1) {
  290. } else {
  291. that.tree_stack.splice(that.tree_stack.length - 1, 1)
  292. }
  293. that.tree = item[that.props.children]
  294. }
  295. if(this.props.checkStrictly) this.checkAllChoose();
  296. this.$forceUpdate()
  297. },
  298. backConfirm(){
  299. this.$emit('sendValue',this.newCheckList,'back')
  300. }
  301. }
  302. }