CSS:
@import url("//unpkg.com/element-ui@2.15.13/lib/theme-chalk/index.css");
JS:
var Main = {
methods: {
deleteRow(index, rows) {
rows.splice(index, 1);
},
getSummaries(param) {
//处理合计被整个table表遮挡问题
this.$nextTick(() =>{
this.$refs.table.doLayout();
});
const { columns, data } = param
const sums = []
const sumsRowNum = [6]
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = '总年龄:'
return
}
const values = data.map(item => Number(item[column.property]))
if (sumsRowNum.find(item => {
return item == index
}) != undefined) {
if (!values.every(value => isNaN(value))) {
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr)
if (!isNaN(value)) {
return prev + curr
} else {
return prev
}
}, 0)
sums[index]
} else {
sums[index] = 'N/A'
}
} else {
sums[index] = null
}
})
return sums
}
},
data() {
return {
tableData: [{
date: '2016-05-03',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:20
}, {
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:21
}, {
date: '2016-05-04',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:21
}, {
date: '2016-05-01',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:22
}, {
date: '2016-05-08',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:22
}, {
date: '2016-05-06',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:22
}, {
date: '2016-05-07',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333,
age:22
}]
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
HTML:
<script src="//unpkg.com/vue@2/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.15.13/lib/index.js"></script>
<div id="app" style="width: 1000px;height: 200px;">
<template>
<el-table
:data="tableData"
style="width: 100%"
max-height="250"
ref="table"
show-summary
:summary-method="getSummaries">
<el-table-column
fixed
prop="date"
label="日期"
width="150">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="120">
</el-table-column>
<el-table-column
prop="province"
label="省份"
width="120">
</el-table-column>
<el-table-column
prop="city"
label="市区"
width="120">
</el-table-column>
<el-table-column
prop="address"
label="地址"
width="300">
</el-table-column>
<el-table-column
prop="zip"
label="邮编"
width="120">
</el-table-column>
<el-table-column
fixed="right"
prop="age"
label="年龄"
width="120">
</el-table-column>
<el-table-column
fixed="right"
label="操作"
width="120">
<template slot-scope="scope">
<el-button
@click.native.prevent="deleteRow(scope.$index, tableData)"
type="text"
size="small">
移除
</el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>
效果图: