DropdownMenu 下拉菜单

引入

app.jsonindex.json中引入组件,默认为ES6版本,ES5引入方式参见快速上手

  1. "usingComponents": {
  2. "van-dropdown-menu": "path/to/@vant/weapp/dist/dropdown-menu/index",
  3. "van-dropdown-item": "path/to/@vant/weapp/dist/dropdown-item/index"
  4. }

代码演示

基础用法

  1. <van-dropdown-menu>
  2. <van-dropdown-item value="{{ value1 }}" options="{{ option1 }}" />
  3. <van-dropdown-item value="{{ value2 }}" options="{{ option2 }}" />
  4. </van-dropdown-menu>
  1. Page({
  2. data: {
  3. option1: [
  4. { text: '全部商品', value: 0 },
  5. { text: '新款商品', value: 1 },
  6. { text: '活动商品', value: 2 }
  7. ],
  8. option2: [
  9. { text: '默认排序', value: 'a' },
  10. { text: '好评排序', value: 'b' },
  11. { text: '销量排序', value: 'c' }
  12. ],
  13. value1: 0,
  14. value2: 'a'
  15. }
  16. });

自定义菜单内容

  1. <van-dropdown-menu>
  2. <van-dropdown-item value="{{ value1 }}" options="{{ option1 }}" />
  3. <van-dropdown-item id="item" title="{{ itemTitle }}">
  4. <van-cell title="{{ switchTitle1 }}">
  5. <van-switch
  6. slot="right-icon"
  7. size="24px"
  8. style="height: 26px"
  9. checked="{{ switch1 }}"
  10. bind:change="onSwitch1Change"
  11. />
  12. </van-cell>
  13. <van-cell title="{{ switchTitle2 }}">
  14. <van-switch
  15. slot="right-icon"
  16. size="24px"
  17. style="height: 26px"
  18. checked="{{ switch2 }}"
  19. bind:change="onSwitch2Change"
  20. />
  21. </van-cell>
  22. <van-button type="info" block bind:click="onConfirm">
  23. 确定
  24. </van-button>
  25. </van-dropdown-item>
  26. </van-dropdown-menu>
Page({
  data: {
    switchTitle1: '包邮',
    switchTitle2: '团购',
    itemTitle: '筛选',
    option1: [
      { text: '全部商品', value: 0 },
      { text: '新款商品', value: 1 },
      { text: '活动商品', value: 2 }
    ],
    value1: 0,
  },

  onConfirm () {
    this.selectComponent('#item').toggle();
  },

  onSwitch1Change ({ detail }) {
    this.setData({ switch1: detail });
  },

  onSwitch2Change ({ detail }) {
    this.setData({ switch2: detail });
  }
});

自定义选中状态颜色

<van-dropdown-menu active-color="#ee0a24">
  <van-dropdown-item value="{{ value1 }}" options="{{ option1 }}" />
  <van-dropdown-item value="{{ value2 }}" options="{{ option2 }}" />
</van-dropdown-menu>

向上展开

<van-dropdown-menu direction="up">
  <van-dropdown-item value="{{ value1 }}" options="{{ option1 }}" />
  <van-dropdown-item value="{{ value2 }}" options="{{ option2 }}" />
</van-dropdown-menu>

禁用菜单

<van-dropdown-menu>
  <van-dropdown-item value="{{ value1 }}" disabled options="{{ option1 }}" />
  <van-dropdown-item value="{{ value2 }}" disabled options="{{ option2 }}" />
</van-dropdown-menu>

API

DropdownMenu Props

AttributeDescriptionTypeDefaultVersion
active-color菜单标题和选项的选中态颜色string#1989fa-
z-index菜单栏 z-index 层级number10-
duration动画时长,单位毫秒number200-
direction菜单展开方向,可选值为upstringdown-
overlay是否显示遮罩层booleantrue-
close-on-click-overlay是否在点击遮罩层后关闭菜单booleantrue-
close-on-click-outside是否在点击外部 menu 后关闭菜单booleantrue-

DropdownItem Props

AttributeDescriptionTypeDefaultVersion
value当前选中项对应的 valuestring | number--
title菜单项标题stringText of selected option-
options选项数组Option[][]-
disabled是否禁用菜单booleanfalse-
title-class标题额外类名string--

DropdownItem Events

EventDescriptionArguments
change点击选项导致 value 变化时触发value
close关闭菜单栏时触发-

DropdownItem Methods

通过 selectComponent(id) 可访问

NameAttributeReturn valueDescription
toggleshow: boolean-切换菜单是否展示

Data Structure of Option

KeyDescriptionType
text文字string
value标识符string | number
icon左侧图标名称或图片链接,可选值见 Icon 组件string

DropdownMenu 下拉菜单 - 图1