You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

1 year ago
  1. // pages/common/comTree/index.js
  2. /**
  3. value = [
  4. {
  5. id: 1,
  6. name: '一级名称',
  7. children: []
  8. }
  9. ]
  10. */
  11. Component({
  12. /**
  13. * 组件的属性列表
  14. */
  15. properties: {
  16. dataTree: {
  17. type: Array,
  18. value: []
  19. },
  20. selectKey: { // 选中的节点id
  21. type: String,
  22. value: ''
  23. },
  24. isSelectLastNode: { //是否必须选中最后一节点
  25. type: Boolean,
  26. value: false
  27. },
  28. isOpenAll: { //是否展开全部节点
  29. type: Boolean,
  30. value: false
  31. }
  32. },
  33. observers: {
  34. 'dataTree': function(params) {
  35. params.forEach(v => {
  36. v.open = this.properties.isOpenAll // 是否展开
  37. })
  38. this.setData({
  39. tree: params
  40. })
  41. }
  42. },
  43. /**
  44. * 组件的初始数据
  45. */
  46. data: {
  47. tree: []
  48. },
  49. /**
  50. * 组件的方法列表
  51. */
  52. methods: {
  53. isOpen(e) {
  54. const open = 'tree[' + e.currentTarget.dataset.index + '].open'
  55. this.setData({
  56. [open]: !this.data.tree[e.currentTarget.dataset.index].open
  57. })
  58. },
  59. select(e) {
  60. const item = e.currentTarget.dataset.item
  61. if(this.properties.isSelectLastNode) {
  62. // console.log(item)
  63. if (!item.children || item.children.length == 0) {
  64. this.triggerEvent('select', { item: item }, { bubbles: true, composed: true })
  65. } else {
  66. this.triggerEvent('select', { tips: '必须选择最后一个节点' }, { bubbles: true, composed: true })
  67. }
  68. } else {
  69. this.triggerEvent('select', { item: item }, { bubbles: true, composed: true })
  70. }
  71. }
  72. }
  73. })