Element-ui 增加行增加列,并对 行 和 列求 和&&平均值。
静态方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta two="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
<el-table
:show-summary="showSum"
:data="tableData"
style="width: 100%"
ref="row"
>
<el-table-column
v-for="(item,index) in data" :key="item.prop"
:prop="item.prop"
:label="item.label"
>
<template slot-scope="scope">
<div>
<span>{{scope.row[item.prop] ? scope.row[item.prop] : '-' }}</span>
</div>
</template>
</el-table-column>
</el-table>
<el-checkbox-group v-model="checkList">
<el-checkbox label="对行求和"></el-checkbox>
<el-checkbox label="对行求平均值"></el-checkbox>
<el-checkbox label="对列求和"></el-checkbox>
<el-checkbox label="对列求平均值"></el-checkbox>
</el-checkbox-group>
<el-button @click="add" type="success">增加</el-button>
</div>
</body>
<script>
new Vue({
el:'#app',
data() {
return {
showSum: false,
getSumFlag1:true,
getSumFlag2: true,
getSumFlag3: true,
getSumFlag4: true,
checkList:[],
tableData: [{
information:'一',
one: 100,
two: 11,
three: 100
}, {
information:'二',
one: 200.19,
two: 22,
three: 200.00
}, {
information:'三',
one: 300.56,
two: 33,
three: 300.00
}, {
information:'四',
one: 400.9,
two: 44,
three: 400.00
}],
data:[
{label: "",prop:"information"},
{label: "一月",prop:"one"},
{label: "二月",prop:"two"},
{label: "三月",prop:"three"}
],
};
},
methods: {
add() {
ones = this.tableData.map(item => item.one)
twos = this.tableData.map(item => item.two)
three = this.tableData.map(item => item.three)
sums = [...ones,...twos,...three]
averages = this.tableData.map(item => item.average)
this.checkList.forEach(item =>{
if (item === "对行求和") {
this.data.push({label:"和",prop:"sum"})
this.tableData.forEach((item,index) => {
item.sum = this.sumRow(index)
})
}
if (item === '对列求和'){
this.tableData.push({
information: '和',
one: (this.sumColum(ones)).toFixed(2),
two: (this.sumColum(twos)).toFixed(2),
three: (this.sumColum(three)).toFixed(2),
})
}
if (item === '对行求平均值') {
this.data.push({label:"平均值",prop:"average"})
this.tableData.forEach((item,index) => {
item.average = (this.sumRow(index)/4).toFixed(2)
})
}
if (item === '对列求平均值') {
this.tableData.push({
information: '平均值',
one: (this.sumColum(ones)/4).toFixed(2),
two: (this.sumColum(twos)/4).toFixed(2),
three: (this.sumColum(three)/4).toFixed(2),
})
}
})
},
sumColum(arr){
let s = 0
arr.forEach(val =>{
s += val
})
return s
},
sumRow(index) {
let data = 0
for (let key in this.tableData[index]) {
if (key !== 'information' && key !== 'sum' && key !== 'average'){
data += this.tableData[index][key]
}
}
return data
}
}
})
</script>
</html>
若表格数据为 后台返回
this.tableHeader.push({ label:'和', 'sum' })
this.tableData.forEach((item, index) => {
item.sum = this.sumRow(index).toFixed(2)
})
const column = {}
keyList.map((ele, index) => {
if(ele !== 'information') {
column[ele] = this.sumColumn(this.statistData.map(item => item[ele])).toFixed(2)
} else {
column[ele] = '和'
}
})
this.tableData.push(column)