關(guān)于el-table表格組件中插槽scope.row的使用方式
el-table表格組件中插槽scope.row使用
要實(shí)現(xiàn)點(diǎn)擊查看顯示后端返回的字段并以文字渲染到頁(yè)面上,就要是使用到插槽
下圖是要實(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é)歷定義一個(gè)edutype方法,通過(guò)scope.row傳參
<div class="heighth">
學(xué)歷:<span>{{ edutype(scope.row.education) }}</span>
</div>
<div class="heighth">
專(zhuān)業(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: {
//通過(guò)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 "中專(zhuān)";
}
if (row == "junior college") {
return "大專(zhuān)";
}
if (row == "undergraduate") {
return "本科";
}
if (row == "graduate student") {
return "研究生";
}
if (row == "unlimited") {
return "不限";
}
}
}
這樣就實(shí)現(xiàn)啦。。。。。
slot-scope和scope.row的用法
實(shí)現(xiàn)效果
根據(jù)后端傳來(lái)的mg_state的bool型數(shù)據(jù)來(lái)渲染開(kāi)關(guān)狀態(tài),當(dāng)為true時(shí),開(kāi)關(guān)打開(kāi);為false時(shí)關(guān)閉
解決
狀態(tài)開(kāi)關(guān)屬于單元格,也屬于一行,如果我們拿到這一行的數(shù)據(jù),就可以.mg_state具體值,則可以按需渲染效果。所以想到用作用域插槽來(lái)渲染狀態(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="電話(huà)" 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”
- 表格綁定了用于存儲(chǔ)數(shù)據(jù)的數(shù)組,里面每一個(gè)元素都是數(shù)據(jù)對(duì)象
- 首先在狀態(tài)這一列中定義了一個(gè)作用域插槽
- 通過(guò)slot-scope="scope"來(lái)接收作用域插槽的數(shù)據(jù)(添加屬性slot-scope,并且定義對(duì)象scope)
- scope.row
- scope有一個(gè)屬性row(ElementUI文檔),scope.row可以拿到對(duì)應(yīng)行的數(shù)據(jù)
- v-model=“scope.row.mg_state”
- 需要把這個(gè)開(kāi)關(guān)的狀態(tài)綁定到scope.row.mg_state屬性上
ElementUI文檔

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

效果

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

