el-transfer增加拖拽功能
Posted xcr1234
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了el-transfer增加拖拽功能相关的知识,希望对你有一定的参考价值。
<template> <el-transfer ref="transfer" id="transfer" v-model="value" :data="data"> <span slot-scope="{ option }" :draggable="!option.disabled" @dragstart="drag($event,option)">{{ option.key }} - {{ option.label }}</span> </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], draggingKey : null } }, mounted() { const transfer = this.$refs.transfer.$el const leftPanel = transfer.getElementsByClassName("el-transfer-panel")[0].getElementsByClassName("el-transfer-panel__body")[0]; const rightPanel = transfer.getElementsByClassName("el-transfer-panel")[1].getElementsByClassName("el-transfer-panel__body")[0]; leftPanel.ondragover = (ev) => { ev.preventDefault() } leftPanel.ondrop = (ev) => { ev.preventDefault(); const index = this.value.indexOf(this.draggingKey) if(index !== -1){ this.value.splice(index,1) } } rightPanel.ondragover = (ev) => { ev.preventDefault() } rightPanel.ondrop = (ev) => { ev.preventDefault(); if(this.value.indexOf(this.draggingKey) === -1){ this.value.push(this.draggingKey) } } }, methods: { drag(ev,option) {
this.draggingKey = option.key } } } </script>
以上是关于el-transfer增加拖拽功能的主要内容,如果未能解决你的问题,请参考以下文章