溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

element-ui table組件使用render屬性的實現方法

發布時間:2021-02-02 14:03:02 來源:億速云 閱讀:2140 作者:小新 欄目:web開發

這篇文章主要介紹了element-ui table組件使用render屬性的實現方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前言

起因:

在使用 element-ui table組件時,由于表列比較多一個個寫特別麻煩,所以想通過將所有表頭定義成一個數組,通過遍歷多方式去實現。這樣解決了手寫很多 el-table-column 的情況。

障礙:

類似于下面自定義表列的樣式,它是通過 slot-scope 去覆蓋 el-table-column 內部slot的樣式實現的。那我們在遍歷表頭數組的時候如何實現呢?

element-ui table組件使用render屬性的實現方法

參考:

用過 react 開發會經常用到 ant design ,其中它的 table 組件是可以接受 render屬性的,下面使用table組件時,只需要定義好,columns(表頭列) data(表的具體數據)即可。整體看起來很簡潔 去渲染自定義的組件的。 點擊查看 antdesign

demo:

codepen 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 中實現 render 屬性

接下來我們要實現下圖的table的樣式,但是這一次我們采用 render 傳參數的方式

element-ui table組件使用render屬性的實現方法 

思路

  1. 父組件將需要渲染的列表通過 props 傳遞給子組件

  2. 子組件使用 slot 并填充默認渲染的 el-table-column 方式為 prop 渲染 data 中傳遞的值

  3. 子組件通過 slot 將值傳回給父組件,父組件通過 slot-scope 接受到子組件的值,判斷該項是否有 render 屬性,有的話在組件標簽添加 render 屬性返回的 html 去覆蓋 slot 中默認的值。

子組件定義默認值

有了上面的思路,去實現子組件。我們需要知道一點,每個 el-table-column 只是定義了一列的表頭和數據,而 :data="tableList" 中的每項值是定義了一行的數據。所以 el-table-column 是按列來分,data 是按行來分

  • 通過props 去接受表頭列表,數據列表

  • 遍歷表頭數據,并且將 el-table-column 作為默認數據,使用 slot 包裹起來

  • 通過 slot 想父組件傳遞當前項的數據

<template>
 <el-table :data="tableList" >
  <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 來接受到子組件傳遞過來的數據,然后判斷是否有 render 屬性來確定是否用要去自定義樣式覆蓋默認的 slot

  • 首先看傳遞給子組件的表頭數據,可以看到,第二,三行列表中有一個render屬性,它是一個函數并返回一個 html 的字符串。

  • tableList就是普通的數據,也就是數據的 key 值去渲染對應的數據

  • 圖片這列舉例子,當父組件通過 props 將 {label,prop,id,render} 傳遞給子組件后,子組件有通過 slot 將值傳遞回父組件。

    • 到這里有些人會有疑問,為什么要將數據這樣傳來傳去,因為我們在子組件中定義好了默認樣式,而父組件中需要判斷該值是否需要自定義樣式,去覆蓋子組件中的樣式。

    • 這些自定義樣式就是一開始,在render函數中返回的 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組件內部同樣將數據通過 slot-scope 傳遞傳來。

  • 通過第一個 slot-scope 拿到 propList 中的定義的 render 函數,通過第二個 slot-scope 拿到 table 組件內部傳遞出來的數據,將數據傳遞給 render 函數去生成自定義模版

最終通過 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  src='${pic}' />`
     }},
     {label:'操作',prop:'operate',id:3,render:({text})=>{
      return `<div >${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:'網舞',pic:'https://zh-static-files.oss-cn-hangzhou.aliyuncs.com//karazhan/content/poster/2019/11/16e30c33144.png',text:'跳轉'},
    ]
   }
  }
 }
</script>

感謝你能夠認真閱讀完這篇文章,希望小編分享的“element-ui table組件使用render屬性的實現方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女