因爲表格數據比較同構,有時候列配置是動態的,此時但願使用v-for來配置columnhtml
<template> <el-table :data="tableData"> <el-table-column v-for="col in columns" :key="col.dataIndex" :label="col.title" :prop="col.dataIndex" :formatter="col.render" /> </el-table> </template> <script> const PHONE_COLS = [ { title: '錄音id', dataIndex: 'attach_id' }, { title: '接聽客服', dataIndex: 'handle_user_info' }, { title: '呼叫類型', dataIndex: 'type_desc' }, { title: '通話時長', dataIndex: 'duration' }, ]; const WORK_COLS = [ { title: '工單id', dataIndex: 'attach_id' }, { title: '建立客服', dataIndex: 'handle_user_info' }, { title: '建立時間', dataIndex: 'c_t' }, { title: '工單來源', dataIndex: 'source' }, { title: '工單分類', dataIndex: 'ws_type', render: (row, column, cell) => (cell || []).join(',') } ]; export default { data () { return { columns: PHONE_COLS, tableData: [], tableTotal: 0, } }, } </script>
能夠看到代碼比直接使用template更加簡潔,另外也能夠利用formatter
函數處理簡單的自定義字符串vue
template
的列當咱們但願使用配置的方式來達到<template v-slot>
的效果,須要藉助於vue
的render
函數與jsx
模板node
jsx
引入方法詳見
渲染函數 & JSX
此時全部想建立vnode的時候均可以藉助jsx
來實現,jsx
編譯須要一個createElement
方法,以下react
// h就是createElement function render(h) { var a = 'test' return ( <el-tooltip class="item" effect="dark"> {a} </el-tooltip> ) }
table的formatter
方法也支持返回vnode渲染,所以咱們能夠想辦法拿到createElement
後使用jsx來達到複雜列的渲染,以下git
<template> <el-table :data="tableData"> <el-table-column v-for="col in columns" :key="col.dataIndex" :label="col.title" :prop="col.dataIndex" :formatter="col.render" /> </el-table> </template> <script> let createElement const WORK_COLS = [ { title: '工單id', dataIndex: 'attach_id' }, { title: '建立客服', dataIndex: 'handle_user_info' }, { title: '建立時間', dataIndex: 'c_t' }, { title: '工單來源', dataIndex: 'source' }, { title: '工單分類', dataIndex: 'ws_type', render: (row, column, cell) => (cell || []).join(',') }, { title: '工單描述', dataIndex: 'desc', render: (row, column, cell) => { return (function(h) { return ( <div domPropsInnerHTML={cell}></div> ); })(createElement) } }, ]; export default { data () { return { columns: WORK_COLS, tableData: [], tableTotal: 0, } }, mounted() { createElement = this.$createElement } } </script>
說明github
jsx
模板,咱們製造了帶有h函數的匿名函數來生成vnodeNote the h function, which is a shorthand for a Vue instance's $createElement method, must be in the scope where the JSX is. 詳細
createElement
方法比較多,好比能夠把columns
放在組件內部經過this.$createElement
,此處是1個簡單例子dangerouslySetInnerHTML
無效,須要使用babel-plugin-transform-vue-jsx
的屬性domPropsInnerHTML
以上。babel