demo.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <div class="demo-list">
  3. <nut-noticebar :closeMode="true" v-if="!isMobile"
  4. >此 Demo 在 PC 端浏览器与移动端浏览器体验差异较大,建议在 Android 或 iOS 设备上体验。</nut-noticebar
  5. >
  6. <h4>基础样式</h4>
  7. <div>
  8. <nut-cell
  9. :show-icon="true"
  10. title="请选择城市"
  11. :desc="option"
  12. @click.native="openSwitch('isVisible')">
  13. </nut-cell>
  14. </div>
  15. <div>
  16. <nut-picker
  17. :is-visible="isVisible"
  18. :list-data="listData"
  19. :default-value-data="defaultValueData"
  20. @close="closeSwitch('isVisible')"
  21. @confirm="confirm"
  22. ></nut-picker>
  23. </div>
  24. <h4>多列样式</h4>
  25. <div>
  26. <nut-cell
  27. :show-icon="true"
  28. title="请选择时间"
  29. :desc="option1"
  30. @click.native="openSwitch('isVisible1')">
  31. </nut-cell>
  32. </div>
  33. <div>
  34. <nut-picker
  35. :is-visible="isVisible1"
  36. :list-data="listData1"
  37. :default-value-data="defaultValueData1"
  38. @close="closeSwitch('isVisible1')"
  39. @confirm="confirm1"
  40. ></nut-picker>
  41. </div>
  42. <h4>多级联动</h4>
  43. <div>
  44. <nut-cell
  45. :show-icon="true"
  46. title="请选择城市"
  47. :desc="cityCustmer"
  48. @click.native="openSwitch('isVisible2')">
  49. </nut-cell>
  50. </div>
  51. <div>
  52. <nut-picker
  53. :is-visible="isVisible2"
  54. :default-value-data="defaultValueData"
  55. :list-data="custmerCityData"
  56. @close="closeSwitch('isVisible2')"
  57. @confirm="setChooseValueCustmer"
  58. @choose="updateChooseValueCustmer"
  59. @close-update="closeUpdateChooseValueCustmer"
  60. ></nut-picker>
  61. </div>
  62. <h4>带标题样式</h4>
  63. <div>
  64. <nut-cell
  65. :show-icon="true"
  66. title="请选择城市"
  67. :desc="option3"
  68. @click.native="openSwitch('isVisible3')">
  69. </nut-cell>
  70. </div>
  71. <div>
  72. <nut-picker
  73. :is-visible="isVisible3"
  74. :list-data="listData"
  75. title="请选择城市"
  76. @close="closeSwitch('isVisible3')"
  77. @confirm="confirm3"
  78. ></nut-picker>
  79. </div>
  80. </div>
  81. </template>
  82. <script>
  83. const APIData = [
  84. {
  85. label: 1,
  86. array: [
  87. {
  88. label: 1,
  89. value: '朝阳区'
  90. },
  91. {
  92. label: 2,
  93. value: '海淀区'
  94. },
  95. {
  96. label: 3,
  97. value: '大兴区'
  98. },
  99. {
  100. label: 4,
  101. value: '东城区'
  102. },
  103. {
  104. label: 5,
  105. value: '西城区'
  106. },
  107. {
  108. label: 6,
  109. value: '丰台区'
  110. }
  111. ]
  112. },
  113. {
  114. label: 2,
  115. array: [
  116. {
  117. label: 1,
  118. value: '黄浦区'
  119. },
  120. {
  121. label: 2,
  122. value: '长宁区'
  123. },
  124. {
  125. label: 3,
  126. value: '普陀区'
  127. },
  128. {
  129. label: 4,
  130. value: '杨浦区'
  131. },
  132. {
  133. label: 5,
  134. value: '浦东新区'
  135. }
  136. ]
  137. }
  138. ];
  139. export default {
  140. data() {
  141. return {
  142. option: '请选择',
  143. isVisible: false,
  144. listData: [
  145. [
  146. {
  147. label: 1,
  148. value: '南京市'
  149. },
  150. {
  151. label: 2,
  152. value: '无锡市'
  153. },
  154. {
  155. label: 3,
  156. value: '海北藏族自治区'
  157. },
  158. {
  159. label: 4,
  160. value: '北京市'
  161. },
  162. {
  163. label: 5,
  164. value: '连云港市'
  165. },
  166. {
  167. label: 6,
  168. value: '浙江市'
  169. }
  170. ]
  171. ],
  172. defaultValueData: null,
  173. option1: '请选择',
  174. isVisible1: false,
  175. listData1: [
  176. ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  177. ['上午', '下午', '晚上']
  178. ],
  179. defaultValueData1: null,
  180. option3: '请选择',
  181. isVisible3: false,
  182. cityCustmer: '请选择',
  183. isVisible2: false,
  184. custmerCityData: [
  185. [
  186. {
  187. label: 1,
  188. value: '北京'
  189. },
  190. {
  191. label: 2,
  192. value: '上海'
  193. }
  194. ]
  195. ]
  196. };
  197. },
  198. mounted() {
  199. this.defaultValueData = [this.listData[0][3]];
  200. this.defaultValueData1 = ['周四', '下午'];
  201. },
  202. methods: {
  203. openSwitch(param) {
  204. this[`${param}`] = true;
  205. },
  206. closeSwitch(param) {
  207. this[`${param}`] = false;
  208. },
  209. confirm(chooseData) {
  210. this.option = `${chooseData[0].value}`;
  211. },
  212. confirm1(chooseData) {
  213. this.option1 = `${chooseData[0]} ${chooseData[1]}`;
  214. },
  215. confirm3(chooseData) {
  216. this.option3 = `${chooseData[0].value}`;
  217. },
  218. setChooseValueCustmer(chooseData) {
  219. var str = chooseData.map(item => item.value).join('-');
  220. this.cityCustmer = str;
  221. },
  222. closeUpdateChooseValueCustmer(self, chooseData) {
  223. //此处模拟查询API,如果数据缓存了不需要再重新请求
  224. setTimeout(() => {
  225. let { label, value } = chooseData[0];
  226. var resItems = APIData.find(item => item.label == label);
  227. if (resItems && resItems.array.length) {
  228. this.$set(this.custmerCityData, 1, resItems.array);
  229. // 复原位置
  230. self.updateChooseValue(self, 0, chooseData[0]);
  231. self.updateChooseValue(self, 1, chooseData[1]);
  232. }
  233. }, 100);
  234. },
  235. updateChooseValueCustmer(self, index, resValue, cacheValueData) {
  236. // 本demo为二级联动,所以限制只有首列变动的时候触发事件
  237. if (index === 0) {
  238. //此处模拟查询API,如果数据缓存了不需要再重新请求
  239. let { label, value } = resValue;
  240. setTimeout(() => {
  241. var resItems = APIData.find(item => item.label == label);
  242. if (resItems && resItems.array.length) {
  243. this.$set(this.custmerCityData, 1, resItems.array);
  244. // 更新第二列位置
  245. self.updateChooseValue(self, index + 1, this.custmerCityData[1][0]);
  246. }
  247. }, 100);
  248. }
  249. }
  250. }
  251. };
  252. </script>