code.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 '-1'
  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. isEnd: false,
  57. categoryCode: '',
  58. pageIndex: 1,
  59. isre: false,
  60. tree: this.trees,
  61. newNum: 0,
  62. oldNum: 0,
  63. allData: this.trees,
  64. tree_stack: [1],
  65. parent_data: this.parentList||[],//选择父辈
  66. searchResult: [],
  67. tochild: false,
  68. newCheckList: this.checkList,
  69. scrollLeft: 999,
  70. nodePathArray: []
  71. }
  72. },
  73. /*
  74. * 已兼容h5和小程序端,其它端没测试过,估计问题不大,只需要改一下传值的方式
  75. *
  76. * 如有问题可以加qq:122720267
  77. *
  78. * 使用该插件的朋友请给个好评,或者到git start一下
  79. * git地址:https://github.com/LSZ579/xiaolu-tree-plugin.git
  80. * 插件市场地址: https://ext.dcloud.net.cn/plugin?id=2423
  81. *
  82. */
  83. components: {
  84. search
  85. },
  86. mounted() {
  87. if(this.props.multiple&&this.props.checkStrictly){
  88. if(this.newCheckList.length!=0){
  89. this.checkAllChoose();
  90. return
  91. }
  92. for(let i = 0; i<this.tree.length;i++){
  93. this.$set(this.tree[i],'bx',0)
  94. this.$set(this.tree[i],'qx',0)
  95. }
  96. }
  97. if(!this.props.multiple&&this.newCheckList.length>0) {
  98. this.getNodeRoute(this.allData,this.newCheckList[0].id)
  99. let arr = this.nodePathArray.reverse()
  100. if(arr.length == 0)return
  101. this.tree_stack = this.tree_stack.concat(arr);
  102. this.tree = this.tree_stack[this.tree_stack.length-1].children;
  103. }
  104. },
  105. methods: {
  106. getList() {
  107. this.pageIndex += 1
  108. this.$http.Material({
  109. categoryCode: this.categoryCode,
  110. pageIndex: this.pageIndex,
  111. pageSize: 20
  112. }).then(res => {
  113. console.log(res)
  114. if(res.code === 0) {
  115. if(res.result.isLastPage) {
  116. this.loadStatus = "nomore"
  117. }else {
  118. this.loadStatus = "loadmore"
  119. }
  120. let array = res.result.list
  121. array.forEach(item => {
  122. item.children = []
  123. item.user = true
  124. })
  125. this.tree.push(...array)
  126. }
  127. })
  128. },
  129. //多选
  130. async checkboxChange(item, index, bx, qx) {
  131. var that = this;
  132. if(!this.props.multiple) return;
  133. let findIdex = that.newCheckList.findIndex(e=>{return item.id==e.id});
  134. if (findIdex>-1) { //反选
  135. if (that.props.checkStrictly) {//关联子级
  136. if (item.user) {//用户
  137. that.newCheckList.splice(findIdex,1)
  138. } else {//非用户,取消所有下一级
  139. that.getIdBydelete(item.children)
  140. }
  141. } else {
  142. that.newCheckList.splice(findIdex,1)
  143. }
  144. } else { //选中
  145. if (!item.user&&that.props.checkStrictly) {//选中下一级
  146. if(qx||bx){//取消下级
  147. await that.getIdBydelete(item.children);
  148. item.qx = 0;item.bx = 0;
  149. }
  150. else{
  151. item.qx = 1;item.bx = 0;
  152. await that.chooseChild(item.children,item.id);
  153. }
  154. that.$emit('sendValue', that.newCheckList);
  155. that.$forceUpdate()
  156. return
  157. }
  158. // if(item.user&&this.props.checkStrictly) this.getNodeRoute(this.allData,item.id);
  159. that.newCheckList.push({...item});
  160. }
  161. that.$emit('sendValue', that.newCheckList)
  162. },
  163. // 取消下一级的选中
  164. getIdBydelete(arr) {
  165. arr.forEach(e=>{
  166. if(e.user){
  167. for(var i = 0; i<this.newCheckList.length;i++){
  168. if(e.id == this.newCheckList[i].id) {
  169. this.newCheckList.splice(i,1)
  170. break;
  171. }
  172. }
  173. }
  174. if(!e.user){
  175. this.getIdBydelete(e.children)
  176. }
  177. })
  178. },
  179. //取消父级
  180. // deleteParent(id){
  181. // for(var i = 0; i<this.parent_data.length;i++){
  182. // if(id == this.parent_data[i].id) {
  183. // this.parent_data.splice(i,1)
  184. // break;
  185. // }
  186. // }
  187. // },
  188. // 关联下一级,选中
  189. chooseChild(arr,pid) {
  190. let that = this;
  191. for (var i = 0, len = arr.length; i < len; i++) {
  192. let item = arr[i];
  193. if(item.user) {
  194. that.newCheckList.push({...item,tree_stackId:pid})
  195. }
  196. if (!item.user) {
  197. this.chooseChild(item.children,item.id)
  198. }
  199. }
  200. },
  201. // (tree为目标树,targetId为目标节点id)
  202. getNodeRoute(tree, targetId) {
  203. for (let index = 0; index < tree.length; index++) {
  204. if (tree[index].children) {
  205. let endRecursiveLoop = this.getNodeRoute(tree[index].children, targetId)
  206. if (endRecursiveLoop) {
  207. this.nodePathArray.push(tree[index])
  208. return true
  209. }
  210. }
  211. if (tree[index].id === targetId) {
  212. return true
  213. }
  214. }
  215. },
  216. //单选
  217. checkbox(item, index) {
  218. var that = this;
  219. that.newCheckList = []
  220. that.newCheckList.push(that.tree[index])
  221. that.$emit('sendValue', that.newCheckList)
  222. },
  223. //到下一级
  224. toChildren(item) {
  225. if(item.isEnd){
  226. this.isEnd = true
  227. this.categoryCode = item.value
  228. this.$http.Material({
  229. categoryCode: item.value,
  230. pageIndex: this.pageIndex || 1,
  231. pageSize: 20
  232. }).then(res => {
  233. if(res.code === 0) {
  234. if(res.result.isLastPage) {
  235. this.loadStatus = "nomore"
  236. }else {
  237. this.loadStatus = "loadmore"
  238. }
  239. this.tree = res.result.list
  240. this.tree.forEach(item => {
  241. item.children = []
  242. item.user = true
  243. })
  244. }
  245. })
  246. } else {
  247. if(item.user) return
  248. var that = this;
  249. this.tochild = true;
  250. let children = that.props.children;
  251. if (!item.user && item[children].length > 0) {
  252. that.tree = item[children];
  253. if (that.tree_stack[0].id == item.id) {
  254. that.tree_stack.push(item);
  255. } else {
  256. console.log('??????')
  257. that.tree_stack.push(item);
  258. console.log(that.tree_stack)
  259. }
  260. }
  261. this.$nextTick(() => {
  262. this.scrollLeft += 200;
  263. })
  264. if(this.props.checkStrictly) this.checkAllChoose();
  265. this.$forceUpdate()
  266. }
  267. },
  268. //搜索
  269. confirmSearch(val) {
  270. // this.searchResult = []
  271. // this.search(this.allData, val)
  272. this.isre = true
  273. this.tree_stack.splice(1, 6666)
  274. // uni.showLoading({
  275. // title: '正在查找'
  276. // })
  277. // setTimeout(() => {
  278. // this.tree = this.searchResult
  279. // uni.hideLoading()
  280. // }, 300)
  281. this.pageIndex = 1
  282. this.$http.Material({
  283. key: val,
  284. pageIndex: this.pageIndex,
  285. pageSize: 20
  286. }).then(res => {
  287. if(res.code === 0) {
  288. this.tree = res.result.list
  289. this.tree.forEach(item => {
  290. item.children = []
  291. item.user = true
  292. })
  293. }
  294. })
  295. },
  296. search(data, keyword) {
  297. var that = this
  298. let children = that.props.children
  299. for (var i = 0, len = data.length; i < len; i++) {
  300. if (data[i].name.indexOf(keyword) >= 0) {
  301. that.searchResult.push(data[i])
  302. }
  303. if (!data[i].user && data[i][children].length > 0) {
  304. that.search(data[i][children], keyword)
  305. }
  306. }
  307. },
  308. checkAllChoose(){
  309. let o = false,t = true;
  310. this.tree.forEach((e,i)=>{
  311. if(!e.user){
  312. e.qx = o;
  313. e.bx = o;
  314. let num2 = this.computAllNumber(e.children);
  315. // console.log(this.newNum,this.oldNum)
  316. if(this.newNum!=0&&this.oldNum!=0){
  317. if(this.newNum==this.oldNum) {
  318. e.qx = t;e.bx = o;
  319. }else{
  320. e.qx = o;e.bx = t;
  321. }
  322. }
  323. if(this.newNum!=0&&this.oldNum == 0){
  324. this.$set(this.tree[i],'bx',o); this.$set(this.tree[i],'qx',o);
  325. }
  326. this.$forceUpdate()
  327. this.newNum=0
  328. this.oldNum=0
  329. }
  330. })
  331. },
  332. computAllNumber(arr) {
  333. for(let j = 0; j<arr.length;j++){
  334. var e = arr[j];
  335. if(arr[j].user) {this.newNum ++;}
  336. this.checkSum(e.id)
  337. if(!e.user){
  338. this.computAllNumber(e.children)
  339. }
  340. }
  341. },
  342. checkSum(id){
  343. for(let i = 0; i<this.newCheckList.length;i++){
  344. if(id == this.newCheckList[i].id) {
  345. this.oldNum++;
  346. break
  347. }
  348. }
  349. },
  350. //返回其它层
  351. backTree(item, index) {
  352. this.newCheckList = [] // 清除选中的数据
  353. this.pageIndex = 1
  354. // isEnd 用于判断是否最后一级 如果为最后一级则列表标为可选
  355. this.isEnd = false
  356. let that = this,max = 66666;
  357. if (index == -1) {
  358. that.tree = that.allData
  359. that.tree_stack.splice(1, max)
  360. that.isre = false
  361. that.$refs.sea.clears()
  362. } else if (index == -2) {
  363. that.tree = that.searchResult
  364. that.tree_stack.splice(1, max)
  365. } else {
  366. if (that.tree_stack.length - index > 2) {
  367. that.tree_stack.forEach((item, i) => {
  368. if (i > index) {
  369. that.tree_stack.splice(i, max)
  370. }
  371. })
  372. } else if (index == that.tree_stack.length - 1) {
  373. } else {
  374. that.tree_stack.splice(that.tree_stack.length - 1, 1)
  375. }
  376. that.tree = item[that.props.children]
  377. }
  378. if(this.props.checkStrictly) this.checkAllChoose();
  379. this.$forceUpdate()
  380. },
  381. backConfirm(){
  382. // console.log(this.newCheckList)
  383. if(this.newCheckList[0]) {
  384. this.$emit('sendValue',this.newCheckList,'back')
  385. }
  386. }
  387. }
  388. }