group.js 4.1 KB

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