zoukankan      html  css  js  c++  java
  • 数飞OA:js控制table单元格多次合并

    当获取一个单元格对象后,通过设置colSpanrowSpan可实现单元格合并,我一直在用deleteCell的方式删除掉被合并的单元格,多次合并的时候,就很难定位到cellIndex, rowIndex,会导致合并后表格混乱的情况。探索了很多次,最终的解决办法是被合并的单元格不直接删除,而是采用设置style.display = “none”的方式。

     

    以下为数飞OA测试单元格合并的代码,有兴趣的朋友一起来把它做完善。

    <!-----数飞OA实验代码:IE7上测试---->

    <table border="1" width="500" height="200" id="tid">

     <tr>

        <td>0-0</td>

        <td>0-1</td>

        <td>0-2</td>

        <td>0-3</td>

     </tr>

     <tr>

        <td>1-0</td>

        <td>1-1</td>

        <td>1-2</td>

        <td>1-3</td>

     </tr>

     <tr>

        <td>2-0</td>

        <td>2-1</td>

        <td>2-2</td>

        <td>2-3</td>

     </tr>

     <tr>

        <td>3-0</td>

        <td>3-1</td>

        <td>3-2</td>

        <td>3-3</td>

     </tr>

    </table>

    当前单元格:<input type="text" value="0" id="rid"> <input type="text" value="1" id="cid">

    <input type="button" value="往下合并" onClick="mergeDown()">

    <input type="button" value="往右合并" onClick="mergeRight()">

    <br>

    当前行的内容:<input type="text" value="" id="vid"><input type="button" value="当前单元格内容" OnClick="DisplayValue();">

    <script language="javascript">

    function mergeDown() {

     var oTable = document.getElementById("tid");

     var iRow = parseInt(document.getElementById("rid").value);

     var iCell = parseInt(document.getElementById("cid").value);

     var oRow = oTable.rows[iRow];

     var oCell = null;

     //数飞OA注释:增加判断,避免null

     if (oRow != null) {

           oCell = oRow.cells[iCell];

     }

     if (oCell == null) {

           return;

     }

     var v = 0;

     var iRow2 = iRow;

     var iRowSpan = oCell.rowSpan;

     while (true) {

        iRow2 = iRow2 + 1;

        if (iRow2>oTable.rows.length) {

           break;

        }

        var oRow2 = oTable.rows[iRow2];

        var oCell2 = null;

        if (oRow2 != null) {

         oCell2 = oRow2.cells[iCell];

               if (oCell2 == null) {

            break;

               }

           //数飞OA注释:增加判断,避免不对称的合并方式

               if (oCell2.colSpan != oCell.colSpan) {

            alert("不可合并");

            break;

               }

               if (oCell2.style.display == "none" && oCell2.style.colSpan == "yes" ) {

            alert("不可合并");

            break;

               }

           if (oCell2.style.display != "none") {

            oCell2.style.display = "none";

            //数飞OA注释:自定义个style样式,避免隐藏的单元格被多次合并。

            oCell2.style.rowSpan = "yes";  

            iRowSpan += oCell2.rowSpan;

            oCell.rowSpan = iRowSpan;

            break;

            } else {

            //再往下合并

            }

        } else {

           break;

        }

     }

    }

    function mergeRight() {

     var oTable = document.getElementById("tid");

     var iRow = parseInt(document.getElementById("rid").value);

     var iCell = parseInt(document.getElementById("cid").value);

     var oRow = oTable.rows[iRow];

     var oCell = null;

     if (oRow != null) {

           oCell = oRow.cells[iCell];

     }

     if (oCell == null) {

           return;

     }

     var v = 0;

     var iCell2 = iCell;

     var iCellSpan = oCell.colSpan;   //

     while (true) {

        iCell2 = iCell2 + 1;

        if (iCell2>oRow.length) {

           break;

        }

        var oCell2 = null;

        oCell2 = oRow.cells[iCell2];

        if (oCell2 == null) {

           break;

        }

        //if (oCell2.rowSpan != oCell.rowSpan || oCell2.style.display == "none") {

        if (oCell2.rowSpan != oCell.rowSpan) {

           alert("不可合并");

           break;

        }

        if (oCell2.style.display == "none" && oCell2.style.rowSpan == "yes" ) {

           alert("不可合并");

           break;

        }

        if (oCell2.style.display != "none") {

           iCellSpan += oCell2.colSpan;

           oCell2.style.display = "none";

           oCell2.style.colSpan = "yes"; //自定义的属性

           oCell.colSpan = iCellSpan;    

           break;

        }

     }

    }

    //当前行内容

    function DisplayValue() {

     var oTable = document.getElementById("tid");

     var iRow = document.getElementById("rid").value;

     var iCell = document.getElementById("cid").value;

     var oRow = oTable.rows[iRow];

     var oCell = null;

     if (oRow != null) {

           oCell = oRow.cells[iCell];

     }

     if (oCell != null) {

         document.getElementById("vid").value = oCell.innerText;

     }

    }

    </script>

     

    我来自深圳数飞尔科技,欢迎一起探讨OA、表单、驾校等软件的规划、开发和销售。
  • 相关阅读:
    AIO: 新版AllInOne Code Framework 2009411新鲜出炉
    AIO: 最新AllInOne Code Framework 2009427 问世了!
    Best Practice: A Summary of Normal Way to Debug Memory Leak
    AllInOne Code Framework (http://cfx.codeplex.com)
    AllInOne Code Framework(AIO) Team 祝博客园所有同仁节日快乐!另:AIO 最新 Release 再次问世了!
    Trick: 巧用.NET Reflector, SOS Debugging找出和某一个TransactionScope绑定的SqlConnection objects以及SqlConnection中开着的SqlDataReader objects (Find all SqlConnection objects associated with a TransactionScope)
    AllInOne Code Framework: 微软一站式开发技术框架解决方案 2009614 新增sample code简介
    AllInOne Code Framework: 微软一站式开发技术框架解决方案 200978 新增sample code简介
    AIO: AllInOne Code Framework 2009510 最新问世!
    poj1737 Connected Graph[转] ***
  • 原文地址:https://www.cnblogs.com/soarwell/p/2252751.html
Copyright © 2011-2022 走看看