How to know the real column and row index of a html table with merged row or column

馋奶兔 提交于 2019-12-13 02:44:58

问题


Please see a demo here. http://jsfiddle.net/wgstudio/CqNfZ/2/

I want merge cell(0,0) and cell(1,0) in a html talbe, it works on a regular table.

The code isbelow.

        function() {
            var table = $("#table1");

            var rowIndex = 0;
            var colIndex = 0;                
            var actionCell =$($(table.children().children()[rowIndex]).children()[colIndex]); //Cell1
            var tr = table.children().children()[rowIndex + 1]; 
            var toBeRemoved = $($(tr).children()[colIndex]);//Cell4
            toBeRemoved.remove();
            rowSpan = toBeRemoved.attr("rowSpan") == undefined ? 1 : parseInt(toBeRemoved.attr("rowSpan"), 10);
            actionCell.attr("rowspan", (actionCell.attr("rowSpan") == undefined ? 1 : parseInt(actionCell.attr("rowSpan"), 10)) + rowSpan);
        }

but if the table is like this:

    <table id="table2" style=" width: 100%;">
        <tr>
            <td rowspan="2">cell_1</td>
            <td rowspan="2">cell_2cell_5</td>
            <td>cell_3</td>
        </tr>
        <tr>
            <td>cell_6</td>
        </tr>
        <tr>
            <td>cell_7</td>
            <td>cell_8</td>
            <td>cell_9</td>
        </tr>
    </table>

(0,0) (0,1) are merged cells, when I want to merge (0,0)"Cell1" and (2,0)"Cell7", how can I find "cell 7" via js code?

Hope I explain it clear.

Thank you very much.

Bill


回答1:


The main problem you are encountering is that once a rowspan is added to a cell, it throws off row indexing. If a cell in a row has rowspan=3 the row index for the next cell in same column is currentRowIndex + 3.

Here's a pretty good start for what you need.

$("#button2").click(function() {
    /* hard code a cell to start with*/
    var actionCell = $('td:first');

    var toBeRemoved = findNextCell(actionCell);

    combineSpans( actionCell, toBeRemoved)

    /* remove merged*/
    toBeRemoved.remove();

});

function combineSpans( actionCell, toBeRemoved){
    var actionSpans = getCellSpans(actionCell);
    var nextSpans = getCellSpans(toBeRemoved);
    /* add colspan and rowspan for both cells to apply to actionCell*/
    var newSpans = {
        rowspan: actionSpans.rowSpan + nextSpans.rowSpan,
        colspan: actionSpans.colSpan + nextSpans.colSpan
    }
     /* adjust actionCell colspan/rowspan*/
    actionCell.attr(newSpans);

}

function findNextCell($cell) {

    var cellIndex = $cell.index();
    var rowSpan = $cell.attr("rowspan") || 1;
    var rowIndex = $cell.closest('tr').index();
    var nextRow = $cell.closest('table').find('tr').eq(1 * rowSpan + rowIndex);
    return nextRow.find('td').eq(cellIndex);

}


function getCellSpans($cell) {
    var obj = {}
    obj.rowSpan = parseInt($cell.attr("rowspan") || 1, 10);
    obj.colSpan = parseInt($cell.attr("colspan") || 1, 10);
    return obj;

}

DEMO: http://jsfiddle.net/CqNfZ/7/

EDIT: just realized that my combineSpans logic needs modification. Adding 2 cells colspan together will cause problems



来源:https://stackoverflow.com/questions/14032023/how-to-know-the-real-column-and-row-index-of-a-html-table-with-merged-row-or-col

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!