index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. getHeight
  3. } from "../../utils/getHeight";
  4. Component({
  5. properties: {
  6. list: {
  7. type: {
  8. type: Array
  9. },
  10. value: [{
  11. label: "筛选1",
  12. index: null,
  13. showName: "name", //显示字段
  14. valueKey: "name", //返回Key
  15. selectKey: "id", //传参 代表选着字段 不传参返回整个选择对象
  16. value: "", //选中值
  17. default: "", //默认索引
  18. list: [{
  19. name: "a1",
  20. id: 0
  21. }, {
  22. name: "a2",
  23. id: 1
  24. }]
  25. }]
  26. },
  27. show: {
  28. type: Boolean
  29. },
  30. handle: {
  31. type: Function
  32. }, //按钮回调函数
  33. dateRange: {
  34. type: Boolean
  35. }, //是否开启日期范围筛选
  36. dateRangeName: {
  37. type: String,
  38. value: "日期范围"
  39. },
  40. dateRange1: {
  41. type: Boolean
  42. }, //是否开启日期范围筛选
  43. zIndex: {
  44. type: String,
  45. value: 99999,
  46. },
  47. interrupt: {
  48. type: Function
  49. },
  50. isReset: {
  51. type: Boolean,
  52. value: true
  53. }
  54. },
  55. data: {
  56. startdate: "", //开始时间
  57. enddate: "", //结束时间
  58. periodstart: "",
  59. periodend: "",
  60. },
  61. lifetimes: {
  62. ready() {
  63. getHeight('.head', this).then(res => this.setData({
  64. listHeight: res - 80
  65. }));
  66. }
  67. },
  68. methods: {
  69. /* 选择 */
  70. onSelect(e) {
  71. const {
  72. item, //被选项
  73. index, //列表下标
  74. i //被选项下标
  75. } = e.currentTarget.dataset;
  76. if (this.data.list[index].index == i) {
  77. this.setData({
  78. [`list[${index}].value`]: "",
  79. [`list[${index}].index`]: null
  80. });
  81. } else {
  82. this.setData({
  83. [`list[${index}].value`]: this.data.list[index].selectKey ? item[this.data.list[index].selectKey] : item,
  84. [`list[${index}].index`]: i
  85. });
  86. };
  87. if (this.data.list[index].interrupt) this.triggerEvent("interrupt", {
  88. item,
  89. index,
  90. name: this.data.list[index].selectKey,
  91. list: this.data.list
  92. })
  93. },
  94. onSelectCheckbox(e) {
  95. const {
  96. item, //被选项
  97. index, //列表下标
  98. i //被选项下标
  99. } = e.currentTarget.dataset,
  100. list = this.data.list;
  101. if (list[index].index == "" || list[index].index == null) {
  102. list[index].index = [];
  103. list[index].value = [];
  104. }
  105. let arrIndex = list[index].index.indexOf(i)
  106. if (arrIndex == -1) {
  107. list[index].index.push(i)
  108. list[index].value.push(list[index].selectKey ? item[list[index].selectKey] : item)
  109. } else {
  110. list[index].index.splice(arrIndex, 1)
  111. list[index].value.splice(arrIndex, 1)
  112. };
  113. this.setData({
  114. list
  115. })
  116. if (this.data.list[index].interrupt) this.triggerEvent("interrupt", {
  117. item,
  118. index,
  119. name: this.data.list[index].selectKey,
  120. list
  121. })
  122. },
  123. /* 点击按钮 */
  124. onClick(e) {
  125. const {
  126. name
  127. } = e.target.dataset;
  128. if (name == 'reset') {
  129. this.setData({
  130. list: this.data.list.map(v => {
  131. let value = "",
  132. index = null;
  133. try {
  134. if ((v.default+'').length) {
  135. index = v.default;
  136. value = v.selectKey ? v.list[v.default][v.selectKey] : v.list[v.default]
  137. }
  138. } catch (error) {
  139. }
  140. v.value = value;
  141. v.index = index;
  142. return v;
  143. })
  144. })
  145. this.setData({
  146. startdate: '',
  147. enddate: ''
  148. })
  149. let MultilevelClass = this.selectComponent("#MultilevelClass");
  150. if (MultilevelClass) MultilevelClass.clearChild()
  151. };
  152. let obj = {};
  153. this.data.list.forEach(v => {
  154. if (v.type == 'multilevelClass') {
  155. obj[v.valueKey] = getApp().globalData.temporaryId;
  156. delete(getApp().globalData.temporaryId);
  157. } else if (v.type == 'checkbox') {
  158. obj[v.valueKey] = v.value || [];
  159. } else {
  160. obj[v.valueKey] = v.value;
  161. }
  162. });
  163. if (this.data.dateRange) {
  164. obj.startdate = this.data.startdate;
  165. obj.enddate = this.data.enddate;
  166. };
  167. if (this.data.dateRange1) {
  168. obj.periodstart = this.data.periodstart;
  169. obj.periodend = this.data.periodend;
  170. };
  171. this.triggerEvent("handle", obj);
  172. this.onClose();
  173. },
  174. /* 筛选日期范围 */
  175. changeDate(e) {
  176. const name = e.currentTarget.dataset.name,
  177. value = e.detail.value;
  178. this.setData({
  179. [name]: value
  180. })
  181. },
  182. onClose() {
  183. this.setData({
  184. show: false
  185. })
  186. }
  187. }
  188. })