關(guān)于el-table表格組件中插槽scope.row的使用方式
el-table表格組件中插槽scope.row使用
要實(shí)現(xiàn)點(diǎn)擊查看顯示后端返回的字段并以文字渲染到頁面上,就要是使用到插槽
下圖是要實(shí)現(xiàn)的

<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>
//給學(xué)歷定義一個edutype方法,通過scope.row傳參
<div class="heighth">
學(xué)歷:<span>{{ edutype(scope.row.education) }}</span>
</div>
<div class="heighth">
專業(yè):<span>{{ scope.row.major }}</span>
</div>
<div class="heighth">
技能及經(jīng)驗(yàn):<span>{{ scope.row.experience_skills }}</span>
</div>
</div>
<el-button slot="reference" type="text">查看</el-button>
</el-popover>
</template>
</el-table-column>
methods: {
//通過row接受參數(shù)
edutype(row) {
// console.log(row);
if (row == "primary school") {
return "小學(xué)";
}
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 "不限";
}
}
}
這樣就實(shí)現(xiàn)啦。。。。。
slot-scope和scope.row的用法
實(shí)現(xiàn)效果
根據(jù)后端傳來的mg_state的bool型數(shù)據(jù)來渲染開關(guān)狀態(tài),當(dāng)為true時,開關(guān)打開;為false時關(guān)閉
解決
狀態(tài)開關(guān)屬于單元格,也屬于一行,如果我們拿到這一行的數(shù)據(jù),就可以.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”
- 表格綁定了用于存儲數(shù)據(jù)的數(shù)組,里面每一個元素都是數(shù)據(jù)對象
- 首先在狀態(tài)這一列中定義了一個作用域插槽
- 通過slot-scope="scope"來接收作用域插槽的數(shù)據(jù)(添加屬性slot-scope,并且定義對象scope)
- scope.row
- scope有一個屬性row(ElementUI文檔),scope.row可以拿到對應(yīng)行的數(shù)據(jù)
- v-model=“scope.row.mg_state”
- 需要把這個開關(guān)的狀態(tài)綁定到scope.row.mg_state屬性上
ElementUI文檔

userList數(shù)據(jù)如下:

效果

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-i18n結(jié)合Element-ui的配置方法
這篇文章主要介紹了vue-i18n結(jié)合Element-ui的配置方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的方法
這篇文章主要介紹了Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
在Vue的mounted中仍然加載渲染不出echarts的方法問題
這篇文章主要介紹了在Vue的mounted中仍然加載渲染不出echarts的方法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決
這篇文章主要介紹了Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3+ts+elementui-plus二次封裝彈框?qū)崙?zhàn)教程
這篇文章主要介紹了vue3+ts+elementui-plus二次封裝彈框?qū)崙?zhàn)教程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

