作品效果
喜欢的下拉别忘记给作者点赞❤
前言
工作中有这样的需求:用户要在表格中输入某步骤的方法且要求此表格可以满足步骤顺序的移动(也就是拖拽功能) 还要求能增加步骤的条数当然也会有删除,那么今天我将给大家用最简单的写法实现一个实用的小demo来实现这些需求!
一、sortablejs 是什么?
Sortable.js是一款优秀的js拖拽库,支持ie9及以上版本ie浏览器和现代浏览器,也可以运行在移动触摸设备中。不依赖jQuery。支持 Meteor、AngularJS、React、Vue、Knockout框架和任何CSS库,如Bootstrap、Element UI。你可以用来拖拽div、table等元素。
二、使用步骤
1.介绍
官方文档:
GitHub - SortableJS/Sortable: Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required. - GitHub - SortableJS/Sortable: Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.https://github.com/SortableJS/Sortable
复制
2.安装
npm安装方式:
npm install sortablejs --save复制
其他安装方式见官方文档
3.使用
1.第一步在组件内引入sortablejs
import Sortable from "sortablejs";
2.vue el-table 的html;
<el-table :data="tableData" border fit class="tabledeg" row-key="id"> <el-table-column label="拖拽排序" width="50" align="center"> <div class="move"> <i class="el-icon-rank" style="fonsize:20px;margin:18px"></i> </div> </el-table-column> <el-table-column label="编号" width="50" align="center" type="index"> </el-table-column> <el-table-column label="步骤描述" width="170" align="center" prop="dectext" > <template slot-scope="scope"> <el-input type="textarea" @change='aaaa(scope)' style="padding:0" :autosize="{ minRows: 2.7, maxRows: 40}" placeholder="未填写" v-model="tableData[scope.$index].dectext" ></el-input> </template> </el-table-column> <el-table-column label="预期结果" width="170" align="center" prop="resulttext"> <template slot-scope="scope"> <el-input type="textarea" @change='aaaa(scope)' style="padding:0" :autosize="{ minRows: 2.7, maxRows: 40}" placeholder="未填写" v-model="tableData[scope.$index].resulttext" ></el-input> </template> </el-table-column> <el-table-column label="操作" align="center" prop="caozuo"> <template slot-scope="scope"> <span class="colors" @click="del(scope.row.id)"> <i class="el-icon-delete"></i>删除 </span> </template> </el-table-column> </el-table> <i class="el-icon-circle-plus-outline" @click="additem"></i>
复制
3.vue css
/deep/ .el-table .cell{ padding: 0 ; } /deep/ .el-table .el-table__cell{ padding: 0; } /deep/ .el-table--border .el-table__cell:first-child .cell { padding-left: 0; } /deep/ .el-textarea__inner{ padding: 0 4px; } .move{ cursor: pointer; } .colors{ cursor: pointer; } /deep/.el-table, .el-table__expanded-cell { background-color: rgba(240, 10, 10,.2); } /deep/.el-table th, .el-table tr { background-color: rgba(64, 158, 255,.6); color: #F5F5F5; } /deep/.el-table__body tr:hover > td{ background-color: rgba(64, 158, 255,.3) !important; color: rgb(255, 0, 0); font-size: 20px; } .oneformitem{ position: relative; } .el-icon-circle-plus-outline{ font-size:20px; color:rgb(64, 158, 255); position: absolute; left: -22px; bottom: 0; }
复制
4.vue javascript
data() { return { tableData: [ { id: '1', caozuo:'删除', dectext:'', resulttext:'' }, { id: '2', caozuo:'删除', dectext:'', resulttext:'' }, { id: '3', caozuo:'删除', dectext:'', resulttext:'' }, { id: '4', caozuo:'删除', dectext:'', resulttext:'' } ], } }
复制
//生命周期 - 挂载完成(访问DOM元素) mounted() { this.rowDrop() }, methods:{ //行拖拽 rowDrop() { const tbody = document.querySelector('.el-table__body-wrapper tbody') const _this = this Sortable.create(tbody, { animation: 500, direction: 'vertical', handle:".move", onEnd({ newIndex, oldIndex }) { // 三个参数 插入-向数组指定位置插入任意项元素 三个参数,第一个参数(起始位 //置),第 //二个参数(0),第三个参数(插入的项数) _this.tableData.splice(newIndex,0,_this.tableData.splice(oldIndex, 1)[0]) // console.log(_this.tableData); //高手代码里看到.slice(0),查了下这样写的好处: // 1.对原数组进行深拷贝,这样进行一系列操作的时候就不影响原数组了; // 2.将类数组对象转化为真正的数组对象 //序号重置 var newArray = _this.tableData.slice(0) _this.tableData = [] _this.$nextTick(function () { _this.tableData = newArray }) } }) }, //拖拽删除 del(e){ this.tableData.forEach((item,index)=>{ if(item.dectext || item.resulttext){ return this.$message({type:'warning',message:'仅能删除空步骤行!'}); } if(item.id == e){ this.tableData.splice(index,1); this.$message({type:'success',message:'删除成功!'}); } //重新排序 this.tableData.forEach((item,index)=>{ item['id'] = index+1; }); }) }, //添加table行 additem(){ this.tableData.push({ id: this.tableData.length+1, caozuo:'删除', dectext:'', resulttext:'' }); this.$message({type:'success',message:'步骤添加成功!'}); } }
复制
总结:
以上就是今天要讲的内容,本文仅仅简单介绍了sortablejs的使用而Sortable.js是一款优秀的js拖拽库,支持ie9及以上版本ie浏览器和现代浏览器,也可以运行在移动触摸设备中。不依赖jQuery。支持 Meteor、AngularJS、React、Vue、Knockout框架和任何CSS库,如Bootstrap、Element UI。你可以用来拖拽div、table等元素。
结尾:有问题请及时评论或联系作者我们一起探讨 让技术延续!!