组件-Element—Transfer(穿梭框)

组件-Element—Transfer(穿梭框)

组件—穿梭框

  1. 基础用法
    组件-Element—Transfer(穿梭框)

    <template>
      <el-transfer v-model="value" :data="data"></el-transfer>
    </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>
    
  2. 可搜索
    组件-Element—Transfer(穿梭框)

    <template>
      <el-transfer
        filterable
        :filter-method="filterMethod"
        filter-placeholder="请输入城市拼音"
        v-model="value"
        :data="data">
      </el-transfer>
    </template>
    
    <script>
      export default {
        data() {
          const generateData = _ => {
            const data = [];
            const cities = ['上海', '北京', '广州', '深圳', '南京', '西安', '成都'];
            const pinyin = ['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'nanjing', 'xian', 'chengdu'];
            cities.forEach((city, index) => {
              data.push({
                label: city,
                key: index,
                pinyin: pinyin[index]
              });
            });
            return data;
          };
          return {
            data: generateData(),
            value: [],
            filterMethod(query, item) {
              return item.pinyin.indexOf(query) > -1;
            }
          };
        }
      };
    </script>
    
  3. 可自定义
    组件-Element—Transfer(穿梭框)
    组件-Element—Transfer(穿梭框)

    <template>
      <p style="text-align: center; margin: 0 0 20px">使用 render-content 自定义数据项</p>
      <div style="text-align: center">
        <el-transfer
          style="text-align: left; display: inline-block"
          v-model="value"
          filterable
          :left-default-checked="[2, 3]"
          :right-default-checked="[1]"
          :render-content="renderFunc"
          :titles="['Source', 'Target']"
          :button-texts="['到左边', '到右边']"
          :format="{
            noChecked: '${total}',
            hasChecked: '${checked}/${total}'
          }"
          @change="handleChange"
          :data="data">
          <el-button class="transfer-footer" slot="left-footer" size="small">操作</el-button>
          <el-button class="transfer-footer" slot="right-footer" size="small">操作</el-button>
        </el-transfer>
      </div>
      <p style="text-align: center; margin: 50px 0 20px">使用 scoped-slot 自定义数据项</p>
      <div style="text-align: center">
        <el-transfer
          style="text-align: left; display: inline-block"
          v-model="value4"
          filterable
          :left-default-checked="[2, 3]"
          :right-default-checked="[1]"
          :titles="['Source', 'Target']"
          :button-texts="['到左边', '到右边']"
          :format="{
            noChecked: '${total}',
            hasChecked: '${checked}/${total}'
          }"
          @change="handleChange"
          :data="data">
          <span slot-scope="{ option }">{{ option.key }} - {{ option.label }}</span>
          <el-button class="transfer-footer" slot="left-footer" size="small">操作</el-button>
          <el-button class="transfer-footer" slot="right-footer" size="small">操作</el-button>
        </el-transfer>
      </div>
    </template>
    
    <style>
      .transfer-footer {
        margin-left: 20px;
        padding: 6px 5px;
      }
    </style>
    
    <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],
            value4: [1],
            renderFunc(h, option) {
              return <span>{ option.key } - { option.label }</span>;
            }
          };
        },
    
        methods: {
          handleChange(value, direction, movedKeys) {
            console.log(value, direction, movedKeys);
          }
        }
      };
    </script>
    
  4. 数据项属性别名
    组件-Element—Transfer(穿梭框)

    <template>
      <el-transfer
        v-model="value"
        :props="{
          key: 'value',
          label: 'desc'
        }"
        :data="data">
      </el-transfer>
    </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>
    
  5. Attributes
    组件-Element—Transfer(穿梭框)
    组件-Element—Transfer(穿梭框)

  6. Slot
    组件-Element—Transfer(穿梭框)

  7. Scoped Slot
    组件-Element—Transfer(穿梭框)

  8. Methods
    组件-Element—Transfer(穿梭框)

  9. Events
    组件-Element—Transfer(穿梭框)

上一篇:Richedit使用大全


下一篇:PTA——念数字