首页 前端知识 vue3 Element Plus实现自定义穿梭框

vue3 Element Plus实现自定义穿梭框

2024-06-17 09:06:45 前端知识 前端哥 115 311 我要收藏

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

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
    }
};

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

转载请注明出处或者链接地址:https://www.qianduange.cn//article/12477.html
标签
评论
发布的文章

HTML5 新增元素

2024-06-22 10:06:20

【前端】HTML5基础

2024-06-22 10:06:36

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!