轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

小程序之ecshop地址3级联动

发布者: hkingson | 发布时间: 2018-2-25 12:08| 查看数: 5736| 评论数: 1|帖子模式

picker标签,官方给出的实例:

  1. <view class="section">
  2. <view class="section__title">地区选择器</view>
  3. <picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}">
  4. <view class="picker">
  5. 当前选择:{{array[index]}}
  6. </view>
  7. </picker>
  8. </view>
  1. Page({
  2. data: {
  3. array: ['美国', '中国', '巴西', '日本'],
  4. index: 0,
  5. },
  6. bindPickerChange: function(e) {
  7. console.log('picker发送选择改变,携带值为', e.detail.value)
  8. this.setData({
  9. index: e.detail.value
  10. })
  11. },
  12. })

wxml页面:

  1. <view class="add-list under-line" >
  2. <view class="add-lab">收货地址</view>
  3. <view class="add-text">
  4. <picker class="w-3" bindchange="bindPickerProvince" value="{{provinceIndex}}" range="{{province}}" >
  5. <view class="picker">{{province[provinceIndex]}}</view>
  6. </picker>
  7. <picker class="w-3" bindchange="bindPickerCity" value="{{cityIndex}}" range="{{city}}" >
  8. <view class="picker">{{city[cityIndex]}}</view>
  9. </picker>
  10. <picker class="w-3" bindchange="bindPickerDistrict" value="{{districtIndex}}" range="{{district}}" >
  11. <view class="picker">{{district[districtIndex]}}</view>
  12. </picker>
  13. </view>
  14. </view>

