u-picker.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "u-picker",
  5. mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.props$23],
  6. data() {
  7. return {
  8. // 上一次选择的列索引
  9. lastIndex: [],
  10. // 索引值 ,对应picker-view的value
  11. innerIndex: [],
  12. // 各列的值
  13. innerColumns: [],
  14. // 上一次的变化列索引
  15. columnIndex: 0
  16. };
  17. },
  18. watch: {
  19. // 监听默认索引的变化,重新设置对应的值
  20. defaultIndex: {
  21. immediate: true,
  22. handler(n) {
  23. this.setIndexs(n, true);
  24. }
  25. },
  26. // 监听columns参数的变化
  27. columns: {
  28. immediate: true,
  29. deep: true,
  30. handler(n) {
  31. this.setColumns(n);
  32. }
  33. }
  34. },
  35. emits: ["close", "cancel", "confirm", "change"],
  36. methods: {
  37. addUnit: common_vendor.addUnit,
  38. testArray: common_vendor.test.array,
  39. // 获取item需要显示的文字,判别为对象还是文本
  40. getItemText(item) {
  41. if (common_vendor.test.object(item)) {
  42. return item[this.keyName];
  43. } else {
  44. return item;
  45. }
  46. },
  47. // 关闭选择器
  48. closeHandler() {
  49. if (this.closeOnClickOverlay) {
  50. this.$emit("close");
  51. }
  52. },
  53. // 点击工具栏的取消按钮
  54. cancel() {
  55. this.$emit("cancel");
  56. },
  57. // 点击工具栏的确定按钮
  58. confirm() {
  59. this.$emit("confirm", {
  60. indexs: this.innerIndex,
  61. value: this.innerColumns.map((item, index) => item[this.innerIndex[index]]),
  62. values: this.innerColumns
  63. });
  64. },
  65. // 选择器某一列的数据发生变化时触发
  66. changeHandler(e) {
  67. const {
  68. value
  69. } = e.detail;
  70. let index = 0, columnIndex = 0;
  71. for (let i = 0; i < value.length; i++) {
  72. let item = value[i];
  73. if (item !== (this.lastIndex[i] || 0)) {
  74. columnIndex = i;
  75. index = item;
  76. break;
  77. }
  78. }
  79. this.columnIndex = columnIndex;
  80. const values = this.innerColumns;
  81. this.setLastIndex(value);
  82. this.setIndexs(value);
  83. this.$emit("change", {
  84. value: this.innerColumns.map((item, index2) => item[value[index2]]),
  85. index,
  86. indexs: value,
  87. // values为当前变化列的数组内容
  88. values,
  89. columnIndex
  90. });
  91. },
  92. // 设置index索引,此方法可被外部调用设置
  93. setIndexs(index, setLastIndex) {
  94. this.innerIndex = common_vendor.deepClone(index);
  95. if (setLastIndex) {
  96. this.setLastIndex(index);
  97. }
  98. },
  99. // 记录上一次的各列索引位置
  100. setLastIndex(index) {
  101. this.lastIndex = common_vendor.deepClone(index);
  102. },
  103. // 设置对应列选项的所有值
  104. setColumnValues(columnIndex, values) {
  105. this.innerColumns.splice(columnIndex, 1, values);
  106. this.setLastIndex(this.innerIndex.slice(0, columnIndex));
  107. let tmpIndex = common_vendor.deepClone(this.innerIndex);
  108. for (let i = 0; i < this.innerColumns.length; i++) {
  109. if (i > this.columnIndex) {
  110. tmpIndex[i] = 0;
  111. }
  112. }
  113. this.setIndexs(tmpIndex);
  114. },
  115. // 获取对应列的所有选项
  116. getColumnValues(columnIndex) {
  117. (async () => {
  118. await common_vendor.sleep();
  119. })();
  120. return this.innerColumns[columnIndex];
  121. },
  122. // 设置整体各列的columns的值
  123. setColumns(columns) {
  124. this.innerColumns = common_vendor.deepClone(columns);
  125. if (this.innerIndex.length === 0) {
  126. this.innerIndex = new Array(columns.length).fill(0);
  127. }
  128. },
  129. // 获取各列选中值对应的索引
  130. getIndexs() {
  131. return this.innerIndex;
  132. },
  133. // 获取各列选中的值
  134. getValues() {
  135. (async () => {
  136. await common_vendor.sleep();
  137. })();
  138. return this.innerColumns.map((item, index) => item[this.innerIndex[index]]);
  139. }
  140. }
  141. };
  142. if (!Array) {
  143. const _easycom_u_toolbar2 = common_vendor.resolveComponent("u-toolbar");
  144. const _easycom_u_loading_icon2 = common_vendor.resolveComponent("u-loading-icon");
  145. const _easycom_u_popup2 = common_vendor.resolveComponent("u-popup");
  146. (_easycom_u_toolbar2 + _easycom_u_loading_icon2 + _easycom_u_popup2)();
  147. }
  148. const _easycom_u_toolbar = () => "../u-toolbar/u-toolbar.js";
  149. const _easycom_u_loading_icon = () => "../u-loading-icon/u-loading-icon.js";
  150. const _easycom_u_popup = () => "../u-popup/u-popup.js";
  151. if (!Math) {
  152. (_easycom_u_toolbar + _easycom_u_loading_icon + _easycom_u_popup)();
  153. }
  154. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  155. return common_vendor.e({
  156. a: _ctx.showToolbar
  157. }, _ctx.showToolbar ? {
  158. b: common_vendor.o($options.cancel),
  159. c: common_vendor.o($options.confirm),
  160. d: common_vendor.p({
  161. cancelColor: _ctx.cancelColor,
  162. confirmColor: _ctx.confirmColor,
  163. cancelText: _ctx.cancelText,
  164. confirmText: _ctx.confirmText,
  165. title: _ctx.title
  166. })
  167. } : {}, {
  168. e: common_vendor.f($data.innerColumns, (item, index, i0) => {
  169. return common_vendor.e({
  170. a: $options.testArray(item)
  171. }, $options.testArray(item) ? {
  172. b: common_vendor.f(item, (item1, index1, i1) => {
  173. return {
  174. a: common_vendor.t($options.getItemText(item1)),
  175. b: index1,
  176. c: index1 === $data.innerIndex[index] ? "bold" : "normal"
  177. };
  178. }),
  179. c: $options.addUnit(_ctx.itemHeight),
  180. d: $options.addUnit(_ctx.itemHeight)
  181. } : {}, {
  182. e: index
  183. });
  184. }),
  185. f: `height: ${$options.addUnit(_ctx.itemHeight)}`,
  186. g: $data.innerIndex,
  187. h: _ctx.immediateChange,
  188. i: `${$options.addUnit(_ctx.visibleItemCount * _ctx.itemHeight)}`,
  189. j: common_vendor.o((...args) => $options.changeHandler && $options.changeHandler(...args)),
  190. k: _ctx.loading
  191. }, _ctx.loading ? {
  192. l: common_vendor.p({
  193. mode: "circle"
  194. })
  195. } : {}, {
  196. m: common_vendor.o($options.closeHandler),
  197. n: common_vendor.p({
  198. show: _ctx.show,
  199. mode: _ctx.popupMode
  200. })
  201. });
  202. }
  203. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-1500ce68"], ["__file", "D:/project/学吧/learn_applet/node_modules/uview-plus/components/u-picker/u-picker.vue"]]);
  204. wx.createComponent(Component);