一個表格中分為 行 和 列 ,有時候你經常在網頁中看到類似這樣的表格,有時候是合并了列,有時候是合并了行。那么這個是怎么做的?也是通過下面的方法實現的。
向單元格添加屬性即可實現,合并 列 和 行的屬性不一樣
1、合并列的屬性:clospan="2" //要合并幾列數字就是幾
2、合并行的屬性:rowspan="2" //同理,要合并幾行數字就是幾
3、合并后的單元格(行或列)必然會自動多出來一個,需要手動刪掉。
4、重點:合并單元格只和 td 標簽有關系:
(1)合并列:是左右合并,在左側的 td 標簽中添加 colspan="2" 要合并的 td 的數量。
(2)合并行:是上下合并,在上面的 td 標簽中添加 rowspan="2" 要合并的td的數量。
(3)合并完后刪除多余的 td 標簽。
1、合并 列 的用法:
(1)首先確定你要合并的單元格在第幾行第幾列,然后找到它,從他的td標簽中添加屬性。
例如要合并第4行的,第4和第5列:
<tr>
<td>第三節</td>
<td>html</td>
<td>css</td>
<td colspan="2">php</td> <!-- 合并 列 的用法-->
<td>php</td>
</tr>
2、合并 行 的用法:
例如下面:合并第3行和第4行的第1列。
完整代碼:↓
<script src="https://lf6-cdn-tos.bytescm.com/obj/cdn-static-resource/tt_player/tt.player.js?v=20160723"></script>
td標簽中合并單元格屬性:
行合并: rowspan="2"
列合并:colspan="2"
https://blog.csdn.net/u013558749/article/details/82257168 (element-ui 實現行合并)
https://www.cnblogs.com/guwufeiyang/p/12850088.html (合并后樣式的處理)
合并后樣式
[
{
"processId": "1499255834195238914",
"currentProcessInfoId": "1499255834245570561",
"flowType": "01",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "01",
"dimensionName": "重點-測試",
"dimensionTag": "1499255834195238914重點-測試",
"nature": "01",
"name": "1212",
"statusRemark": "1212",
"scoreStand": "1212",
"source": "1212",
"weight": 0.3,
"status": "Y",
"remark": "-",
"index": 1,
"rowIndex": 0
},
{
"processId": "1499255834195238914",
"currentProcessInfoId": "1499255834245570561",
"flowType": "01",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "01",
"dimensionName": "日常-測試",
"dimensionTag": "1499255834195238914日常-測試",
"nature": "01",
"name": "TEST",
"statusRemark": "100",
"scoreStand": "100",
"source": "100",
"weight": 0.3,
"status": "Y",
"remark": "-",
"index": 1,
"rowIndex": 1
},
{
"processId": "1499255834195238914",
"currentProcessInfoId": "1499255834245570561",
"flowType": "01",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "01",
"dimensionName": "日常-測試",
"dimensionTag": "1499255834195238914日常-測試",
"nature": "01",
"name": "12",
"statusRemark": "12",
"scoreStand": "12",
"source": "12",
"weight": 0.3,
"status": "Y",
"remark": "-",
"index": 1,
"rowIndex": 2
},
{
"processId": "1499255834195238914",
"currentProcessInfoId": "1499255834245570561",
"flowType": "01",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "01",
"dimensionName": "管理指標",
"dimensionTag": "1499255834195238914管理指標",
"nature": "02",
"name": "12",
"statusRemark": "12",
"scoreStand": "12",
"source": "12",
"weight": 0.1,
"status": "Y",
"remark": "-",
"index": 1,
"rowIndex": 3
},
{
"processId": "1499255834996350977",
"currentProcessInfoId": null,
"flowType": "02",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "00",
"dimensionName": "重點-測試",
"dimensionTag": "1499255834996350977重點-測試",
"nature": "01",
"name": "1212",
"statusRemark": "1212",
"scoreStand": "1212",
"source": "1212",
"weight": 0.3,
"status": "-",
"remark": "-",
"index": 2,
"rowIndex": 4
},
{
"processId": "1499255834996350977",
"currentProcessInfoId": null,
"flowType": "02",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "00",
"dimensionName": "日常-測試",
"dimensionTag": "1499255834996350977日常-測試",
"nature": "01",
"name": "TEST",
"statusRemark": "100",
"scoreStand": "100",
"source": "100",
"weight": 0.3,
"status": "-",
"remark": "-",
"index": 2,
"rowIndex": 5
},
{
"processId": "1499255834996350977",
"currentProcessInfoId": null,
"flowType": "02",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "00",
"dimensionName": "日常-測試",
"dimensionTag": "1499255834996350977日常-測試",
"nature": "01",
"name": "12",
"statusRemark": "12",
"scoreStand": "12",
"source": "12",
"weight": 0.3,
"status": "-",
"remark": "-",
"index": 2,
"rowIndex": 6
},
{
"processId": "1499255834996350977",
"currentProcessInfoId": null,
"flowType": "02",
"userName": "xxx",
"itcode": "xxx",
"pernr": "xxx",
"approveStatus": "00",
"dimensionName": "管理指標",
"dimensionTag": "1499255834996350977管理指標",
"nature": "02",
"name": "12",
"statusRemark": "12",
"scoreStand": "12",
"source": "12",
"weight": 0.1,
"status": "-",
"remark": "-",
"index": 2,
"rowIndex": 7
}
]
// 按照dimensionTag合并行
getDimensionNumber() {
this.dimensionIndexArr = []
let DimensionObj = {}
this.dataList.forEach((element, index) => {
element.rowIndex = index
if (DimensionObj[element.dimensionTag]) {
DimensionObj[element.dimensionTag].push(index)
} else {
DimensionObj[element.dimensionTag] = []
DimensionObj[element.dimensionTag].push(index)
}
})
for (let k in DimensionObj) {
if (DimensionObj[k].length > 1) {
this.dimensionIndexArr.push(DimensionObj[k])
}
}
},
// 按照processId合并行
getRecordNumber() {
this.recordIndexArr=[]
let OrderObj = {}
this.dataList.forEach((element, index) => {
element.rowIndex = index
if (OrderObj[element.processId]) {
OrderObj[element.processId].push(index)
} else {
OrderObj[element.processId] = []
OrderObj[element.processId].push(index)
}
})
for (let k in OrderObj) {
if (OrderObj[k].length > 1) {
this.recordIndexArr.push(OrderObj[k])
}
}
},
objectSpanMethod({row, column, rowIndex, columnIndex}) {
if (columnIndex === 0 || columnIndex === 1 || columnIndex >= 9) {
for (let i = 0; i < this.recordIndexArr.length; i++) {
let element = this.recordIndexArr[i]
for (let j = 0; j < element.length; j++) {
let item = element[j]
if (rowIndex == item) {
if (j == 0) {
return {
rowspan: element.length,
colspan: 1
}
} else if (j != 0) {
return {
rowspan: 0,
colspan: 0
}
}
}
}
}
}
if (columnIndex === 2) {
for (let i = 0; i < this.dimensionIndexArr.length; i++) {
let element = this.dimensionIndexArr[i]
for (let j = 0; j < element.length; j++) {
let item = element[j]
if (rowIndex == item) {
if (j == 0) {
return {
rowspan: element.length,
colspan: 1
}
} else if (j != 0) {
return {
rowspan: 0,
colspan: 0
}
}
}
}
}
}
},
樣式錯亂
<el-table
ref="table"
v-loading="dataListLoading"
:data="dataList"
border
:span-method="objectSpanMethod"
:row-class-name="tableRowClassName"
@cell-mouse-enter="cellMouseEnter"
@cell-mouse-leave="cellMouseLeave"
style="width: 100%;margin-top: 16px"
>
.......
</el-table>
tableRowClassName({row, rowIndex}) {
let arr = this.hoverOrderArr
for (let i = 0; i < arr.length; i++) {
if (rowIndex === arr[i]) {
return 'success-row'
}
}
},
cellMouseEnter(row, column, cell, event) {
this.rowIndex = row.rowIndex
this.hoverOrderArr = []
this.recordIndexArr.forEach((element) => {
if (element.indexOf(this.rowIndex) >= 0) {
this.hoverOrderArr = element
}
})
},
cellMouseLeave(row, column, cell, event) {
this.rowIndex = '-1'
this.hoverOrderArr = []
},
<style scoped>
/deep/ .el-table .success-row {
background: #ecf4fe;
}
</style>
*請認真填寫需求信息,我們會在24小時內與您取得聯系。