elementUI表格拖拽(vue项目)

dom结构

<el-table :data="fileForm.bsDeptfileFieldconfigList" id="yourId">
                        <el-table-column type="index" width="50" />
                        <el-table-column label="字段名" align="center" prop="fieldName" >
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.fieldName" placeholder="请输入文件字段名称"  />
                            </template>
                        </el-table-column>
                        <el-table-column label="字段类型" align="center" prop="fieldType" >
                            <template slot-scope="scope">
                                <el-select clearable v-model="scope.row.fieldType" class="m-2" placeholder="请选择字段类型" >
                                    <el-option v-for="item in fieldtypeList" :key="item.value" :label="item.label" :value="item.value" />
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column label="是否必填" align="center" prop="isRequired" >
                            <template slot-scope="scope">
                                <el-select clearable v-model="scope.row.isRequired" class="m-2" placeholder="请选择是否必填" >
                                    <el-option v-for="item in requiredList" :key="item.value" :label="item.label" :value="item.value" />
                                </el-select>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column label="显示排序" align="center" prop="sort" width="250">
                            <template slot-scope="scope">
                                <el-input-number v-model="scope.row.sort" controls-position="right" :min="1" :max="100"></el-input-number>
                            </template>
                        </el-table-column> -->
                        <el-table-column label="操作" align="center" class-name="table-button" width="80">
                            <template slot-scope="scope" >
                                <el-button size="mini" icon="el-icon-delete"  type="danger" plain @click="fieldDeleteFun(scope)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

引入sortablejs

import Sortable from 'sortablejs'

实现拖拽(之所以要获取yourId,是因为我在主页面也有一个表格,而我需要给弹框里面的表格加拖拽)

 const element = document.getElementById('yourId');
                const el = element.querySelector('.el-table__body-wrapper tbody')
                Sortable.create(el, {
                    disabled: false, // 是否开启拖拽
                    ghostClass: 'sortable-ghost', //拖拽样式
                    animation: 150, // 拖拽延时,效果更好看
                    group: { // 是否开启跨表拖拽
                        pull: false,
                        put: false
                    },
                    onEnd: (event) => {
                        const { oldIndex, newIndex } = event
                        this.updateRowOrder(oldIndex, newIndex)
                    }
                })

拖拽后执行的想退应操作

updateRowOrder (oldIndex, newIndex) {
            this.fileForm.bsDeptfileFieldconfigList.splice(newIndex, 0, this.fileForm.bsDeptfileFieldconfigList.splice(oldIndex, 1)[0]);
            var newArray = this.fileForm.bsDeptfileFieldconfigList.slice(0);
            this.fileForm.bsDeptfileFieldconfigList = [];
            this.$nextTick(function () {
                this.fileForm.bsDeptfileFieldconfigList = newArray;
            });
        },

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值