Transfer穿梭框

Transfer 穿梭框

双栏穿梭选择框。

何时使用

  • 需要在多个可选项中进行多选时。

  • 比起 Sel​ect 和 TreeSelect,穿梭框占据更大的空间,可以展示可选项的更多信息。

代码演示

基本用法

最基本的用法,展示了 dataSource、targetKeys、每行的渲染函数 render 以及回调函数 change selectChange scroll 的用法。

<​template>  <​div>    <​a-transfer      :data-source="mockData"      :titles="['Source', 'Target']"      :target-keys="targetKeys"      :selected-keys="selectedKeys"      :render="item => item.title"      :disabled="disabled"      @change="handleChange"      @selectChange="handleSelectChange"      @scroll="handleScroll"    />    <​a-switch      un-checked-children="enabled"      checked-children="disabled"      v-model:checked="disabled"      style="margin-top: 16px"    />  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';interface MockData {  key: string;  title: string;  description: string;  disabled: boolean;}const mockData: MockData[] = [];for (let i = 0; i < 20; i++) {  mockData.push({    key: i.toString(),    title: `content${i + 1}`,    description: `description of content${i + 1}`,    disabled: i % 3 < 1,  });}const oriTargetKeys = mockData.filter(item => +item.key % 3 > 1).map(item => item.key);export default defineComponent({  data() {    const disabled = ref<​boolean>(false);    const targetKeys = ref<​string[]>(oriTargetKeys);    const selectedKeys = ref<​string[]>(['1', '4']);    const handleChange = (nextTargetKeys: string[], direction: string, moveKeys: string[]) => {      targetKeys.value = nextTargetKeys;      console.log('targetKeys: ', nextTargetKeys);      console.log('direction: ', direction);      console.log('moveKeys: ', moveKeys);    };    const handleSelectChange = (sourceSelectedKeys: string[], targetSelectedKeys: string[]) => {      selectedKeys.value = [...sourceSelectedKeys, ...targetSelectedKeys];      console.log('sourceSelectedKeys: ', sourceSelectedKeys);      console.log('targetSelectedKeys: ', targetSelectedKeys);    };    const handleScroll = (direction: string, e: Event) => {      console.log('direction:', direction);      console.log('target:', e.target);    };    return {      mockData,      targetKeys,      selectedKeys,      disabled,      handleChange,      handleSelectChange,      handleScroll,    };  },});<​/script>

带搜索框

带搜索框的穿梭框,可以自定义搜索函数。

<​template>  <​a-transfer    :data-source="mockData"    show-search    :filter-option="filterOption"    :target-keys="targetKeys"    :render="item => item.title"    @change="handleChange"    @search="handleSearch"  /><​/template><​script lang="ts">import { defineComponent, onMounted, ref } from 'vue';interface MockData {  key: string;  title: string;  description: string;  chosen: boolean;}export default defineComponent({  setup() {    const mockData = ref<​MockData[]>([]);    const targetKeys = ref<​string[]>([]);    onMounted(() => {      getMock();    });    const getMock = () => {      const keys = [];      const mData = [];      for (let i = 0; i < 20; i++) {        const data = {          key: i.toString(),          title: `content${i + 1}`,          description: `description of content${i + 1}`,          chosen: Math.random() * 2 > 1,        };        if (data.chosen) {          keys.push(data.key);        }        mData.push(data);      }      mockData.value = mData;      targetKeys.value = keys;    };    const filterOption = (inputValue: string, option: MockData) => {      return option.description.indexOf(inputValue) > -1;    };    const handleChange = (keys: string[], direction: string, moveKeys: string[]) => {      console.log(keys, direction, moveKeys);      targetKeys.value = keys;    };    const handleSearch = (dir: string, value: string) => {      console.log('search:', dir, value);    };    return {      mockData,      targetKeys,      filterOption,      handleChange,      handleSearch,    };  },});<​/script>

高级用法

穿梭框高级用法,可配置操作文案,可定制宽高,可对底部进行自定义渲染。

<​template>  <​a-transfer    :data-source="mockData"    show-search    :list-style="{      width: '250px',      height: '300px',    }"    :operations="['to right', 'to left']"    :target-keys="targetKeys"    :render="item => `${item.title}-${item.description}`"    @change="handleChange"  >    <​template #footer>      <​a-button size="small" style="float: right; margin: 5px" @click="getMock">reload<​/a-button>    <​/template>    <​template #notFoundContent>      <​span>没数据<​/span>    <​/template>  <​/a-transfer><​/template><​script lang="ts">import { defineComponent, ref, onMounted } from 'vue';interface MockData {  key: string;  title: string;  description: string;  chosen: boolean;}export default defineComponent({  setup() {    const mockData = ref<​MockData[]>([]);    const targetKeys = ref<​string[]>([]);    onMounted(() => {      getMock();    });    const getMock = () => {      const keys = [];      const mData = [];      for (let i = 0; i < 20; i++) {        const data = {          key: i.toString(),          title: `content${i + 1}`,          description: `description of content${i + 1}`,          chosen: Math.random() * 2 > 1,        };        if (data.chosen) {          keys.push(data.key);        }        mData.push(data);      }      mockData.value = mData;      targetKeys.value = keys;    };    const handleChange = (keys: string[], direction: string, moveKeys: string[]) => {      targetKeys.value = keys;      console.log(keys, direction, moveKeys);    };    return {      mockData,      targetKeys,      handleChange,      getMock,    };  },});<​/script>

自定义渲染行数据

自定义渲染每一个 Transfer Item,可用于渲染复杂数据。

<​template>  <​a-transfer    :data-source="mockData"    :list-style="{      width: '300px',      height: '300px',    }"    :target-keys="targetKeys"    @change="handleChange"  >    <​template #render="item">      <​span class="custom-item" style="color: red">{{ item.title }} - {{ item.description }}<​/span>    <​/template>  <​/a-transfer><​/template><​script lang="ts">import { defineComponent, ref, onMounted } from 'vue';interface MockData {  key: string;  title: string;  description: string;  chosen: boolean;}export default defineComponent({  setup() {    const mockData = ref<​MockData[]>([]);    const targetKeys = ref<​string[]>([]);    onMounted(() => {      getMock();    });    const getMock = () => {      const keys = [];      const mData = [];      for (let i = 0; i < 20; i++) {        const data = {          key: i.toString(),          title: `content${i + 1}`,          description: `description of content${i + 1}`,          chosen: Math.random() * 2 > 1,        };        if (data.chosen) {          keys.push(data.key);        }        mData.push(data);      }      mockData.value = mData;      targetKeys.value = keys;    };    const handleChange = (keys: string[], direction: string, moveKeys: string[]) => {      targetKeys.value = keys;      console.log(keys, direction, moveKeys);    };    return {      mockData,      targetKeys,      handleChange,      getMock,    };  },});<​/script>

表格穿梭框

使用 Table 组件作为自定义渲染列表。