關于el-table表格組件中插槽scope.row的使用方式
更新時間:2022年08月13日 15:12:05 作者:小檸檬愛編程
這篇文章主要介紹了關于el-table表格組件中插槽scope.row的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
el-table表格組件中插槽scope.row使用
要實現點擊查看顯示后端返回的字段并以文字渲染到頁面上,就要是使用到插槽
下圖是要實現的
<el-table-column label="任職要求" width="100" align="center"> <template slot-scope="scope"> <el-popover placement="bottom" width="300" trigger="click"> <div> <div class="line">任職要求</div> <div class="heighth"> 工作年限:<span>{{ scope.row.worked_year }}</span> </div> //給學歷定義一個edutype方法,通過scope.row傳參 <div class="heighth"> 學歷:<span>{{ edutype(scope.row.education) }}</span> </div> <div class="heighth"> 專業(yè):<span>{{ scope.row.major }}</span> </div> <div class="heighth"> 技能及經驗:<span>{{ scope.row.experience_skills }}</span> </div> </div> <el-button slot="reference" type="text">查看</el-button> </el-popover> </template> </el-table-column>
methods: { //通過row接受參數 edutype(row) { // console.log(row); if (row == "primary school") { return "小學"; } if (row == "junior high school") { return "初中"; } if (row == "senior high school") { return "高中"; } if (row == "technical secondary school") { return "中專"; } if (row == "junior college") { return "大專"; } if (row == "undergraduate") { return "本科"; } if (row == "graduate student") { return "研究生"; } if (row == "unlimited") { return "不限"; } } }
這樣就實現啦。。。。。
slot-scope和scope.row的用法
實現效果
根據后端傳來的mg_state的bool型數據來渲染開關狀態(tài),當為true時,開關打開;為false時關閉
解決
狀態(tài)開關屬于單元格,也屬于一行,如果我們拿到這一行的數據,就可以.mg_state具體值,則可以按需渲染效果。所以想到用作用域插槽來渲染狀態(tài)這一列
<el-table :data="userlist" border stripe> <el-table-column type="index"></el-table-column> <el-table-column label="姓名" prop="username"></el-table-column> <el-table-column label="郵箱" prop="email"></el-table-column> <el-table-column label="電話" prop="mobile"></el-table-column> <el-table-column label="角色" prop="role_name"></el-table-column> <el-table-column label="狀態(tài)" > <template slot-scope="scope"> <el-switch v-model="scope.row.mg_state"></el-switch> </template> </el-table-column> <el-table-column label="操作"> </el-table-column> </el-table>
- data=“userList”
- 表格綁定了用于存儲數據的數組,里面每一個元素都是數據對象
- 首先在狀態(tài)這一列中定義了一個作用域插槽
- 通過slot-scope="scope"來接收作用域插槽的數據(添加屬性slot-scope,并且定義對象scope)
- scope.row
- scope有一個屬性row(ElementUI文檔),scope.row可以拿到對應行的數據
- v-model=“scope.row.mg_state”
- 需要把這個開關的狀態(tài)綁定到scope.row.mg_state屬性上
ElementUI文檔
userList數據如下:
效果
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在Vue的mounted中仍然加載渲染不出echarts的方法問題
這篇文章主要介紹了在Vue的mounted中仍然加載渲染不出echarts的方法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決
這篇文章主要介紹了Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue3+ts+elementui-plus二次封裝彈框實戰(zhàn)教程
這篇文章主要介紹了vue3+ts+elementui-plus二次封裝彈框實戰(zhàn)教程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07