Back to Vant Weapp

Cascader 级联选择

packages/cascader/README.md

1.11.76.0 KB
Original Source

Cascader 级联选择

介绍

级联选择框,用于多层级数据的选择,典型场景为省市区选择。

引入

app.jsonindex.json中引入组件,详细介绍见快速上手

json
"usingComponents": {
  "van-cascader": "@vant/weapp/cascader/index"
}

代码演示

基础用法

级联选择组件可以搭配 Field 和 Popup 组件使用,示例如下:

html
<van-field
  value="{{ fieldValue }}"
  is-link
  readonly
  label="地区"
  placeholder="请选择所在地区"
  bind:tap="onClick"
/>
<van-popup show="{{ show }}" round position="bottom">
  <van-cascader
    wx:if="{{ show }}"
    value="{{ cascaderValue }}"
    title="请选择所在地区"
    options="{{ options }}"
    bind:close="onClose"
    bind:finish="onFinish"
  />
</van-popup>
js

const options = [
  {
    text: '浙江省',
    value: '330000',
    children: [{ text: '杭州市', value: '330100' }],
  },
  {
    text: '江苏省',
    value: '320000',
    children: [{ text: '南京市', value: '320100' }],
  },
];

Page({
  data: {
    show: false,
    options,
    fieldValue: '',
    cascaderValue: '',
  },

  onClick() {
    this.setData({
      show: true,
    });
  },

  onClose() {
    this.setData({
      show: false,
    });
  },

  onFinish(e) {
    const { selectedOptions, value } = e.detail;
    const fieldValue = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      fieldValue,
      cascaderValue: value,
    })
  },
});

自定义颜色

通过 active-color 属性来设置选中状态的高亮颜色。

html
<van-cascader
  value="{{ cascaderValue }}"
  title="请选择所在地区"
  options="{{ options }}"
  active-color="#ee0a24"
  bind:close="onClose"
  bind:finish="onFinish"
/>

异步加载选项

可以监听 change 事件并动态设置 options,实现异步加载选项。

html
<van-field
  value="{{ fieldValue }}"
  is-link
  readonly
  label="地区"
  placeholder="请选择所在地区"
  bind:tap="onClick"
/>
<van-popup show="{{ show }}" round position="bottom">
  <van-cascader
    wx:if="{{ show }}"
    value="{{ cascaderValue }}"
    title="请选择所在地区"
    options="{{ options }}"
    bind:close="onClose"
    bind:change="onChange"
    bind:finish="onFinish"
  />
</van-popup>
js
Page({
  data: {
    options: [
      {
        text: '浙江省',
        value: '330000',
        children: [],
      }
    ];
  },
  onChange(e) {
    const { value } = e.detail;
    if (value === this.data.options[0].value) {
      setTimeout(() => {
        const children = [
          { text: '杭州市', value: '330100' },
          { text: '宁波市', value: '330200' },
        ];
        this.setData({
          'options[0].children': children,
        })
      }, 500);
    }
  },
});

自定义字段名

通过 field-names 属性可以自定义 options 里的字段名称。

html
<van-cascader
  value="{{ code }}"
  title="请选择所在地区"
  options="{{ options }}"
  field-names="{{ fieldNames }}"
/>
js
Page({
  data: {
    code: '',
    fieldNames: {
      text: 'name',
      value: 'code',
      children: 'items',
    },
    options: [
      {
        name: '浙江省',
        code: '330000',
        items: [{ name: '杭州市', code: '330100' }],
      },
      {
        name: '江苏省',
        code: '320000',
        items: [{ name: '南京市', code: '320100' }],
      },
    ],
  },
});

API

Props

参数说明类型默认值
title顶部标题string-
value选中项的值string | number-
options可选项数据源CascaderOption[][]
placeholder未选中时的提示文案string请选择
active-color选中状态的高亮颜色string#1989fa
swipeable是否开启手势左右滑动切换booleanfalse
closeable是否显示关闭图标booleantrue
ellipsis v1.11.7是否省略过长的标题文字, 关闭后文字过长会出现横向滚动booleantrue
show-header是否展示标题栏booleantrue
close-icon关闭图标名称或图片链接,等同于 Icon 组件的 name 属性stringcross
field-names自定义 options 结构中的字段CascaderFieldNames{ text: 'text', value: 'value', children: 'children' }
use-title-slot v1.11.3是否使用自定义标题的插槽booleanfalse

CascaderOption 数据结构

options 属性是一个由对象构成的数组,数组中的每个对象配置一个可选项,对象可以包含以下值:

键名说明类型
text选项文字(必填)string
value选项对应的值(必填)string | number
color选项文字颜色string
children子选项列表CascaderOption[]
disabled是否禁用选项boolean
className为对应列添加额外的 classstring | Array | object

Events

事件说明回调参数
bind:change选中项变化时触发event.detail:{ value: string | number, selectedOptions: CascaderOption[], tabIndex: number }
bind:finish全部选项选择完成后触发event.detail:{ value: string | number, selectedOptions: CascaderOption[], tabIndex: number }
bind:close点击关闭图标时触发-
bind:click-tab点击标签时触发event.detail:{ tabIndex: number, title: string }

Slots

名称说明参数
title自定义顶部标题-