ElementPlus Transfer 穿梭框

2021-09-24 21:10 更新

Transfer 穿梭框

基礎(chǔ)用法


Transfer 的數(shù)據(jù)通過 data 屬性傳入。數(shù)據(jù)需要是一個對象數(shù)組,每個對象有以下屬性:key 為數(shù)據(jù)的唯一性標識,label 為顯示文本,disabled 表示該項數(shù)據(jù)是否禁止轉(zhuǎn)移。目標列表中的數(shù)據(jù)項會同步到綁定至 v-model 的變量,值為數(shù)據(jù)項的 key 所組成的數(shù)組。當然,如果希望在初始狀態(tài)時目標列表不為空,可以像本例一樣為 v-model 綁定的變量賦予一個初始值。

<template>
  <el-transfer v-model="value" :data="data" />
</template>

<script>
  export default {
    data() {
      const generateData = (_) => {
        const data = []
        for (let i = 1; i <= 15; i++) {
          data.push({
            key: i,
            label: `備選項 ${i}`,
            disabled: i % 4 === 0,
          })
        }
        return data
      }
      return {
        data: generateData(),
        value: [1, 4],
      }
    },
  }
</script>

可搜索

在數(shù)據(jù)很多的情況下,可以對數(shù)據(jù)進行搜索和過濾。


設(shè)置 filterable 為 true 即可開啟搜索模式。默認情況下,若數(shù)據(jù)項的 label 屬性包含搜索關(guān)鍵字,則會在搜索結(jié)果中顯示。你也可以使用 filter-method 定義自己的搜索邏輯。filter-method 接收一個方法,當搜索關(guān)鍵字變化時,會將當前的關(guān)鍵字和每個數(shù)據(jù)項傳給該方法。若方法返回 true,則會在搜索結(jié)果中顯示對應(yīng)的數(shù)據(jù)項。

<template>
  <el-transfer
    v-model="value"
    filterable
    :filter-method="filterMethod"
    filter-placeholder="請輸入城市拼音"
    :data="data"
  />
</template>

<script>
  export default {
    data() {
      const generateData = (_) => {
        const data = []
        const cities = ['上海', '北京', '廣州', '深圳', '南京', '西安', '成都']
        const spell = [
          'shanghai',
          'beijing',
          'guangzhou',
          'shenzhen',
          'nanjing',
          'xian',
          'chengdu',
        ]
        cities.forEach((city, index) => {
          data.push({
            label: city,
            key: index,
            spell: spell[index],
          })
        })
        return data
      }
      return {
        data: generateData(),
        value: [],
        filterMethod(query, item) {
          return item.spell.indexOf(query) > -1
        },
      }
    },
  }
</script>

可自定義

可以對列表標題文案、按鈕文案、數(shù)據(jù)項的渲染函數(shù)、列表底部的勾選狀態(tài)文案、列表底部的內(nèi)容區(qū)等進行自定義。


可以使用 titles、button-texts、render-content 和 format 屬性分別對列表標題文案、按鈕文案、數(shù)據(jù)項的渲染函數(shù)和列表頂部的勾選狀態(tài)文案進行自定義。數(shù)據(jù)項的渲染還可以使用 scoped-slot 進行自定義。對于列表底部的內(nèi)容區(qū),提供了兩個具名 slot:left-footer 和 right-footer。此外,如果希望某些數(shù)據(jù)項在初始化時就被勾選,可以使用 left-default-checked 和 right-default-checked 屬性。最后,本例還展示了 change 事件的用法。注意:由于 jsfiddle 不支持 JSX 語法,所以使用 render-content 自定義數(shù)據(jù)項的例子在 jsfiddle 中無法運行。但是在實際的項目中,只要正確地配置了相關(guān)依賴,就可以正常運行。

<template>
  <p style="text-align: center; margin: 0 0 20px">
    使用 render-content 自定義數(shù)據(jù)項
  </p>
  <div style="text-align: center">
    <el-transfer
      v-model="leftValue"
      style="text-align: left; display: inline-block"
      filterable
      :left-default-checked="[2, 3]"
      :right-default-checked="[1]"
      :render-content="renderFunc"
      :titles="['Source', 'Target']"
      :button-texts="['到左邊', '到右邊']"
      :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
      :data="data"
      @change="handleChange"
    >
      <template #left-footer>
        <el-button class="transfer-footer" size="small">操作</el-button>
      </template>
      <template #right-footer>
        <el-button class="transfer-footer" size="small">操作</el-button>
      </template>
    </el-transfer>
  </div>
  <p style="text-align: center; margin: 50px 0 20px">
    使用 scoped-slot 自定義數(shù)據(jù)項
  </p>
  <div style="text-align: center">
    <el-transfer
      v-model="rightValue"
      style="text-align: left; display: inline-block"
      filterable
      :left-default-checked="[2, 3]"
      :right-default-checked="[1]"
      :titles="['Source', 'Target']"
      :button-texts="['到左邊', '到右邊']"
      :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
      :data="data"
      @change="handleChange"
    >
      <template #default="{option}">
        <span>{{ option.key }} - {{ option.label }}</span>
      </template>
      <template #left-footer>
        <el-button class="transfer-footer" size="small">操作</el-button>
      </template>
      <template #right-footer>
        <el-button class="transfer-footer" size="small">操作</el-button>
      </template>
    </el-transfer>
  </div>
