vue 使用elementUI表格动态行合并
需求背景
在开发中又是表格合并的行数并不是固定的行数,需要根据接口返回的数据来动态合并需要合并的行数
html代码:
<el-table :data="tableData" border :span-method="objectSpanMethod" > <el-table-column label="前面" align="center"> <el-table-column prop="inquiryCode" label="id" align="center" /> <el-table-column prop="phoneNumber" label="1" align="center" /> <el-table-column prop="createTime" label="2" align="center" /> <el-table-column prop="createTime" label="3" align="center" /> <el-table-column prop="contractCount" label="4" align="center" /> </el-table-column> <el-table-column label="中间" align="center"> <el-table-column prop="bankName" label="6" align="center" /> <el-table-column prop="acceptanceAmount" label="7" align="center" /> <el-table-column prop="dueTime" label="8" align="center" /> <el-table-column prop="dueDay" label="9" align="center" /> <el-table-column prop="interestAmount" label="10" align="center" /> </el-table-column> <el-table-column label="总计" align="center"> <el-table-column prop="cashPayAmount" label="11" align="center" /> <el-table-column prop="acceptanceAllAmount" label="12" align="center" /> <el-table-column prop="payAllAmount" label="13" align="center" /> <el-table-column prop="interestAllAmount" label="14" align="center" /> </el-table-column> </el-table>
js代码:(注意:此处我是根据接口返回的inquiryCode是否相同来确实是否需要合并,你如果是其他字段自行更改)
export default { data() { return { tableData: [], spanArr: [], } }, mounted() { this.rowspan(); }, methods: { rowspan() { // 每次调用清空数据 this.spanArr = [] this.position = 0 this.tableData.forEach((item, index) => { if (index === 0) { this.spanArr.push(1) this.position = 0 } else { // inquiryCode 为需要合并查询的项 if (this.tableData[index].inquiryCode === this.tableData[index - 1].inquiryCode) { this.spanArr[this.position] += 1 this.spanArr.push(0) } else { this.spanArr.push(1) this.position = index } } }) }, objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2 || columnIndex === 3 || columnIndex === 4 || columnIndex === 10 || columnIndex === 11 || columnIndex === 12 || columnIndex === 13) { const _row = this.spanArr[rowIndex] const _col = _row > 0 ? 1 : 0 return { rowspan: _row, colspan: _col } } }, } }
效果截图:
vue elementUI动态删除表格当前行内容
具体步骤
第一步:拿到当前行数据的索引
第二部:绑定函数置删除键将拿到的数据索引通过请求发送给后端
第三部,调用获取数据借口(这样就不用删除数据后还要再次刷新页面,用户体验会更好)
实现方法
首先获取将要被删除行的数据索引
<el-card class="box-card"> <el-table :data="tableData" border stripe style="width: 100% " @selection-change="handleSelectionChange" > <el-table-column type="selection" width="55" align="center"></el-table-column> <el-table-column prop="id" label="学号" align="center"></el-table-column> <el-table-column prop="name" label="姓名" align="center"></el-table-column> <el-table-column prop="sex" label="性别" align="center"></el-table-column> <el-table-column prop="college" label="学院" align="center"></el-table-column> <el-table-column label="操作" align="center"> <template slot-scope="scope" class="active"> <el-button @click="editData(scope.row)" type="primary" icon="el-icon-edit" circle></el-button> <el-button @click="removeData(scope.row.id)" type="danger" icon="el-icon-delete" circle></el-button> </template> </el-table-column> </el-table> <el-row :gutter="20"> <el-col :span="6" :offset="12"> <div class="block"> <el-pagination background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="pageSize" layout="total, prev, next, jumper, pager" :total="total" ></el-pagination> </div> </el-col> </el-row> </el-card>
在这一步中,获取当行数据使用的方法是 scope.row.id 。这将把当行数据的id传递给删除函数
接下来就是删除函数的逻辑实现了
在这一步中需要注意的是,请求成功后应该调用一次获取数据列表的函数,否则需要刷行页面才看得到删除的效果
removeData(id) { //提示框,判断用户是否操作失误 this.$confirm("此操作将永久删除此学生, 是否继续?", "警告", { confirmButtonText: "继续", cancelButtonText: "取消", type: "warning" }) .then(() => { const data = { id }; //这里<=>data = {id:id} axios .post("URL", data) .then(response => { this.fetchdata(); //删除数据后重新获取数据 }) .catch(() => { this.$message({ type:"warning", message:"请求失败,请检查网络设置" }) }); }) .catch(() => { this.$message({ type: "info", message: "已取消删除" }); }); }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持阿兔在线工具。