vue中如何使用elementUI表格動態(tài)行合并
vue 使用elementUI表格動態(tài)行合并
需求背景
在開發(fā)中又是表格合并的行數(shù)并不是固定的行數(shù),需要根據(jù)接口返回的數(shù)據(jù)來動態(tài)合并需要合并的行數(shù)
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代碼:(注意:此處我是根據(jù)接口返回的inquiryCode是否相同來確實是否需要合并,你如果是其他字段自行更改)
export default { data() { return { tableData: [], spanArr: [], } }, mounted() { this.rowspan(); }, methods: { rowspan() { // 每次調(diào)用清空數(shù)據(jù) 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動態(tài)刪除表格當前行內(nèi)容
具體步驟
第一步:拿到當前行數(shù)據(jù)的索引
第二部:綁定函數(shù)置刪除鍵將拿到的數(shù)據(jù)索引通過請求發(fā)送給后端
第三部,調(diào)用獲取數(shù)據(jù)借口(這樣就不用刪除數(shù)據(jù)后還要再次刷新頁面,用戶體驗會更好)
實現(xiàn)方法
首先獲取將要被刪除行的數(shù)據(jù)索引
<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>
在這一步中,獲取當行數(shù)據(jù)使用的方法是 scope.row.id 。這將把當行數(shù)據(jù)的id傳遞給刪除函數(shù)
接下來就是刪除函數(shù)的邏輯實現(xiàn)了
在這一步中需要注意的是,請求成功后應(yīng)該調(diào)用一次獲取數(shù)據(jù)列表的函數(shù),否則需要刷行頁面才看得到刪除的效果
removeData(id) { //提示框,判斷用戶是否操作失誤 this.$confirm("此操作將永久刪除此學生, 是否繼續(xù)?", "警告", { confirmButtonText: "繼續(xù)", cancelButtonText: "取消", type: "warning" }) .then(() => { const data = { id }; //這里<=>data = {id:id} axios .post("URL", data) .then(response => { this.fetchdata(); //刪除數(shù)據(jù)后重新獲取數(shù)據(jù) }) .catch(() => { this.$message({ type:"warning", message:"請求失敗,請檢查網(wǎng)絡(luò)設(shè)置" }) }); }) .catch(() => { this.$message({ type: "info", message: "已取消刪除" }); }); }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vuejs使用addEventListener的事件如何觸發(fā)執(zhí)行函數(shù)的this
這篇文章主要介紹了Vuejs使用addEventListener的事件觸發(fā)執(zhí)行函數(shù)的this方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06vue scroll滾動判斷的實現(xiàn)(是否滾動到底部、滾動方向、滾動節(jié)流、獲取滾動區(qū)域dom元素)
這篇文章主要介紹了vue scroll滾動判斷的實現(xiàn)(是否滾動到底部、滾動方向、滾動節(jié)流、獲取滾動區(qū)域dom元素),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06詳解Vue的computed(計算屬性)使用實例之TodoList
本篇文章主要介紹了詳解Vue的computed(計算屬性)使用實例之TodoList,具有一定的參考價值,有興趣的可以了解一下2017-08-08