当前位置:Gxlcms > JavaScript > 微信小程序 select 下拉框组件功能

微信小程序 select 下拉框组件功能

时间:2021-07-01 10:21:17 帮助过:24人阅读

一、源码地址

https://github.com/imxiaoer/WeChatMiniSelect

二、效果图

录屏图片质量较差,所以大家会看到残影(捂脸)

三、组件源码

1. select.wxml

  1. <view class="select-box">
  2. <view class="select-current" catchtap="openClose">
  3. <text class="current-name">{{current.name}}</text>
  4. </view>
  5. <view class="option-list" wx:if="{{isShow}}" catchtap="optionTap">
  6. <text class="option"
  7. data-id="{{defaultOption.id}}"
  8. data-name="{{defaultOption.name}}">{{defaultOption.name}}
  9. </text>
  10. <text class="option"
  11. wx:for="{{result}}"
  12. wx:key="{{item.id}}"
  13. data-id="{{item.id}}"
  14. data-name="{{item.name}}">{{item.name}}
  15. </text>
  16. </view>
  17. </view>

说明:用 catchtap 而不用 bindtap 是为了阻止事件冒泡,为了实现点击页面其他地方关闭 select, 所以在父页面(index.wxml)最外层绑定了 bindtap="close" 方法, 不阻止冒泡的话会执行父组件的 close 方法

 2. select.js

 

  1. Component({
  2. properties: {
  3. options: {
  4. type: Array,
  5. value: []
  6. },
  7. defaultOption: {
  8. type: Object,
  9. value: {
  10. id: '000',
  11. name: '全部城市'
  12. }
  13. },
  14. key: {
  15. type: String,
  16. value: 'id'
  17. },
  18. text: {
  19. type: String,
  20. value: 'name'
  21. }
  22. },
  23. data: {
  24. result: [],
  25. isShow: false,
  26. current: {}
  27. },
  28. methods: {
  29. optionTap(e) {
  30. let dataset = e.target.dataset
  31. this.setData({
  32. current: dataset,
  33. isShow: false
  34. });
  35. // 调用父组件方法,并传参
  36. this.triggerEvent("change", { ...dataset })
  37. },
  38. openClose() {
  39. this.setData({
  40. isShow: !this.data.isShow
  41. })
  42. },
  43. // 此方法供父组件调用
  44. close() {
  45. this.setData({
  46. isShow: false
  47. })
  48. }
  49. },
  50. lifetimes: {
  51. attached() {
  52. // 属性名称转换, 如果不是 { id: '', name:'' } 格式,则转为 { id: '', name:'' } 格式
  53. let result = []
  54. if (this.data.key !== 'id' || this.data.text !== 'name') {
  55. for (let item of this.data.options) {
  56. let { [this.data.key]: id, [this.data.text]: name } = item
  57. result.push({ id, name })
  58. }
  59. }
  60. this.setData({
  61. current: Object.assign({}, this.data.defaultOption),
  62. result: result
  63. })
  64. }
  65. }
  66. })

说明:properties中的 key 和 text 是为了做属性名转换。比如我现在的数据结构如下:

  1. [{
  2. city_id: '001',
  3. city_name: '北京'
  4. }, {
  5. city_id: '002',
  6. city_name: '上海'
  7. }, {
  8. city_id: '003',
  9. city_name: '深圳'
  10. }]

而 select 组件要求的数据结构是:

  1. [{
  2. id: '001',
  3. name: '北京'
  4. }, {
  5. id: '002',
  6. name: '上海'
  7. }, {
  8. id: '003',
  9. name: '深圳'
  10. }]

因此我们就要将 city_id 转换成 id,city_name 转换成 name。 怎么实现属性名转换呢? 就是通过 key 和 text 这两个参数。

 3. select.json

  1. {
  2. "component": true,
  3. "usingComponents": {}
  4. }

4. select.wxss

  1. .select-box {
  2. position: relative;
  3. width: 100%;
  4. font-size: 30rpx;
  5. }
  6. .select-current {
  7. position: relative;
  8. width: 100%;
  9. padding: 0 10rpx;
  10. line-height: 70rpx;
  11. border: 1rpx solid #ddd;
  12. border-radius: 6rpx;
  13. box-sizing: border-box;
  14. }
  15. .select-current::after {
  16. position: absolute;
  17. display: block;
  18. right: 16rpx;
  19. top: 30rpx;
  20. content: '';
  21. width: 0;
  22. height: 0;
  23. border: 10rpx solid transparent;
  24. border-top: 10rpx solid #999;
  25. }
  26. .current-name {
  27. display: block;
  28. width: 85%;
  29. height: 100%;
  30. word-wrap: normal;
  31. overflow: hidden;
  32. }
  33. .option-list {
  34. position: absolute;
  35. left: 0;
  36. top: 76rpx;
  37. width: 100%;
  38. padding: 12rpx 20rpx 10rpx 20rpx;
  39. border-radius: 6rpx;
  40. box-sizing: border-box;
  41. z-index: 99;
  42. box-shadow: 0rpx 0rpx 1rpx 1rpx rgba(0, 0, 0, 0.2) inset;
  43. background-color: #fff;
  44. }
  45. .option {
  46. display: block;
  47. width: 100%;
  48. line-height: 70rpx;
  49. border-bottom: 1rpx solid #eee;
  50. }
  51. .option:last-child {
  52. border-bottom: none;
  53. padding-bottom: 0;
  54. }

 四、组件的使用

index.wxml

  1. <view class="container" bindtap="close">
  2. <view class="select-wrap">
  3. <select id="select" options="{{options}}" key="city_id" text="city_name" bind:change="change"></select>
  4. </view>
  5. </view>

总结

以上所述是小编给大家介绍的微信小程序 select 下拉框组件功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

人气教程排行