国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Vant2 Picker 選擇器

2025-08-27 14:46 更新

介紹

提供多個(gè)選項(xiàng)集合供用戶選擇,支持單列選擇和多列級(jí)聯(lián),通常與彈出層組件配合使用。

引入

import Vue from 'vue';
import { Picker } from 'vant';

Vue.use(Picker);

代碼演示

基礎(chǔ)用法

選項(xiàng)配置

Picker 組件通過 columns 屬性配置選項(xiàng)數(shù)據(jù),columns 是一個(gè)包含字符串或?qū)ο蟮臄?shù)組。

頂部欄

設(shè)置 show-toolbar 屬性后會(huì)展示頂部操作欄,頂部欄包含標(biāo)題、確認(rèn)按鈕和取消按鈕,點(diǎn)擊確認(rèn)按鈕觸發(fā) confirm 事件,點(diǎn)擊取消按鈕觸發(fā) cancel 事件。

<van-picker
  title="標(biāo)題"
  show-toolbar
  :columns="columns"
  @confirm="onConfirm"
  @cancel="onCancel"
  @change="onChange"
/>
import { Toast } from 'vant';

export default {
  data() {
    return {
      columns: ['杭州', '寧波', '溫州', '紹興', '湖州', '嘉興', '金華', '衢州'],
    };
  },
  methods: {
    onConfirm(value, index) {
      Toast(`當(dāng)前值:${value}, 當(dāng)前索引:${index}`);
    },
    onChange(picker, value, index) {
      Toast(`當(dāng)前值:${value}, 當(dāng)前索引:${index}`);
    },
    onCancel() {
      Toast('取消');
    },
  },
};

默認(rèn)選中項(xiàng)

單列選擇時(shí),可以通過 default-index 屬性設(shè)置初始選中項(xiàng)的索引。

<van-picker show-toolbar title="標(biāo)題" :columns="columns" :default-index="2" />

多列選擇

columns 屬性可以通過對(duì)象數(shù)組的形式配置多列選擇,對(duì)象中可以配置選項(xiàng)數(shù)據(jù)、初始選中項(xiàng)等,詳細(xì)格式見下方表格。

<van-picker show-toolbar title="標(biāo)題" :columns="columns" />
export default {
  data() {
    return {
      columns: [
        // 第一列
        {
          values: ['周一', '周二', '周三', '周四', '周五'],
          defaultIndex: 2,
        },
        // 第二列
        {
          values: ['上午', '下午', '晚上'],
          defaultIndex: 1,
        },
      ],
    };
  },
};

級(jí)聯(lián)選擇

使用 columnschildren 字段可以實(shí)現(xiàn)選項(xiàng)級(jí)聯(lián)的效果(從 2.4.5 版本開始支持)。

<van-picker show-toolbar title="標(biāo)題" :columns="columns" />
export default {
  data() {
    return {
      columns: [
        {
          text: '浙江',
          children: [
            {
              text: '杭州',
              children: [{ text: '西湖區(qū)' }, { text: '余杭區(qū)' }],
            },
            {
              text: '溫州',
              children: [{ text: '鹿城區(qū)' }, { text: '甌海區(qū)' }],
            },
          ],
        },
        {
          text: '福建',
          children: [
            {
              text: '福州',
              children: [{ text: '鼓樓區(qū)' }, { text: '臺(tái)江區(qū)' }],
            },
            {
              text: '廈門',
              children: [{ text: '思明區(qū)' }, { text: '海滄區(qū)' }],
            },
          ],
        },
      ],
    };
  },
};

級(jí)聯(lián)選擇的數(shù)據(jù)嵌套深度需要保持一致,如果部分選項(xiàng)沒有子選項(xiàng),可以使用空字符串進(jìn)行占位

禁用選項(xiàng)

選項(xiàng)可以為對(duì)象結(jié)構(gòu),通過設(shè)置 disabled 來禁用該選項(xiàng)。

<van-picker show-toolbar :columns="columns" />
export default {
  data() {
    return {
      columns: [
        { text: '杭州', disabled: true },
        { text: '寧波' },
        { text: '溫州' },
      ],
    };
  },
};

動(dòng)態(tài)設(shè)置選項(xiàng)

通過 Picker 上的實(shí)例方法可以更靈活地控制選擇器,比如使用 setColumnValues 方法實(shí)現(xiàn)多列聯(lián)動(dòng)。

<van-picker show-toolbar :columns="columns" @change="onChange" />
const cities = {
  浙江: ['杭州', '寧波', '溫州', '嘉興', '湖州'],
  福建: ['福州', '廈門', '莆田', '三明', '泉州'],
};

export default {
  data() {
    return {
      columns: [{ values: Object.keys(cities) }, { values: cities['浙江'] }],
    };
  },
  methods: {
    onChange(picker, values) {
      picker.setColumnValues(1, cities[values[0]]);
    },
  },
};

加載狀態(tài)

若選擇器數(shù)據(jù)是異步獲取的,可以通過 loading 屬性顯示加載提示。

<van-picker show-toolbar :columns="columns" :loading="loading" />
export default {
  data() {
    return {
      columns: [],
      loading: true,
    };
  },
  created() {
    setTimeout(() => {
      this.loading = false;
      this.columns = ['選項(xiàng)'];
    }, 1000);
  },
};

搭配彈出層使用

在實(shí)際場(chǎng)景中,Picker 通常作為用于輔助表單填寫,可以搭配 Popup 和 Field 實(shí)現(xiàn)該效果。

<van-field
  readonly
  clickable
  label="城市"
  :value="value"
  placeholder="選擇城市"
  @click="showPicker = true"
/>
<van-popup v-model="showPicker" round position="bottom">
  <van-picker
    show-toolbar
    :columns="columns"
    @cancel="showPicker = false"
    @confirm="onConfirm"
  />