</template>

<script>
  export default {
    data() {
      const generateData = (_) => {
        const data = []
        for (let i = 1; i <= 15; i++) {
          data.push({
            key: i,
            label: `備選項 ${i}`,
            disabled: i % 4 === 0,
          })
        }
        return data
      }
      return {
        data: generateData(),
        leftValue: [1],
        rightValue: [1],
        renderFunc(h, option) {
          return h('span', null, option.key, ' - ', option.label)
        },
      }
    },

    methods: {
      handleChange(value, direction, movedKeys) {
        console.log(value, direction, movedKeys)
      },
    },
  }
</script>
<style>
  .transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
</style>

數(shù)據(jù)項屬性別名

默認情況下,Transfer 僅能識別數(shù)據(jù)項中的 key、label 和 disabled 字段。如果你的數(shù)據(jù)的字段名不同,可以使用 props 屬性為它們設(shè)置別名。


本例中的數(shù)據(jù)源沒有 key 和 label 字段,在功能上與它們相同的字段名為 value 和 desc。因此可以使用props 屬性為 key 和 label 設(shè)置別名。

<template>
  <el-transfer
    v-model="value"
    :props="{
      key: 'value',
      label: 'desc'
    }"
    :data="data"
  />
</template>

<script>
  export default {
    data() {
      const generateData = (_) => {
        const data = []
        for (let i = 1; i <= 15; i++) {
          data.push({
            value: i,
            desc: `備選項 ${i}`,
            disabled: i % 4 === 0,
          })
        }
        return data
      }
      return {
        data: generateData(),
        value: [],
      }
    },
  }
</script>

Attributes

參數(shù)說明類型可選值默認值
model-value / v-model綁定值array
dataTransfer 的數(shù)據(jù)源array[{ key, label, disabled }][ ]
filterable是否可搜索booleanfalse
filter-placeholder搜索框占位符string請輸入搜索內(nèi)容
filter-method自定義搜索方法function
target-order右側(cè)列表元素的排序策略:若為 original,則保持與數(shù)據(jù)源相同的順序;若為 push,則新加入的元素排在最后;若為 unshift,則新加入的元素排在最前stringoriginal / push / unshiftoriginal
titles自定義列表標題array['列表 1', '列表 2']
button-texts自定義按鈕文案array[ ]
render-content自定義數(shù)據(jù)項渲染函數(shù)function(h, option)
format列表頂部勾選狀態(tài)文案object{noChecked, hasChecked}{ noChecked: '${checked}/${total}', hasChecked: '${checked}/${total}' }
props數(shù)據(jù)源的字段別名object{key, label, disabled}
left-default-checked初始狀態(tài)下左側(cè)列表的已勾選項的 key 數(shù)組array[ ]
right-default-checked初始狀態(tài)下右側(cè)列表的已勾選項的 key 數(shù)組array[ ]

Slot

name說明
left-footer左側(cè)列表底部的內(nèi)容
right-footer右側(cè)列表底部的內(nèi)容

Scoped Slot

name說明
自定義數(shù)據(jù)項的內(nèi)容,參數(shù)為 { option }

Methods

方法名說明參數(shù)
clearQuery清空某個面板的搜索關(guān)鍵詞'left' / 'right',指定需要清空的面板

Events

事件名稱說明回調(diào)參數(shù)
change右側(cè)列表元素變化時觸發(fā)當前值、數(shù)據(jù)移動的方向('left' / 'right')、發(fā)生移動的數(shù)據(jù) key 數(shù)組
left-check-change左側(cè)列表元素被用戶選中 / 取消選中時觸發(fā)當前被選中的元素的 key 數(shù)組、選中狀態(tài)發(fā)生變化的元素的 key 數(shù)組
right-check-change右側(cè)列表元素被用戶選中 / 取消選中時觸發(fā)當前被選中的元素的 key 數(shù)組、選中狀態(tài)發(fā)生變化的元素的 key 數(shù)組


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號