js页面:

  1. var app = getApp()
  2. Page({
  3. data:{
  4. motto: 'jxcat',
  5. serverUrl: app.globalData.ajaxUrl,
  6. baseUrl: app.globalData.baseUrl,
  7. title: "收货地址",
  8. address_id: "",
  9. address: "",
  10. province:[],
  11. province_id: [], //后台返回的数据对应 region_id city,district 与此相同
  12. province_name: [], //后台返回的数据对应 region_name
  13. provinceIndex: 0, //wxml页面选择的选项,从0开始
  14. provinceId: 0, //根据wxml页面选择的选项获取到province_id: []对应的region_id
  15. city:[].
  16. city_id: [],
  17. city_name: [],
  18. cityIndex: 0,
  19. cityId: 0,
  20. district:[],
  21. district_id: [],
  22. district_name: [],
  23. districtIndex: 0,
  24. districtId: 0,
  25. },
  26. onLoad:function(options){
  27. // 页面初始化 options为页面跳转所带来的参数
  28. var that = this
  29. var get_data
  30. wx.checkSession({
  31. success: function(){
  32. //登录态未过期
  33. wx.getStorage({
  34. key: 'wcx_session',
  35. success: function(sres) {
  36. get_data = {
  37. m: 'api',
  38. c: 'user' ,
  39. a: 'edit_address',
  40. wcx_session: sres.data,
  41. }
  42. if(options.act == 'edit'){
  43. get_data = {
  44. m: 'api',
  45. c: 'user' ,
  46. a: 'edit_address',
  47. id: options.id,
  48. wcx_session: sres.data,
  49. }
  50. }
  51. wx.request({
  52. url: app.globalData.ajaxUrl,
  53. data: get_data,
  54. header: {
  55. 'content-type': 'application/json'
  56. },
  57. success: function(res) {
  58. if(options.act == "edit"){
  59. that.data.provinceId = res.data.consignee.province
  60. that.data.cityId = res.data.consignee.city
  61. that.data.districtid = res.data.consignee.district
  62. }
  63. for(var i=0; i<res.data.province_list.length; i++){{
  64. that.data.province_id[i] = res.data.province_list[i].region_id //把region_id存入province_id
  65. that.data.province_name[i] = res.data.province_list[i].region_name //把region_name存入province_name
  66. if(res.data.consignee.province == res.data.province_list[i].region_id){
  67. that.data.provinceIndex = i
  68. }
  69. }}
  70. for(var i=0; i<res.data.city_list.length; i++){{
  71. that.data.city_id[i] = res.data.city_list[i].region_id
  72. that.data.city_name[i] = res.data.city_list[i].region_name
  73. if(res.data.consignee.city == res.data.city_list[i].region_id){
  74. that.data.cityIndex = i
  75. }
  76. }}
  77. for(var i=0; i<res.data.district_list.length; i++){{
  78. that.data.district_id[i] = res.data.district_list[i].region_id
  79. that.data.district_name[i] = res.data.district_list[i].region_name
  80. if(res.data.consignee.district == res.data.district_list[i].region_id){
  81. that.data.districtIndex = i
  82. }
  83. }}
  84. that.data.address_id = options.id
  85. that.setData({
  86. consignee: res.data.consignee,
  87. province: that.data.province_name,
  88. provinceIndex: that.data.provinceIndex,
  89. city: that.data.city_name,
  90. cityIndex: that.data.cityIndex,
  91. district: that.data.district_name,
  92. districtIndex: that.data.districtIndex
  93. })
  94. }
  95. })
  96. //request
  97. }
  98. })
  99. },
  100. fail: function(){
  101. //登录态过期
  102. wx.login()
  103. }
  104. })
  105. },
  106. bindPickerProvince: function(event){
  107. var that = this
  108. var getId = event.detail.value //获取到wxml选择的选项对应的下标,从0开始
  109. that.data.provinceId = that.data.province_id[getId] //根据获取到的下标获取到region_name对应的region_id
  110. wx.request({
  111. url: app.globalData.ajaxUrl,
  112. data: {
  113. m: 'api',
  114. c: 'public' ,
  115. a: 'region',
  116. rtype: 2,
  117. rparent: that.data.provinceId,
  118. },
  119. header: {
  120. 'content-type': 'application/json'
  121. },
  122. success: function(res){
  123. for(var i=0; i<res.data.regions.length; i++){{
  124. that.data.city_id[i] = res.data.regions[i].region_id
  125. that.data.city_name[i] = res.data.regions[i].region_name
  126. }}
  127. that.setData({
  128. city: that.data.city_name,
  129. provinceIndex: getId,
  130. })
  131. },
  132. })
  133. },
  134. bindPickerCity: function(event){
  135. var that = this
  136. var getId = event.detail.value
  137. that.data.cityId = that.data.city_id[getId]
  138. wx.request({
  139. url: app.globalData.ajaxUrl,
  140. data: {
  141. m: 'api',
  142. c: 'public' ,
  143. a: 'region',
  144. rtype: 3,
  145. rparent: that.data.cityId,
  146. },
  147. header: {
  148. 'content-type': 'application/json'
  149. },
  150. success: function(res){
  151. for(var i=0; i<res.data.regions.length; i++){{
  152. that.data.district_id[i] = res.data.regions[i].region_id
  153. that.data.district_name[i] = res.data.regions[i].region_name
  154. }}
  155. that.setData({
  156. district: that.data.district_name,
  157. cityIndex: getId,
  158. })
  159. },
  160. })
  161. },
  162. bindPickerDistrict: function(event){
  163. var that = this
  164. var getId = event.detail.value
  165. that.data.districtId = that.data.district_id[getId]
  166. that.setData({
  167. districtIndex: getId,
  168. })
  169. },
  170. formSubmit: function(event) {
  171. var that = this
  172. wx.checkSession({
  173. success: function(){
  174. //登录态未过期
  175. wx.getStorage({
  176. key: 'wcx_session',
  177. success: function(sres) {
  178. wx.request({
  179. url: app.globalData.ajaxUrl,
  180. data: {
  181. m: 'api',
  182. c: 'user' ,
  183. a: 'add_address',
  184. address_id: that.data.address_id,
  185. province: that.data.provinceId, // wxml页面选择的地址对应的 region_id
  186. city: that.data.cityId,
  187. district: that.data.districtId,
  188. address: event.detail.value.address,
  189. consignee: event.detail.value.consignee,
  190. mobile: event.detail.value.mobile,
  191. zipcode: event.detail.value.zipcode,
  192. wcx_session: sres.data,
  193. },
  194. header: {
  195. 'content-type': 'application/json'
  196. },
  197. success: function(res) {
  198. console.log(res)
  199. wx.redirectTo({
  200. url: 'address'
  201. })
  202. }
  203. })
  204. //request
  205. }
  206. })
  207. },
  208. fail: function(){
  209. //登录态过期
  210. wx.login()
  211. }
  212. })
  213. },
  214. })

最新评论

xinyui 发表于 2022-5-24 16:47
免费代码大全

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表