element-ui table組件如何使用render屬性的實現(xiàn)
前言
起因:
在使用 element-ui table組件時,由于表列比較多一個個寫特別麻煩,所以想通過將所有表頭定義成一個數(shù)組,通過遍歷多方式去實現(xiàn)。這樣解決了手寫很多 el-table-column 的情況。
障礙:
類似于下面自定義表列的樣式,它是通過 slot-scope 去覆蓋 el-table-column 內部slot的樣式實現(xiàn)的。那我們在遍歷表頭數(shù)組的時候如何實現(xiàn)呢?

參考:
用過 react 開發(fā)會經(jīng)常用到 ant design ,其中它的 table 組件是可以接受 render屬性的,下面使用table組件時,只需要定義好,columns(表頭列) data(表的具體數(shù)據(jù))即可。整體看起來很簡潔 去渲染自定義的組件的。 點擊查看 antdesign
demo:
const columns = [
{
title: 'Name',
dataIndex: 'name',
render: (text, row, index) => {
if (index < 4) {
return <a>{text}</a>;
}
return {
children: <a>{text}</a>,
props: {
colSpan: 5,
},
};
},
}]
const const data = [
{
key: '1',
name: 'John Brown',
age: 32,
tel: '0571-22098909',
phone: 18889898989,
address: 'New York No. 1 Lake Park',
}]
ReactDOM.render(<Table columns={columns} dataSource={data} bordered />, mountNode);
在 Vue 中實現(xiàn) render 屬性
接下來我們要實現(xiàn)下圖的table的樣式,但是這一次我們采用 render 傳參數(shù)的方式
思路
- 父組件將需要渲染的列表通過 props 傳遞給子組件
- 子組件使用 slot 并填充默認渲染的 el-table-column 方式為 prop 渲染 data 中傳遞的值
- 子組件通過 slot 將值傳回給父組件,父組件通過 slot-scope 接受到子組件的值,判斷該項是否有 render 屬性,有的話在組件標簽添加 render 屬性返回的 html 去覆蓋 slot 中默認的值。
子組件定義默認值
有了上面的思路,去實現(xiàn)子組件。我們需要知道一點,每個 el-table-column 只是定義了一列的表頭和數(shù)據(jù),而 :data="tableList" 中的每項值是定義了一行的數(shù)據(jù)。所以 el-table-column 是按列來分,data 是按行來分
- 通過props 去接受表頭列表,數(shù)據(jù)列表
- 遍歷表頭數(shù)據(jù),并且將 el-table-column 作為默認數(shù)據(jù),使用 slot 包裹起來
- 通過 slot 想父組件傳遞當前項的數(shù)據(jù)
<template>
<el-table :data="tableList" style="width:500px">
<template v-for="item in propList">
<slot :content="item">
<el-table-column :key="item.id" :prop="item.prop" :label="item.label"></el-table-column>
</slot>
</template>
</el-table>
</template>
<script>
export default {
props:{
propList:{
type:Array,
default:()=>[]
},
tableList:{
type:Array,
default:()=>[]
},
}
}
</script>
父組件定義
父組件通過 slot-scope 來接受到子組件傳遞過來的數(shù)據(jù),然后判斷是否有 render 屬性來確定是否用要去自定義樣式覆蓋默認的 slot
- 首先看傳遞給子組件的表頭數(shù)據(jù),可以看到,第二,三行列表中有一個render屬性,它是一個函數(shù)并返回一個 html 的字符串。
- tableList就是普通的數(shù)據(jù),也就是數(shù)據(jù)的 key 值去渲染對應的數(shù)據(jù)
- 圖片這列舉例子,當父組件通過 props 將 {label,prop,id,render} 傳遞給子組件后,子組件有通過 slot 將值傳遞回父組件。
- 到這里有些人會有疑問,為什么要將數(shù)據(jù)這樣傳來傳去,因為我們在子組件中定義好了默認樣式,而父組件中需要判斷該值是否需要自定義樣式,去覆蓋子組件中的樣式。
- 這些自定義樣式就是一開始,在render函數(shù)中返回的 html 字符串
- 為啥 React 直接返回 jsx ,而Vue需要返回 html 字符串,因為react本身就是使用 JSX 來渲染模版的,最終都會通過 babel 編譯成 React.createElement ,而Vue是通過 template 來渲染模版的,這里通過定義 template 模版字符串,最終通過 v-html 來解析
- 為什么這里有兩個 slot-scope ,第一個是 slot-item 的,組件內部通過 slot-scope 將值傳遞出來。而第二個是 el-table-item 的,ui組件內部同樣將數(shù)據(jù)通過 slot-scope 傳遞傳來。
- 通過第一個 slot-scope 拿到 propList 中的定義的 render 函數(shù),通過第二個 slot-scope 拿到 table 組件內部傳遞出來的數(shù)據(jù),將數(shù)據(jù)傳遞給 render 函數(shù)去生成自定義模版
最終通過 v-html 去解析生成的字符串模版
<slot-item :propList="propList" :tableList="tableList">
<template slot-scope="{content}" v-if="content.render">
<el-table-column :label="content.label">
<template slot-scope="{$index,row}">
<div v-html="content.render(row)"></div>
</template>
</el-table-column>
</template>
</slot-item>
export default {
components:{
SlotItem
},
data () {
return {
propList:[
{label:'姓名',prop:'name',id:1},
{label:'圖片',prop:'pic',id:2,render:({pic})=>{
return `<img style="width:30px;height:30px" src='${pic}' />`
}},
{label:'操作',prop:'operate',id:3,render:({text})=>{
return `<div style="color:#999">${text}</div>`
}},
],
tableList:[
{name:'章三',pic:'https://zh-static-files.oss-cn-hangzhou.aliyuncs.com//karazhan/content/poster/2019/11/16e30c192f6.png',text:'新增'},
{name:'里斯',pic:'https://zh-static-files.oss-cn-hangzhou.aliyuncs.com//karazhan/content/poster/2019/11/16e30c2797e.png',text:'刪除'},
{name:'網(wǎng)舞',pic:'https://zh-static-files.oss-cn-hangzhou.aliyuncs.com//karazhan/content/poster/2019/11/16e30c33144.png',text:'跳轉'},
]
}
}
}
</script>
結尾
有了render屬性,可以想 ant-design 那樣簡潔的屬性 ui組件模版了!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue3?在<script?setup>里設置組件name屬性的方法
這篇文章主要介紹了Vue3?在<script?setup>里設置組件name屬性的方法,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-11-11
vue項目刷新當前頁面的三種方式(重載當前頁面數(shù)據(jù))
這篇文章主要介紹了vue項目刷新當前頁面的三種方式(重載當前頁面數(shù)據(jù)),本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01

