封装element table表格 使用render函数
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
免费下载资源
·
目的:使其table中得每个单元格都能做到自定义
新建一个组件 STable.vue
<template>
<el-table
:data="list"
style="width: 100%">
<el-table-column
v-for="item in columns"
:key="item.key"
:prop="item.key"
:label="item.title"
width="180">
<template slot-scope="scope">
<ex-slot v-if="item.render" :render="item.render" :row="scope.row" :index="scope.$index" :column="item" />
<span v-else>{{ scope.row[item.key] || '-' }}</span>
</template>
</el-table-column>
</el-table>
</template>
<script>
// 自定义内容的组件
var exSlot = {
functional: true,
props: {
row: Object,
render: Function,
index: Number,
column: {
type: Object,
default: null
}
},
render: (h, data) => {
const params = {
row: data.props.row,
index: data.props.index
}
if (data.props.column) params.column = data.props.column
return data.props.render(h, params)
}
}
export default {
components: {
exSlot,
},
props:{
columns:{
type:Array,
default:()=>[]
},
list:{
type:Array,
default:()=>[]
}
},
data() {
return {
tableData: [{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄'
}, {
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}, {
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄'
}]
}
}
}
</script>
<style></style>
使用
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<STable :columns="columns" :list="list"/>
</div>
</template>
<script>
import STable from './STable.vue'
export default {
name: 'app',
data(){
return{
columns:[
{
title:"姓名",
key:"name",
align:"center"
},
{
title: "年龄",
key: "age",
align: "center"
},
{
title: "地址",
key: "address",
align: "center"
},
{
title: "操作",
key: "caozuo",
align: "left",
render:(h,params)=>{
if (!params.row.operations||params.row.operations.length === 0) {
return h('div', '')
}
let arr = []
params.row.operations.forEach(type => {
arr.push(h("el-button",{
props: {
type:"primary",
icon:"el-icon-edit"
},
on:{
click:()=>{
alert("执行了",JSON.stringify(params.row),type)
}
}
},this.getType(type)))
})
return h('div',arr)
}
},
],
list:[
{
name:"yangjie",
age:18,
address:"aaaa",
operations:[0]
},
{
name:"junchi",
age:33,
address:"bbb",
operations:[2]
},
{
name:"haojie",
age:45,
address:"ccc",
operations:[]
},
{
name:"king",
age:59,
address:"dddd",
operations:[1,0]
},
],
}
},
methods: {
getType(type){
if(type === 0){
return "修改"
}else if(type === 1){
return "删除"
}else if(type === 2){
return "重置"
}
}
},
components: {
STable
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:3 个月前 )
c345bb45
7 个月前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 7 个月前
更多推荐
已为社区贡献5条内容
所有评论(0)