做项目的时候遇到一个需求,做一个车站车次的时刻表,可以添加删除行或者列,实现效果如下:
效果图
这个表格使用的是Element-UI,第一行减号icon其实是表格的表头部分,最左边的减号icon是表格的第一列。表格的第二行,也就是显示车次名的那一行,在tableData对应的对象里,记录的是车次名,其余行都是记录发车时刻。colList是所有车次名组成的数组,新增行就是在tableData里新增一个对象,并且把每一个车次设为空,新增列则是将tableData里每一个对象都新增一个车次属性,index是一个数字,每次使用完会自增,保证每个车次名都是唯一的,以下是实现代码:
<template>
<div style="padding:30px">
<div >
<el-button type="primary" @click="addRow">新增车站</el-button>
<el-button type="primary" @click="addCol">新增车次</el-button>
</div>
<el-table :data="tableData" cell-class-name="table-cell" height="420" :cell-style="iconHeader" :header-cell-style="iconHeader" ref="table">
<el-table-column label="" fixed="left" width="100" align="center">
<template slot-scope="{$index}">
<i v-if="$index != 0" class="el-icon-remove add_btn" @click="delRow($index)"></i>
</template>
</el-table-column>
<el-table-column fixed="left" header-align="center" width="200" >
<template slot-scope="{row,$index}">
<span v-if="$index == 0">{{row.stationName}}</span>
<el-input v-else v-model="row.stationName" placeholder="请输入车站"></el-input>
</template>
</el-table-column>
<el-table-column v-for="item in colList" :key="item" header-align="center" width="200" >
<template slot="header" slot-scope="{column,$index}">
<i v-if="$index != 1" class="el-icon-remove add_btn" @click="delCol(column,$index)"></i>
</template>
<template slot-scope="{row,$index}">
<el-input v-if="$index==0" v-model="row[item]" placeholder="请输入车次"></el-input>
<el-time-picker v-else v-model="row[item]" value-format="HH:mm" format="HH:mm" placeholder="请输入发车时间"></el-time-picker>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
colList: [],
index: 1,
tableData: [],
}
},
beforeUpdate(){
this.$nextTick(() => { //在数据加载完,重新渲染表格
this.$refs['table'].doLayout();
})
},
mounted() {
this.initTable()
},
methods: {
iconHeader({row, column, rowIndex, columnIndex}) {
if(columnIndex == 0) {
return 'border-bottom:transparent'
}
},
initTable() {
this.tableData = []
let head = {}
head.stationName = '车站/车次'
this.tableData.push(head)
this.addRow()
this.addCol()
},
addRow() {
let obj = {}
obj.stationName = ''
this.colList.map((item) => {
obj[item] = ''
})
this.tableData.push(obj)
},
delRow(index) {
if(this.tableData.length == 2) {
this.tableData.splice(index,1)
this.addRow()
}
else {
this.tableData.splice(index,1)
}
},
addCol() {
let str = 'train' + this.index
this.index++
this.tableData.map((item) => {
this.$set(item,str,'')
})
this.colList.push(str)
},
delCol(column,index) {
let str = this.colList[index-2]
this.tableData.map(item => {
delete item[str]
})
this.colList.splice(index-2,1)
if(Object.keys(this.tableData[0]).length == 1) {
this.addCol()
}
}
}
}
</script>
<style scoped lang="less">
.el-table--border::after, .el-table--group::after, .el-table::before {
background-color: transparent;
}
.add_btn {
text-align: center;
font-size: 25px;
color: #909399;
cursor: pointer;
}
/deep/ .el-table th.gutter {
display: table-cell !important;
width: 10px !important;
}
.el-date-editor.el-input, .el-date-editor.el-input__inner {
width: 179px;
}
/deep/.table-cell {
border-right: 1px solid #EBEEF5;
}
</style>
<style>
.el-table__fixed-right::before, .el-table__fixed::before {
height: 0px !important;
}
</style>
在写这个页面的时候,花最多时间的不是上面的样式和逻辑,其实是处理数据,从后端拿到的数据是要处理过才能显示在页面,保存的数据时候也要处理成后端需要的样子。