group.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Component({
  2. properties: {
  3. apps: {
  4. type: Array,
  5. value: []
  6. },
  7. callback: {
  8. type: Function
  9. },
  10. },
  11. observers: {
  12. "apps": function (apps) {
  13. if (this.data.count == 1) return;
  14. this.refactorDom();
  15. }
  16. },
  17. data: {
  18. backupArrays: [],
  19. nubArr: [],
  20. count: 0,
  21. show: true,
  22. },
  23. methods: {
  24. /* 功能单选 */
  25. onChange(e) {
  26. const {
  27. index
  28. } = e.currentTarget.dataset;
  29. let obj = {
  30. systemappid: e.currentTarget.id - 0,
  31. optionids: e.detail
  32. };
  33. let arr = this.data.backupArrays,
  34. nubArr = this.data.nubArr,
  35. i = arr.findIndex(v => v.systemappid == obj.systemappid);
  36. if (e.detail.length > 0) {
  37. if (i == -1) {
  38. arr.push(obj);
  39. nubArr.push(obj.systemappid);
  40. } else {
  41. arr[i] = obj;
  42. }
  43. } else {
  44. nubArr.splice(i, 1)
  45. arr.splice(i, 1)
  46. };
  47. this.setData({
  48. ["apps[" + index + "].optionids"]: obj.optionids,
  49. backupArrays: arr,
  50. nubArr
  51. });
  52. this.triggerEvent("callback", {
  53. arr: this.data.backupArrays,
  54. apps: this.data.apps
  55. });
  56. },
  57. /* 类选择 */
  58. classClick(e) {
  59. const {
  60. item
  61. } = e.currentTarget.dataset;
  62. let i = this.data.nubArr.findIndex(v => v == item.systemappid);
  63. if (i == -1) {
  64. const obj = {
  65. systemappid: item.systemappid,
  66. optionids: item.options.map(v => v.optionid.toString())
  67. };
  68. this.data.backupArrays.push(obj);
  69. this.data.nubArr.push(item.systemappid);
  70. let index = this.data.apps.findIndex(v => v.systemappid == item.systemappid);
  71. this.setData({
  72. backupArrays: this.data.backupArrays,
  73. nubArr: this.data.nubArr,
  74. ["apps[" + index + "].optionids"]: obj.optionids
  75. })
  76. } else {
  77. let index = this.data.apps.findIndex(v => v.systemappid == item.systemappid);
  78. this.setData({
  79. ["apps[" + index + "].optionids"]: [],
  80. nubArr: this.data.nubArr.filter(v => v != item.systemappid),
  81. backupArrays: this.data.backupArrays.filter(v => v.systemappid != item.systemappid)
  82. });
  83. };
  84. this.triggerEvent("callback", {
  85. arr: this.data.backupArrays,
  86. apps: this.data.apps
  87. });
  88. },
  89. /* 重构 */
  90. refactorDom() {
  91. let apps = this.data.apps;
  92. let arr = [],
  93. nubArr = [];
  94. for (let i = 0; i < apps.length; i++) {
  95. if (apps[i].optionids.length == 0) continue;
  96. let obj = {
  97. systemappid: apps[i].systemappid,
  98. optionids: apps[i].optionids.map(v => v + '')
  99. };
  100. arr.push(obj);
  101. nubArr.push(apps[i].systemappid);
  102. this.setData({
  103. [`apps[${i}].optionids`]: apps[i].optionids.map(v => v + '')
  104. })
  105. };
  106. this.setData({
  107. backupArrays: arr,
  108. nubArr,
  109. count: 1
  110. })
  111. if (arr.length == 0) return;
  112. this.triggerEvent("callback", {
  113. arr: this.data.backupArrays,
  114. apps: this.data.apps
  115. });
  116. this.setData({
  117. show: false
  118. })
  119. this.setData({
  120. show: true
  121. })
  122. },
  123. }
  124. })