微信小程序 扩展组件索引列表组件


本文整理自网络,侵删。

index-list

索引列表组件,可实现类似通讯录效果。组件内节点将被添加到列表上方。

示例代码:

const res = {
  result: [
    [{
      cidx: [0, 15],
      fullname: "北京市",
      id: "110000",
      location: {
        lat: 39.90469,
        lng: 116.40717 
      },
      name: "北京",
      pinyin: ["bei", "jing"]
    }, {
      cidx: [16, 31],
      fullname: "天津市",
      id: "120000",
      location: {lat: 39.0851, lng: 117.19937},
      name: "天津",
      pinyin: ["tian", "jin"]
    }, {
      cidx: [32, 42],
      fullname: "河北省",
      id: "130000",
      location: {lat: 38.03599, lng: 114.46979},
      name: "河北",
      pinyin: ["he", "bei"],
    }, {
      cidx: [43, 53],
      fullname: "山西省",
      id: "140000",
      location: {lat: 37.87343, lng: 112.56272},
      name: "山西",
      pinyin:  ["shan", "xi"],
    }]
  ]
}
Page({

  onLoad(options) {
    this.getCitys()
  },

  onChoose(e) {
    console.log('onChoose', e)
  },

  getCitys() {
    const _this = this
    const cities = res.result[0]
        // 按拼音排序
        cities.sort((c1, c2) => {
          let pinyin1 = c1.pinyin.join('')
          let pinyin2 = c2.pinyin.join('')
          return pinyin1.localeCompare(pinyin2)
        })
        // 添加首字母
        const map = new Map()
        for (const city of cities) {
          const alpha = city.pinyin[0].charAt(0).toUpperCase()
          if (!map.has(alpha)) map.set(alpha, [])
          map.get(alpha).push({ name: city.fullname })
        }
    
        const keys = []
        for (const key of map.keys()) {
          keys.push(key)
        }
        keys.sort()
    
        const list = []
        for (const key of keys) {
          list.push({
            alpha: key,
            subItems: map.get(key)
          })
        }

        _this.setData({list})
  }

})

<mp-indexList class="city__list" list="{{list}}" bindchoose="onChoose">
    <view class="page">
        <view class="page__hd">
            <view class="page__title">Index List</view>
            <view class="page__desc">类通讯录列表</view>
        </view>
        <view class="page__bd">
        </view>
    </view>
</mp-indexList>


属性列表

属性类型默认值必填说明
listArray<listItem>[]列表数据
vibratedbooleantrue索引上滑动时是否产生振动,仅 iOS 生效
bindchooseeventhandle选择列表项, e.detail={name}

listItem 属性列表

属性类型说明
alphastring首字母(大写)
subItemsArray<subItem>子元素集合

subItem 属性列表

属性类型说明
namestring名称

注意事项

  1. demo 中省市信息为模拟数据,开发者可以使用腾讯位置服务提供的 SDK 来获取省市信息。



标签:微信小程序

相关阅读 >>

微信小程序api 背景音频-backgroundaudiomanager实例

微信小程序 getaccesstoken

微信小程序api 字体

微信小程序 多线程worker

微信小程序api coordinates(canvas 坐标系)

微信小程序中调用scale方法对横纵坐标进行缩放

微信小程序api 图片-预览图片

微信小程序 小程序使用getpath

微信小程序 扩展组件小程序弹幕组件

微信小程序设计规范(1) 友好礼貌

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...