code.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. if (item.hasChildren) {
  96. return
  97. }
  98. var that = this;
  99. if(!this.props.multiple) return;
  100. let findIdex = that.newCheckList.findIndex(e=>{return item.id==e.id});
  101. if (findIdex>-1) { //反选
  102. if (that.props.checkStrictly) {//关联子级
  103. if (!item.hasChildren) {//用户
  104. that.newCheckList.splice(findIdex,1)
  105. } else {//非用户,取消所有下一级
  106. that.getIdBydelete(item.children)
  107. }
  108. } else {
  109. that.newCheckList.splice(findIdex,1)
  110. }
  111. } else { //选中
  112. if (item.hasChildren&&that.props.checkStrictly) {//选中下一级
  113. if(qx||bx){//取消下级
  114. await that.getIdBydelete(item.children);
  115. item.qx = 0;item.bx = 0;
  116. }
  117. else{
  118. item.qx = 1;item.bx = 0;
  119. await that.chooseChild(item.children,item.id);
  120. }
  121. that.$forceUpdate()
  122. return
  123. }
  124. // if(item.hasChildren&&this.props.checkStrictly) this.getNodeRoute(this.allData,item.id);
  125. that.newCheckList.push({...item});
  126. }
  127. },
  128. // 取消下一级的选中
  129. getIdBydelete(arr) {
  130. arr.forEach(e=>{
  131. if(!e.hasChildren){
  132. for(var i = 0; i<this.newCheckList.length;i++){
  133. if(e.id == this.newCheckList[i].id) {
  134. this.newCheckList.splice(i,1)
  135. break;
  136. }
  137. }
  138. }
  139. if(e.hasChildren){
  140. this.getIdBydelete(e.children)
  141. }
  142. })
  143. },
  144. //取消父级
  145. // deleteParent(id){
  146. // for(var i = 0; i<this.parent_data.length;i++){
  147. // if(id == this.parent_data[i].id) {
  148. // this.parent_data.splice(i,1)
  149. // break;
  150. // }
  151. // }
  152. // },
  153. // 关联下一级,选中
  154. chooseChild(arr,pid) {
  155. let that = this;
  156. for (var i = 0, len = arr.length; i < len; i++) {
  157. let item = arr[i];
  158. if(!item.hasChildren) {
  159. that.newCheckList.push({...item,tree_stackId:pid})
  160. }
  161. if (item.hasChildren) {
  162. this.chooseChild(item.children,item.id)
  163. }
  164. }
  165. },
  166. // (tree为目标树,targetId为目标节点id)
  167. getNodeRoute(tree, targetId) {
  168. for (let index = 0; index < tree.length; index++) {
  169. if (tree[index].children) {
  170. let endRecursiveLoop = this.getNodeRoute(tree[index].children, targetId)
  171. if (endRecursiveLoop) {
  172. this.nodePathArray.push(tree[index])
  173. return true
  174. }
  175. }
  176. if (tree[index].id === targetId) {
  177. return true
  178. }
  179. }
  180. },
  181. //单选
  182. checkbox(item, index) {
  183. var that = this;
  184. that.newCheckList = []
  185. that.newCheckList.push(that.tree[index])
  186. },
  187. //到下一级
  188. toChildren(item) {
  189. if(!item.hasChildren) return
  190. var that = this;
  191. this.tochild = true;
  192. let children = that.props.children;
  193. if (item.hasChildren && item[children].length > 0) {
  194. that.tree = item[children];
  195. if (that.tree_stack[0].id == item.id) {
  196. } else {
  197. that.tree_stack.push(item);
  198. }
  199. }
  200. this.$nextTick(() => {
  201. this.scrollLeft += 200;
  202. })
  203. if(this.props.checkStrictly) this.checkAllChoose();
  204. this.$forceUpdate()
  205. },
  206. //搜索
  207. confirmSearch(val) {
  208. this.searchResult = []
  209. this.search(this.allData, val)
  210. this.isre = true
  211. this.tree_stack.splice(1, 6666)
  212. uni.showLoading({
  213. title: '正在查找'
  214. })
  215. setTimeout(() => {
  216. this.tree = this.searchResult
  217. uni.hideLoading()
  218. }, 300)
  219. },
  220. search(data, keyword) {
  221. var that = this
  222. let children = that.props.children
  223. for (var i = 0, len = data.length; i < len; i++) {
  224. if (data[i][this.props.label].indexOf(keyword) >= 0) {
  225. that.searchResult.push(data[i])
  226. }
  227. if (data[i].hasChildren && data[i][children].length > 0) {
  228. that.search(data[i][children], keyword)
  229. }
  230. }
  231. },
  232. checkAllChoose(){
  233. let o = false,t = true;
  234. this.tree.forEach((e,i)=>{
  235. if(e.hasChildren){
  236. e.qx = o;
  237. e.bx = o;
  238. let num2 = this.computAllNumber(e.children);
  239. if(this.newNum!=0&&this.oldNum!=0){
  240. if(this.newNum==this.oldNum) {
  241. e.qx = t;e.bx = o;
  242. }else{
  243. e.qx = o;e.bx = t;
  244. }
  245. }
  246. if(this.newNum!=0&&this.oldNum == 0){
  247. this.$set(this.tree[i],'bx',o); this.$set(this.tree[i],'qx',o);
  248. }
  249. this.$forceUpdate()
  250. this.newNum=0
  251. this.oldNum=0
  252. }
  253. })
  254. },
  255. computAllNumber(arr) {
  256. for(let j = 0; j<arr.length;j++){
  257. var e = arr[j];
  258. if(!arr[j].hasChildren) {this.newNum ++;}
  259. this.checkSum(e.id)
  260. if(e.hasChildren){
  261. this.computAllNumber(e.children)
  262. }
  263. }
  264. },
  265. checkSum(id){
  266. for(let i = 0; i<this.newCheckList.length;i++){
  267. if(id == this.newCheckList[i].id) {
  268. this.oldNum++;
  269. break
  270. }
  271. }
  272. },
  273. //返回其它层
  274. backTree(item, index) {
  275. let that = this,max = this.max;
  276. if (index == -1) {
  277. that.tree = that.allData
  278. that.tree_stack.splice(1, max)
  279. that.isre = false
  280. that.$refs.sea.clears()
  281. } else if (index == -2) {
  282. that.tree = that.searchResult
  283. that.tree_stack.splice(1, max)
  284. } else {
  285. if (that.tree_stack.length - index > 2) {
  286. that.tree_stack.forEach((item, i) => {
  287. if (i > index) {
  288. that.tree_stack.splice(i, max)
  289. }
  290. })
  291. } else if (index == that.tree_stack.length - 1) {
  292. } else {
  293. that.tree_stack.splice(that.tree_stack.length - 1, 1)
  294. }
  295. that.tree = item[that.props.children]
  296. }
  297. if(this.props.checkStrictly) this.checkAllChoose();
  298. this.$forceUpdate()
  299. },
  300. backConfirm(){
  301. this.$emit('sendValue',this.newCheckList,'back')
  302. }
  303. }
  304. }