vue3+Element Plus实现自定义穿梭框的详细代码

 更新时间:2024年01月30日 09:26:11   作者:左小白  
找到一个好用的vue树形穿梭框组件都很难,又不想仅仅因为一个穿梭框在element-ui之外其他重量级插件,本文给大家分享vue3+Element Plus实现自定义穿梭框的示例代码,感兴趣的朋友一起看看吧

先上效果图:具体细节可以自己调整这里主要说明 派单,取消这两个按钮的实现

DOM部分代码:

    <el-row style="min-height: 7.5rem">
        <el-col :span="10" style="border: 1px solid #E1E1E1;border-radius: 5px">
            <div>
                <div style="border-radius: 5px 5px 0px 0px;
                                                                    border-bottom: 1px solid #42A4ED;display: flex;
                                                                    justify-content: space-around;background: #42A4ED">
                    <div id="rec" style="
                                 font-weight: bolder;
                                                                         cursor: pointer;/*鼠标变成手指样式*/
                                                                         transition: all 0.3s;
                                                                         color: #fff;">
                        <p style="padding: 10px 10px;height: 40px;"></p>
                    </div>
                </div>
                <div style="padding-bottom: 20px;margin: auto;
                                                                    min-height: 15%;width:80%;font-size: .2rem;
                                                                    color:#0c1a24;background-color: #fff;">
                    <el-row>
                        <el-col :span="20" style="margin-left: 40px; margin-top: 10px;">
                            <el-input :style="{ margin: 'auto', height: '30px'}" v-model.trim="formData.orgName"
                                v-show="flag == 'REC'" placeholder="请输入组织名称">
                                <template #append>
                                    <img @click="getUserData()" src="@/assets/images/icon/search.png"
                                        style="position:relative;top:0px;right: 0px;cursor: pointer">
                                </template>
                            </el-input>
                        </el-col>
                    </el-row>
                </div>
                <el-table v-loading="flagTbale" ref="selectedLeft"  align='center' :row-style="{ height: '30px', padding: '0 0' }"
                    highlight-current-row :data="tableData" @selection-change="handleLeftSelectionChange"
                    :cell-style="{ padding: '3px', background: '#fff', paddingLeft: '0', color: '#666' }"
                    :header-cell-style="{ background: '#42A4ED', color: '#FFFFFF', height: '20px', fontSize: '0.2rem', fontWeight: 'bold' }"
                    :header-row-style="{ padding: '10px' }">
                    <el-table-column type="selection" width="50" prop="select"></el-table-column>
                    <el-table-column :show-overflow-tooltip="true" align="center" v-for="col in cols" :key="col.id"
                        :label="col.label" :prop="col.prop" :width="col.width">
                    </el-table-column>
                </el-table>
                <div style="height: 1rem;margin-bottom:0.2rem;display: flex; justify-content: center;">
                    <el-pagination class="pagination"  small layout="prev, pager, next" @size-change="handleSizeChange"
                        :pager-count="3" @current-change="handleCurrentChange" v-model:current-page="pageNum"
                        v-model:page-size="pageSize" :total="total"  />
                </div>
            </div>
        </el-col>
        <!-- 中间按钮 -->
        <!--中间按钮开始-->
        <el-col :span="2" class="layui-transfer-active" style="margin: 230px 3% 0 5%;">
            <div>
                <el-button class="setting-btn" style="width: 70%;" @click="buttonRight" type="primary"
                    :disabled="fxDisabled">
                    派单
                </el-button>
            </div>
            <div>
                <el-button class="setting-btn mt20" style="width: 70%;" @click="buttonLeft" type="primary"
                    :disabled="qxfxDisabled">取消
                </el-button>
            </div>
        </el-col>
        <el-col :span="10" style="border: 1px solid #E1E1E1;border-radius: 5px">
            <div>
                <div style="border-radius: 5px 5px 0px 0px;
                                                            border-bottom: 0px solid #42A4ED;display: flex;
                                                            justify-content: space-around;background: #42A4ED">
                    <div id="reced" style="font-family: SourceHanSansCN-Medium;
                                                                 font-weight: bolder;
                                                                 cursor: pointer;/*鼠标变成手指样式*/
                                                                 transition: all 0.3s;
                                                                 color: #fff;">
                        <p style="padding: 1px 0px;height: 4px;"></p>
                    </div>
                </div>
                <el-table ref="selectedRight"  align='center' max-height="470px" :row-style="{ height: '30px', padding: '0 0' }"
                    highlight-current-row :data="sharedData" @selection-change="handleRightSelectionChange"
                    :cell-style="{ padding: '3px', background: '#fff', paddingLeft: '0', color: '#666' }"
                    :header-cell-style="{ background: '#42A4ED', color: '#FFFFFF', height: '20px', fontSize: '0.2rem', fontWeight: 'bold' }"
                    :header-row-style="{ padding: '10px' }"
                    >
                    <el-table-column type="selection" width="55" prop="select"></el-table-column>
                    <el-table-column :show-overflow-tooltip="true" align="center" v-for="col in cols" :key="col.id"
                        :label="col.label" :prop="col.prop" :width="col.width">
                    </el-table-column>
                </el-table>
            </div>
        </el-col>
    </el-row>