</van-popup>
export default {
  data() {
    return {
      value: '',
      showPicker: false,
      columns: ['杭州', '寧波', '溫州', '紹興', '湖州', '嘉興', '金華', '衢州'],
    };
  },
  methods: {
    onConfirm(value) {
      this.value = value;
      this.showPicker = false;
    },
  },
};

API

Props

參數(shù) 說明 類型 默認(rèn)值
columns 對(duì)象數(shù)組,配置每一列顯示的數(shù)據(jù) Column[] []
title 頂部欄標(biāo)題 string -
confirm-button-text 確認(rèn)按鈕文字 string 確認(rèn)
cancel-button-text 取消按鈕文字 string 取消
value-key 選項(xiàng)對(duì)象中,選項(xiàng)文字對(duì)應(yīng)的鍵名 string text
toolbar-position 頂部欄位置,可選值為bottom string top
loading 是否顯示加載狀態(tài) boolean false
readonly v2.10.5 是否為只讀狀態(tài),只讀狀態(tài)下無法切換選項(xiàng) boolean false
show-toolbar 是否顯示頂部欄 boolean false
allow-html 是否允許選項(xiàng)內(nèi)容中渲染 HTML boolean true
default-index 單列選擇時(shí),默認(rèn)選中項(xiàng)的索引 number | string 0
item-height v2.8.6 選項(xiàng)高度,支持 px vw vh rem 單位,默認(rèn) px number | string 44
visible-item-count 可見的選項(xiàng)個(gè)數(shù) number | string 6
swipe-duration 快速滑動(dòng)時(shí)慣性滾動(dòng)的時(shí)長,單位 ms number | string 1000

Events

當(dāng)選擇器有多列時(shí),事件回調(diào)參數(shù)會(huì)返回?cái)?shù)組

事件名 說明 回調(diào)參數(shù)
confirm 點(diǎn)擊完成按鈕時(shí)觸發(fā) 單列:選中值,選中值對(duì)應(yīng)的索引
多列:所有列選中值,所有列選中值對(duì)應(yīng)的索引
cancel 點(diǎn)擊取消按鈕時(shí)觸發(fā) 單列:選中值,選中值對(duì)應(yīng)的索引
多列:所有列選中值,所有列選中值對(duì)應(yīng)的索引
change 選項(xiàng)改變時(shí)觸發(fā) 單列:Picker 實(shí)例,選中值,選中值對(duì)應(yīng)的索引
多列:Picker 實(shí)例,所有列選中值,當(dāng)前列對(duì)應(yīng)的索引

Slots

名稱 說明 參數(shù)
default 自定義整個(gè)頂部欄的內(nèi)容 -
title 自定義標(biāo)題內(nèi)容 -
confirm v2.10.11 自定義確認(rèn)按鈕內(nèi)容 -
cancel v2.10.11 自定義取消按鈕內(nèi)容 -
option v2.10.11 自定義選項(xiàng)內(nèi)容 option: string | object
columns-top 自定義選項(xiàng)上方內(nèi)容 -
columns-bottom 自定義選項(xiàng)下方內(nèi)容 -

Column 數(shù)據(jù)結(jié)構(gòu)

當(dāng)傳入多列數(shù)據(jù)時(shí),columns 為一個(gè)對(duì)象數(shù)組,數(shù)組中的每一個(gè)對(duì)象配置每一列,每一列有以下 key:

鍵名 說明 類型
values 列中對(duì)應(yīng)的備選值 string[]
defaultIndex 初始選中項(xiàng)的索引,默認(rèn)為 0 number
className 為對(duì)應(yīng)列添加額外的類名 any
children 級(jí)聯(lián)選項(xiàng) Column

方法

通過 ref 可以獲取到 Picker 實(shí)例并調(diào)用實(shí)例方法,詳見組件實(shí)例方法。

方法名 說明 參數(shù) 返回值
getValues 獲取所有列選中的值 - values
setValues 設(shè)置所有列選中的值 values -
getIndexes 獲取所有列選中值對(duì)應(yīng)的索引 - indexes
setIndexes 設(shè)置所有列選中值對(duì)應(yīng)的索引 indexes -
getColumnValue 獲取對(duì)應(yīng)列選中的值 columnIndex value
setColumnValue 設(shè)置對(duì)應(yīng)列選中的值 columnIndex, value -
getColumnIndex 獲取對(duì)應(yīng)列選中項(xiàng)的索引 columnIndex optionIndex
setColumnIndex 設(shè)置對(duì)應(yīng)列選中項(xiàng)的索引 columnIndex, optionIndex -
getColumnValues 獲取對(duì)應(yīng)列中所有選項(xiàng) columnIndex values
setColumnValues 設(shè)置對(duì)應(yīng)列中所有選項(xiàng) columnIndex, values -
confirm 停止慣性滾動(dòng)并觸發(fā) confirm 事件 - -

樣式變量

組件提供了下列 Less 變量,可用于自定義樣式,使用方法請(qǐng)參考主題定制。

名稱 默認(rèn)值 描述
@picker-background-color @white -
@picker-toolbar-height 44px -
@picker-title-font-size @font-size-lg -
@picker-title-line-height @line-height-md -
@picker-action-padding 0 @padding-md -
@picker-action-font-size @font-size-md -
@picker-confirm-action-color @text-link-color -
@picker-cancel-action-color @gray-6 -
@picker-option-font-size @font-size-lg -
@picker-option-text-color @black -
@picker-option-disabled-opacity 0.3 -
@picker-loading-icon-color @blue -
@picker-loading-mask-color rgba(255, 255, 255, 0.9) -

常見問題

在桌面端無法操作組件?

參見桌面端適配。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)