支付宝小程序 省市区三级联动

 <!--级联选择器-->
  <multi-select selectShow="{{selectShow}}" selectValue="{{selectValue}}" list="{{rawCitiesData}}" directoryCode="{{directoryCode}}" onSelectSuccess="onSelectSuccess" onClose="closeSelect"/>
  <!---->

组件部分

select.json

{
  "component": true,
  "usingComponents": {
    "popup": "mini-antui/es/popup/index",
    "tabs": "mini-antui/es/tabs/index",
    "tab-content": "mini-antui/es/tabs/tab-content/index"
  }
}Ï

select.axml

<view>
  <popup show="{{selectShow}}" position="bottom" onClose="onPopupClose" className="picker-popup" disableScroll="{{false}}">
    <view class="btn-action">
      <view class="cancel-btn" data-click="{{isConfirm}}" onTap="onCancel">取消                                                            
      </view>
      <view class="confirm-btn {{isConfirm?'active':'disable'}}" data-click="{{isConfirm}}" onTap="onConfirm">确定                                                            
      </view>
    </view>
    <tabs className="pipick-view-tab" tabBarCls="pick-view-tab-header" activeCls="activeTab" tabBarActiveTextColor="#333333" tabBarBackgroundColor="transparent" tabs="{{selectList}}" swipeable="{{false}}" onTabClick="handleTabClick" activeTab="{{activeTab}}">
      <block a:for="{{selectList}}" a:for-index="idx" a:for-item="itemName">
        <tab-content key="{{idx}}">
          <scroll-view class="pick-view-content" scroll-y="{{true}}" trap-scroll="{{true}}">
            <block a:for="{{itemName.sub}}">
              <view data-key="{{idx}}" data-name="{{item.name}}" data-code="{{item.code}}" data-sub="{{item.sub?item.sub:''}}" class="pick-view-content-item {{selectList[idx].title===item.name?'curret':''}}" onTap="itemSelect">{{item.name}}                                                        
              </view>
            </block>
          </scroll-view>
        </tab-content>
      </block>
    </tabs>
  </popup>
</view>

select.js

  1. 省市区数据全部返回