穿梭框的table表头是动态的html中无需修改,使用这个组件首先需要在scripe中创建如下变量

const tableData = ref([]) //左侧数据
const selectedLeft = ref([])// 左侧选中数据
const sharedData = ref([])//右侧数据
const selectedRight = ref([])// 右侧选中数据
//表格loading
const flagTbale = ref(false) //可以去除所有的flagTbale 变量即可去除loading功能
//两个table表头
const cols = ref([
    { id: 1, prop: 'latnName', label: '本地网' },
    { id: 2, prop: 'orgId', label: '组织编码' },
    { id: 3, prop: 'orgName', label: '组织' },
])

然后就是最重要的按钮功能

// 取消按钮
const buttonLeft = async () => {
    if (!selectedRight.value || !Array.isArray(selectedRight.value)) {
        selectedRight.value = [];
    }
    // 将右侧选中的数据移动到左侧
    tableData.value = tableData.value.concat(selectedRight.value);
    // 从右侧数据中移除已经移动的数据
    sharedData.value = sharedData.value.filter(item => !selectedRight.value.includes(item));
    // 清空右侧选中的数据
    selectedRight.value = [];
}
// 分享按钮
const buttonRight = async () => {
    if (!selectedLeft.value || !Array.isArray(selectedLeft.value)) {
        selectedLeft.value = [];
    }
    // 将左侧选中的数据移动到右侧
    sharedData.value = sharedData.value.concat(selectedLeft.value);
    // 从左侧数据中移除已经移动的数据
    tableData.value = tableData.value.filter(item => !selectedLeft.value.includes(item));
    // 清空左侧选中的数据
    selectedLeft.value = [];
}

按钮禁用逻辑实现

// 按钮是否禁用
const qxfxDisabled = ref(true)
const fxDisabled = ref(true)
// 左侧表格选中事件
const handleLeftSelectionChange = (selection) => {
    if (selection.length !== 0) {
        fxDisabled.value = false
    }
    if (selection.length === 0) {
        fxDisabled.value = true
    }
};
// 右侧表格选中事件
const handleRightSelectionChange = (selection) => {
    selectedRight.value = selection;
    if (selection.length !== 0) {
        qxfxDisabled.value = false
    }
    if (selection.length === 0) {
        qxfxDisabled.value = true
    }
};

到这里你就可以使用在这个组件了。这是经过插分的代码原本还拥有表头切换的逻辑如有需要可留言.我后续可以在做整理

到此这篇关于vue3+Element Plus实现自定义穿梭框的文章就介绍到这了,更多相关vue3 Element Plus穿梭框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue element 生成无线级左侧菜单的实现代码

    vue element 生成无线级左侧菜单的实现代码

    这篇文章主要介绍了vue element 生成无线级左侧菜单的实现代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • vue新vue-cli3环境配置和模拟json数据的实例

    vue新vue-cli3环境配置和模拟json数据的实例

    今天小编就为大家分享一篇vue新vue-cli3环境配置和模拟json数据的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • vue-cli4.x创建企业级项目的方法步骤

    vue-cli4.x创建企业级项目的方法步骤

    这篇文章主要介绍了vue-cli4.x创建企业级项目的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • vue中使用protobuf的过程记录

    vue中使用protobuf的过程记录

    由于目前公司采用了ProtoBuf做前后端数据交互,进公司以来一直用的是公司大神写好的基础库,完全不了解底层是如何解析的。下面小编给大家分享vue中使用protobuf的过程记录,需要的朋友参考下吧
    2018-10-10
  • vue实现计算器功能

    vue实现计算器功能

    这篇文章主要为大家详细介绍了vue实现计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Vue项目接入Paypal实现示例详解

    Vue项目接入Paypal实现示例详解

    这篇文章主要介绍了Vue项目接入Paypal实现示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 深入理解vue.js中的v-if和v-show

    深入理解vue.js中的v-if和v-show

    这篇文章主要给大家深入的介绍了关于vue.js中v-if和v-show的相关资料,文中详细介绍两者的共同点和区别,通过图文介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-06-06
  • 解决elementui中NavMenu导航菜单高亮问题(解决多种情况)

    解决elementui中NavMenu导航菜单高亮问题(解决多种情况)

    这篇文章主要介绍了解决elementui中NavMenu 导航菜单高亮问题(解决多种情况),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • vue使用拖拽方式创建结构树

    vue使用拖拽方式创建结构树

    这篇文章主要为大家详细介绍了vue使用拖拽方式创建结构树,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Vue全局共享数据之globalData,vuex,本地存储的使用

    Vue全局共享数据之globalData,vuex,本地存储的使用

    这篇文章主要介绍了Vue全局共享数据之globalData,vuex,本地存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10

最新评论