Picker 拾取器请使用手机扫码体验

提供多个选项集合供用户选择其中一项。

基本用法

  1. html
    <nut-picker
  2. :is-visible="isVisible1"
  3. :default-value-data="defaultValueData1"
  4. :list-data="listData1"
  5. @close="switchPicker('isVisible1')"
  6. @confirm="setYearValue"
  7. >
  8. </nut-picker>

多列用法

  1. html
    <nut-picker
  2. :is-visible="isVisible0"
  3. :list-data="listData0"
  4. title="请选择年月"
  5. :default-value-data="defaultValueData0"
  6. @close="switchPicker('isVisible0')"
  7. @confirm="setChooseValue0"
  8. >
  9. </nut-picker>

联动(省市区)

  1. html
    <nut-picker
  2. :is-visible="isVisible"
  3. title="请选择城市"
  4. :list-data="listData"
  5. :default-value-data="defaultValueData"
  6. @close="switchPicker('isVisible')"
  7. @confirm="setChooseValue"
  8. @choose="updateChooseValue"
  9. @close-update="closeUpdateChooseValue"
  10. >
  11. </nut-picker>

联动(省市区)自定义数据

  1. html
    <nut-picker
  2. :is-visible="isVisible2"
  3. title="请选择城市"
  4. :default-value-data="defaultValueData"
  5. :list-data="custmerCityData"
  6. @close="switchPicker('isVisible2')"
  7. @confirm="setChooseValueCustmer"
  8. @choose="updateChooseValueCustmer"
  9. @close-update="closeUpdateChooseValueCustmer"
  10. ></nut-picker>
  1. javascript
    const APIData = [
  2. {
  3. label: 1,
  4. array: [
  5. {
  6. label: 3,
  7. value: "朝阳区"
  8. },
  9. {
  10. label: 4,
  11. value: "海淀区"
  12. }
  13. ]
  14. },
  15. {
  16. label: 2,
  17. array: [
  18. {
  19. label: 5,
  20. value: "测试1"
  21. },
  22. {
  23. label: 6,
  24. value: "测试2"
  25. }
  26. ]
  27. }
  28. ];
  29. export default {
  30. data() {
  31. return {
  32. date: null,
  33. isVisible0: false,
  34. listData0: [
  35. [
  36. "2010",
  37. "2011",
  38. "2012",
  39. "2013",
  40. "2014",
  41. "2015",
  42. "2016",
  43. "2017",
  44. "2018",
  45. "2019",
  46. "2020",
  47. "2021",
  48. "2022",
  49. "2023",
  50. "2024",
  51. "2025",
  52. "2026",
  53. "2027",
  54. "2028",
  55. "2029",
  56. "2030",
  57. "2031",
  58. "2032",
  59. "2033",
  60. "2034",
  61. "2035",
  62. "2036",
  63. "2037",
  64. "2038",
  65. "2039"
  66. ],
  67. ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
  68. ],
  69. defaultValueData0: ["2012", "2"],
  70. city: null,
  71. isVisible: false,
  72. data: {
  73. 北京: ["北京"],
  74. 黑龙江: [
  75. "哈尔滨",
  76. "绥化",
  77. "漠河",
  78. "大兴安岭",
  79. "牡丹江",
  80. "佳木斯",
  81. "齐齐哈尔",
  82. "大庆",
  83. "五大连池"
  84. ],
  85. 江西: ["九江", "南昌", "赣州"],
  86. 上海: ["上海"],
  87. 重庆: ["重庆"],
  88. 内蒙古: [
  89. "呼和浩特",
  90. "呼和浩特1",
  91. "呼和浩特2",
  92. "呼和浩特3",
  93. "呼和浩特4",
  94. "呼和浩特5",
  95. "呼和浩特6",
  96. "呼和浩特7"
  97. ]
  98. },
  99. dataSub: {
  100. 上海: ["测试1", "测试2"],
  101. 北京: ["西城区", "东城区", "大兴区", "朝阳区", "海淀区"],
  102. 南昌: ["青山湖区", "西湖区", "宏都中路", "梦时代", "八一广场"],
  103. 绥化: ["明水", "拜泉"],
  104. 哈尔滨: ["道里区", "道外区"]
  105. },
  106. listData: [["上海", "黑龙江", "北京", "重庆", "江西", "内蒙古"]],
  107. defaultValueData: null,
  108. year: null,
  109. isVisible1: false,
  110. listData1: [["2018", "2019","2010"]],
  111. custmerCityData: [
  112. [
  113. {
  114. label: 1,
  115. value: "北京"
  116. },
  117. {
  118. label: 2,
  119. value: "上海"
  120. }
  121. ]
  122. ],
  123. cityCustmer: null,
  124. isVisible2: false,
  125. defaultValueData1: null
  126. };
  127. },
  128. created() {
  129. this.listData = [
  130. ...[this.listData[0]],
  131. this.data[this.listData[0][0]],
  132. this.dataSub[this.data[this.listData[0][0]]]
  133. ];
  134. },
  135. methods: {
  136. switchPicker(param) {
  137. this[`${param}`] = !this[`${param}`];
  138. },
  139. setChooseValue0(chooseData) {
  140. this.date = `${chooseData[0]}年${chooseData[1]}月`;
  141. },
  142. setYearValue(chooseData) {
  143. this.year = `${chooseData[0]}年`;
  144. },
  145. modifyCity() {
  146. this.updateLinkage("", "重庆", 1, "重庆");
  147. this.defaultValueData = ["重庆", "重庆"];
  148. },
  149. modifyYear() {
  150. this.defaultValueData1 = ["2018"];
  151. },
  152. // demo 城市选择(联动) start
  153. setChooseValue(chooseData) {
  154. this.city = `${chooseData[0]}-${chooseData[1]}${
  155. chooseData[2] ? "-" + chooseData[2] : ""
  156. }`;
  157. },
  158. updateLinkage(self, value, index, chooseValue, cacheValueData) {
  159. if (!value) {
  160. return false;
  161. }
  162. switch (index) {
  163. case 1:
  164. let i = this.listData[0].indexOf(value);
  165. this.listData.splice(index, 1, [...this.data[this.listData[0][i]]]);
  166. chooseValue = chooseValue ? chooseValue : this.listData[index][0];
  167. self && self.updateChooseValue(self, index, chooseValue);
  168. this.updateLinkage(
  169. self,
  170. chooseValue,
  171. 2,
  172. cacheValueData && cacheValueData[2] ? cacheValueData[2] : null
  173. );
  174. break;
  175. case 2:
  176. let areaData = this.dataSub[value] ? this.dataSub[value] : [];
  177. this.listData.splice(index, 1, [...areaData]);
  178. chooseValue = chooseValue ? chooseValue : this.listData[index][0];
  179. self && self.updateChooseValue(self, index, chooseValue);
  180. break;
  181. }
  182. },
  183. updateChooseValue(self, index, value, cacheValueData) {
  184. index < 2 && this.updateLinkage(self, value, index + 1, null);
  185. },
  186. closeUpdateChooseValue(self, chooseData) {
  187. this.updateLinkage(self, chooseData[0], 1, chooseData[1], chooseData);
  188. },
  189. // demo 城市选择(联动) end
  190. setChooseValueCustmer(chooseData) {
  191. //alert(JSON.stringify(chooseData));
  192. var str = chooseData.map(item => item.value).join("-");
  193. this.cityCustmer = str;
  194. },
  195. closeUpdateChooseValueCustmer(self, chooseData) {
  196. //此处模拟查询API,如果数据缓存了不需要再重新请求
  197. setTimeout(() => {
  198. let { label, value } = chooseData[0];
  199. var resItems = APIData.find(item => item.label == label);
  200. if (resItems && resItems.array.length) {
  201. this.$set(this.custmerCityData, 1, resItems.array);
  202. // 复原位置
  203. self.updateChooseValue(self, 0, chooseData[0]);
  204. self.updateChooseValue(self, 1, chooseData[1]);
  205. }
  206. }, 100);
  207. },
  208. updateChooseValueCustmer(self, index, resValue, cacheValueData) {
  209. // 本demo为二级联动,所以限制只有首列变动的时候触发事件
  210. if (index === 0) {
  211. //此处模拟查询API,如果数据缓存了不需要再重新请求
  212. let { label, value } = resValue;
  213. setTimeout(() => {
  214. var resItems = APIData.find(item => item.label == label);
  215. if (resItems && resItems.array.length) {
  216. this.$set(this.custmerCityData, 1, resItems.array);
  217. // 更新第二列位置
  218. self.updateChooseValue(self, index + 1, this.custmerCityData[1][0]);
  219. }
  220. }, 100);
  221. }
  222. }
  223. }
  224. };

Prop

字段说明类型默认值
isVisible是否可见Booleanfalse
customClassName自定义classStringnull
title设置标题Stringnull
listData列表数据Array[]
defaultValueData默认选中Array[]

Event

字段说明回调参数
confirm点击确认按钮时候回调返回选中值
choose每一列值变更时调用依次返回this、改变的列数,改变值,当前选中值
chclose-update联动时,关闭时回调依次返回this、当前选中值
close关闭时触发-

Picker 拾取器 - 图1