Component({
  data: {
    isConfirm: false,
    // 确认是否可点击
    selectList: [{
      title: '请选择',
      sub: []
    }],
    // list数据
    activeTab: 0 // 当前tab项

  },
  props: {
    selectShow: false // 选择面板是否展示
  },
  didMount: function didMount() {
    this.setData({
      selectList: [{
        title: '请选择',
        sub: this.props.list
      }]
    });
  },
  didUpdate: function didUpdate(prevProps) {
    if (!prevProps.selectShow && this.props.selectShow && this.props.list != prevProps.list) {
      // console.log('didUpdate', this.props.list)
      this.setData({
        selectList: [
          {
            title: '请选择',
            sub: this.props.list
          }
        ],
        isConfirm: false,
        activeTab: 0
      })
      // console.log(this.data.selectList)
    }
  },
  didUnmount: function didUnmount() { },
  methods: {
    /**
    * 关闭popup
    * @method onPopupClose
    */
    onPopupClose: function onPopupClose() {
      var _this$props2 = this.props,
        selectValue = _this$props2.selectValue,
        list = _this$props2.list;

      if (!selectValue) {
        this.setData({
          isConfirm: false,
          selectList: [{
            title: '请选择',
            sub: list
          }],
          activeTab: 0
        });
      }

      this.props.onClose();
    },

    /**
     * 确认
     * @method onConfirm
     * @param {*} e
     */
    onConfirm: function onConfirm(e) {
      if (e.target.dataset.click) {
        // 点击确定
        var selectList = this.data.selectList;
        var result = [];
        selectList.forEach(function (item) {
          var singleSelect = {
            name: item.title,
            code: item.code,
          };
          result.push(singleSelect);
        });
        console.log('selectList', selectList[selectList.length - 1])
        result.push(selectList[selectList.length - 1].sub[0].TASK_CODE)
        this.props.onSelectSuccess(result);
        this.props.onClose();
      }
    },

    /**
     * 取消
     * @method onCancel
     */
    onCancel: function onCancel() {
      this.props.onClose();
    },

    /**
     * tab切换
     * @method handleTabClick
     * @param {*} index
     */
    handleTabClick: function handleTabClick(_ref) {
      var index = _ref.index;
      // console.log("xxxx", index)
      this.setData({
        activeTab: index
      });
    },

    /**
     * 省市区选择事件
     * @method itemSelect
     * @param {*} e
     */
    itemSelect: function itemSelect(e) {
      var _e$target$dataset = e.target.dataset,
        key = _e$target$dataset.key,
        name = _e$target$dataset.name,
        code = _e$target$dataset.code,
        sub = _e$target$dataset.sub;
      var list = this.props.list;

      if (key === 0) {
        // 第一级数据处理
        if (sub) {
          this.setData({
            selectList: [{
              title: name,
              code: code,
              sub: list
            }, {
              title: '城市',
              sub: sub
            }],
            activeTab: 1,
            isConfirm: false
          });
        } else {
          this.setData({
            selectList: [{
              title: name,
              code: code,
              sub: list
            }],
            isConfirm: true
          });
        }
      }

      if (key === 1) {
        // 第二级数据处理
        if (sub) {
          this.setData({
            selectList: [{
              title: this.data.selectList[0].title,
              code: this.data.selectList[0].code,
              sub: list
            }, {
              title: name,
              code: code,
              sub: this.data.selectList[1].sub
            }, {
              title: '区县',
              sub: sub
            }],
            activeTab: 2,
            isConfirm: false
          });
        } else {
          this.setData({
            selectList: [{
              title: this.data.selectList[0].title,
              code: this.data.selectList[0].code,
              sub: list
            }, {
              title: name,
              code: code,
              sub: this.data.selectList[1].sub
            }],
            activeTab: 1,
            isConfirm: true
          });
        }
      }

      if (key === 2) {
        // 第三级数据处理
        this.setData({
          selectList: [{
            title: this.data.selectList[0].title,
            code: this.data.selectList[0].code,
            sub: list
          }, {
            title: this.data.selectList[1].title,
            code: this.data.selectList[1].code,
            sub: this.data.selectList[1].sub
          }, {
            title: name,
            code: code,
            sub: this.data.selectList[2].sub
          }],
          activeTab: 2,
          isConfirm: true
        });
      }
    }
  }
});
  1. 省市区数据一级级获取
import { getLocation } from '/services/all';

