html的table标签用js做合并单元格操作

JavaScript026

html的table标签用js做合并单元格操作,第1张

为什么要用js呢?table本身就有合并单元格的功能,其实如果你用js也是给table添加对应的样式。具体代码如下:

<style>

table{ border-collapse:collapse}

table td{ border:2px solid #ddd width:50px height:25px text-align:center}

</style>

<table>

  <tr>

    <td colspan="2">123</td> //横向合并单元格

    <td>456</td>

    <td>789</td>

  </tr>

  <tr>

    <td rowspan="2">123</td> //竖向合并单元格

    <td>456</td>

    <td>456</td>

    <td>456</td>

  </tr>

  <tr>

    <td>123</td>

    <td>456</td>

    <td>456</td>

  </tr>

</table>

<el-table :data=" dataDetailsList "  :span-method="objectSpanMethod"   :row-class-name="tableRowClassName" >

            <el-table-column prop=" photo_group_name " label="照片组名称"></el-table-column>

            <el-table-column prop="title" label="文件名称"></el-table-column>

</el-table>

js:

data(){

return{

            position: 0,

            rowSpanArr: [],

    }

}

        methods:{

         // 获取合并的数组

     getRowSpan()  {

      this.rowSpanArr = []

      this.dataDetailsList.forEach((item, index) => {

        if (index == 0) {

          this.rowSpanArr.push(1)

          this.position = 0

        } else {

          if (this. dataDetailsList[index].photo_group_name  == this. dataDetailsList[index - 1].photo_group_name ) {

            this.rowSpanArr[this.position] += 1 //项目名称相同,合并到同一个数组中

            this.rowSpanArr.push(0)

            this.dataDetailsList[index].photo_group_name = this.dataDetailsList[index - 1].photo_group_name

          } else {

            this.rowSpanArr.push(1)

            this.position = index

          }

        }

      })

    },

     objectSpanMethod ({ row, column, rowIndex, columnIndex }) {

      // 只合并区域位置

      if (columnIndex === 0) {

        const _row = this.rowSpanArr[rowIndex]

        return {

          rowspan: _row, //行

          colspan: 1 //列

        }

      }

    },

 queryExcelDetails(){

              queryExcelDetails(this.queryData).then(res => {

                  if (res.code == 200) {

                    this.tableData = []

                    this.dataDetailsList = []

                    this.dataDetailsList = res.data.list

                    this.queryData.total = res.data.total

                    this.dataDetails = true

                    this. getRowSpan()

                  }else{

                    this.$message.error({

                      showClose: true,

                      message: res.message

                    })

                  }

                })

            },

}

JS合并单元格、JavaScript单元格合并

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>合并表格</title>

<script>

//功能:合并表格

//参数:tb-需要合并的表格ID

//参数:colLength--需要对前几列进行合并,比如,

//想合并前两列,后面的数据列忽略合并,colLength应为2

//缺省表示对全部列合并

//data:2011.11.06

///////////////////////////////////////////////

function uniteTable(tb,colLength){

//检查表格是否规整

if(!checkTable(tb)) return

var i=0

var j=0

var rowCount=tb.rows.length//行数

var colCount=tb.rows[0].cells.length//列数

var obj1=null

var obj2=null

//为每个单元格命名

for(i=0i<rowCounti++){

for(j=0j<colCountj++){

tb.rows[i].cells[j].id="tb__" + i.toString() + "_" + j.toString()

}

}

//逐列检查合并

for(i=0i<colCounti++){

if(i==colLength) return

obj1=document.getElementById("tb__0_"+i.toString())

for(j=1j<rowCountj++){

obj2=document.getElementById("tb__"+j.toString()+"_"+i.toString())

if(obj1.innerText==obj2.innerText){

obj1.rowSpan++

obj2.parentNode.removeChild(obj2)

}else{

obj1=document.getElementById("tb__"+j.toString()+"_"+i.toString())

}

}

}

}

//功能:检查表格是否规整

//参数:tb--需要检查的表格ID

//data: 2011.11.06

function checkTable(tb){

if(tb.rows.length==0) return false

if(tb.rows[0].cells.length==0) return false

for(var i=0i<tb.rows.lengthi++){

if(tb.rows[0].cells.length!=tb.rows[i].cells.length) return false

}

return true

}

</script>

</head>

<body>

<table width="400" border="1" id="table1">

<tr>

<td>a</td>

<td>for</td>

<td>100</td>

<td>200</td>

<td>1</td>

</tr>

<tr>

<td>a</td>

<td>for</td>

<td>100</td>

<td>300</td>

<td>2</td>

</tr>

<tr>

<td>a</td>

<td>if</td>

<td>100</td>

<td>200</td>

<td>3</td>

</tr>

<tr>

<td>a</td>

<td>if</td>

<td>300</td>

<td>230</td>

<td>4</td>

</tr>

<tr>

<td>a</td>

<td>if</td>

<td>320</td>

<td>230</td>

<td>5</td>

</tr>

</table>

<br><input type="button" value="合并表格" onClick="uniteTable(table1,4)">

</body>

</html>