99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Vant4 TreeSelect 分類選擇

2023-02-16 17:57 更新

介紹

用于從一組相關聯(lián)的數(shù)據(jù)集合中進行選擇。

引入

通過以下方式來全局注冊組件,更多注冊方式請參考組件注冊。

import { createApp } from 'vue';
import { TreeSelect } from 'vant';

const app = createApp();
app.use(TreeSelect);

代碼演示

單選模式

?item? 為分類顯示所需的數(shù)據(jù),數(shù)據(jù)格式見下方示例。?main-active-index? 表示左側高亮選項的索引,?active-id? 表示右側高亮選項的 id。

<van-tree-select
  v-model:active-id="activeId"
  v-model:main-active-index="activeIndex"
  :items="items"
/>
import { ref } from 'vue';

export default {
  setup() {
    const activeId = ref(1);
    const activeIndex = ref(0);
    const items = [
      {
        text: '浙江',
        children: [
          { text: '杭州', id: 1 },
          { text: '溫州', id: 2 },
          { text: '寧波', id: 3, disabled: true },
        ],
      },
      {
        text: '江蘇',
        children: [
          { text: '南京', id: 4 },
          { text: '無錫', id: 5 },
          { text: '徐州', id: 6 },
        ],
      },
      { text: '福建', disabled: true },
    ];

    return {
      items,
      activeId,
      activeIndex,
    };
  },
};

多選模式

?active-id? 為數(shù)組格式時,可以選中多個右側選項。

<van-tree-select
  v-model:active-id="activeIds"
  v-model:main-active-index="activeIndex"
  :items="items"
/>
import { ref } from 'vue';

export default {
  setup() {
    const activeId = ref([1, 2]);
    const activeIndex = ref(0);
    const items = [
      {
        text: '浙江',
        children: [
          { text: '杭州', id: 1 },
          { text: '溫州', id: 2 },
          { text: '寧波', id: 3, disabled: true },
        ],
      },
      {
        text: '江蘇',
        children: [
          { text: '南京', id: 4 },
          { text: '無錫', id: 5 },
          { text: '徐州', id: 6 },
        ],
      },
      { text: '福建', disabled: true },
    ];

    return {
      items,
      activeId,
      activeIndex,
    };
  },
};

自定義內容

通過 ?content? 插槽可以自定義右側區(qū)域的內容。

<van-tree-select
  v-model:main-active-index="activeIndex"
  height="55vw"
  :items="items"
>
  <template #content>
    <van-image
      v-if="activeIndex === 0"
      src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg" rel="external nofollow" 
    />
    <van-image
      v-if="activeIndex === 1"
      src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg" rel="external nofollow" 
    />
  </template>
</van-tree-select>
import { ref } from 'vue';

export default {
  setup() {
    const activeIndex = ref(0);
    return {
      activeIndex,
      items: [{ text: '分組 1' }, { text: '分組 2' }],
    };
  },
};

徽標提示

設置 ?dot? 屬性后,會在圖標右上角展示一個小紅點;設置 ?badge? 屬性后,會在圖標右上角展示相應的徽標。

<van-tree-select
  v-model:main-active-index="activeIndex"
  height="55vw"
  :items="items"
/>
import { ref } from 'vue';

export default {
  setup() {
    const activeIndex = ref(0);
    return {
      activeIndex,
      items: [
        {
          text: '浙江',
          children: [
            { text: '杭州', id: 1 },
            { text: '溫州', id: 2 },
            { text: '寧波', id: 3, disabled: true },
          ],
          dot: true,
        },
        {
          text: '江蘇',
          children: [
            { text: '南京', id: 4 },
            { text: '無錫', id: 5 },
            { text: '徐州', id: 6 },
          ],
          badge: 5,
        },
      ],
    };
  },
};

API

Props

參數(shù) 說明 類型 默認值
items 分類顯示所需的數(shù)據(jù) TreeSelectItem[] []
height 高度,默認單位為px number | string 300
main-active-index 左側選中項的索引 number | string 0
active-id 右側選中項的 id,支持傳入數(shù)組 number | string |
(number | string)[]
0
max 右側項最大選中個數(shù) number | string Infinity
selected-icon 自定義右側欄選中狀態(tài)的圖標 string success

Events

事件名 說明 回調參數(shù)
click-nav 點擊左側導航時觸發(fā) index: number
click-item 點擊右側選擇項時觸發(fā) item: TreeSelectChild

Slots

名稱 說明
content 自定義右側區(qū)域內容

TreeSelectItem 數(shù)據(jù)結構

?TreeSelectItem? 整體為一個數(shù)組,數(shù)組內包含一系列描述分類的對象,每個分類里,?text? 表示當前分類的名稱,?children? 表示分類里的可選項。

[
  {
    // 導航名稱
    text: '所有城市',
    // 導航名稱右上角徽標
    badge: 3,
    // 是否在導航名稱右上角顯示小紅點
    dot: true,
    // 導航節(jié)點額外類名
    className: 'my-class',
    // 該導航下所有的可選項
    children: [
      {
        // 名稱
        text: '溫州',
        // id,作為匹配選中狀態(tài)的標識符
        id: 1,
        // 禁用選項
        disabled: true,
      },
      {
        text: '杭州',
        id: 2,
      },
    ],
  },
];

類型定義

組件導出以下類型定義:

import type { TreeSelectItem, TreeSelectChild, TreeSelectProps } from 'vant';

主題定制

樣式變量

組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請參考 ConfigProvider 組件。

名稱 默認值 描述
--van-tree-select-font-size var(--van-font-size-md) -
--van-tree-select-nav-background var(--van-background) -
--van-tree-select-content-background var(--van-background-2) -
--van-tree-select-nav-item-padding 14px var(--van-padding-sm) -
--van-tree-select-item-height 48px -
--van-tree-select-item-active-color var(--van-primary-color) -
--van-tree-select-item-disabled-color var(--van-gray-5) -
--van-tree-select-item-selected-size 16px -


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號