Component({
  data: {
    isConfirm: false, // 确认是否可点击
    selectList: [
      {
        title: '请选择',
        sub: []
      }
    ], // list数据
    activeTab: 0,// 当前tab项
  },
  props: {
    selectShow: false // 选择面板是否展示
  },
  async didMount() {
    // 获取省
    const areares = await getLocation('area', '');
    let rawCitiesData = areares.data.data;
    rawCitiesData.forEach(item => { item.sub = [] })

    this.setData({
      selectList: [
        {
          title: '请选择',
          sub: rawCitiesData
        }
      ]
    });
  },
  async didUpdate(prevProps, prevData) {
    let { selectValue, list } = this.props;
    if (!list.length) {
      list = this.data.selectList
    }

    // console.log(list, 'update')
    // console.log(selectValue, 'update1')

    // 面板状态改变的时候数据的重新渲染
    if (!prevProps.selectShow && this.props.selectShow && selectValue) {
      const selectArray = selectValue.split(' ');
      let selectList = [];
      selectArray.map((item, k) => {

        if (k === 0) {
          const provinces = {
            title: item,
            sub: list
          };
          selectList.push(provinces);
        }
        if (k === 1) {
          list.map((data, index) => {
            if (data.name === selectArray[k - 1]) {
              const city = {
                title: item,
                sub: data.sub
              };
              selectList.push(city);
            }
          });
        }
        if (k === 2) {
          list.map((data, index) => {
            if (data.name === selectArray[k - 2]) {
              data.sub.map((areaData, i) => {
                if (areaData.name === selectArray[k - 1]) {
                  const area = {
                    title: item,
                    sub: areaData.sub
                  };
                  selectList.push(area);
                }
              });
            }
          });
        }
      });

      this.setData({
        // selectList,
        activeTab: selectArray.length - 1
      });
    }
  },
  didUnmount() { },
  methods: {
		/**
	   * 关闭popup
	   * @method onPopupClose
	   */
    async onPopupClose() {
      const { selectValue, list } = this.props;
      if (!selectValue) {
        this.setData({
          isConfirm: false,
          // selectList: [
          //   {
          //     title: '请选择',
          //     // sub: list
          //   }
          // ],
          activeTab: 0
        });
      }
      this.props.onClose();
    },
		/**
		 * 确认
		 * @method onConfirm
		 * @param {*} e
		 */
    async onConfirm(e) {

      if (e.target.dataset.click) {
        // 点击确定
        const { selectList } = this.data;
        let result = [];
        selectList.map((item) => {
          const singleSelect = {
            name: item.title,
            code: item.code
          };
          result.push(singleSelect);
        });
        this.props.onSelectSuccess(result);
        this.props.onClose();
      }
    },
		/**
		 * 取消
		 * @method onCancel
		 */
    onCancel() {
      this.props.onClose();
    },
		/**
		 * tab切换
		 * @method handleTabClick
		 * @param {*} index
		 */
    handleTabClick({ index }) {
      this.setData({
        activeTab: index
      });
    },
		/**
		 * 省市区选择事件
		 * @method itemSelect
		 * @param {*} e
		 */
    async itemSelect(e) {
      const { key, name, code, sub } = e.target.dataset;
      const { list } = this.props;
      // console.log('itemselect', key)
      // console.log(code)
      if (key === 0) { // 第一级数据处理

        // 获取省
        const arearesfirst = await getLocation('area', code);
        let firstArr = arearesfirst.data.data;
        firstArr.forEach(item => { item.sub = [] })
        // console.log(firstArr)
        // console.log(this.data.selectList, 'selectList')
        let selectList = this.data.selectList[0].sub;

        // 获取下标
        const index = selectList.findIndex(item => { return item.code == code })
        if (firstArr.length) {
          this.setData({
            selectList: [
              {
                title: selectList[index].name,
                code,
                sub: this.data.selectList[0].sub
              },
              {
                title: '城市',
                sub: firstArr
              }
            ],
            activeTab: 1,
            isConfirm: false
          });
        } else {
          this.setData({
            selectList: [
              {
                title: selectList[index].name,
                code,
                sub: this.data.selectList[0].sub
              }
            ],
            isConfirm: true
          });
        }
      }
      if (key === 1) { // 第二级数据处理

        // 获取省
        const arearessecond = await getLocation('area', code);

        let secondArr = arearessecond.data.data;
        secondArr.forEach(item => { item.sub = [] })
        if (secondArr.length) {
          this.setData({
            selectList: [
              {
                title: this.data.selectList[0].title,
                code: this.data.selectList[0].code,
                sub: this.data.selectList[0].sub
              },
              {
                title: name,
                code,
                sub: this.data.selectList[1].sub
              },
              {
                title: '区',
                sub: secondArr
              }
            ],
            activeTab: 2,
            isConfirm: false
          });
        } else {
          this.setData({
            selectList: [
              {
                title: this.data.selectList[0].title,
                code: this.data.selectList[0].code,
                sub: this.data.selectList[0].sub
              },
              {
                title: name,
                code,
                sub: this.data.selectList[1].sub
              }
            ],
            activeTab: 1,
            isConfirm: true
          });
        }
      }
      if (key === 2) { // 第三级数据处理
        this.setData({
          selectList: [
            {
              title: this.data.selectList[0].title,
              code: this.data.selectList[0].code,
              sub: this.data.selectList[0].sub
            },
            {
              title: this.data.selectList[1].title,
              code: this.data.selectList[1].code,
              sub: this.data.selectList[1].sub
            },
            {
              title: name,
              code,
              sub: this.data.selectList[2].sub
            }
          ],
          activeTab: 2,
          isConfirm: true
        });
      }
    }
  }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值