Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have created an HTML table.
Selecting the td's by draging.
Mergeing td's who has highlighted by color in each tr.

Until merging cells I have done successfully. Now I'm stuck in splitting(unmerge) those merge td's. Can someone give me a idea how can I split the merged cells on clicking splitcells(button) using Javascript or jquery.

Thanks.

What I have tried:

HTML
<table cellspacing="0" cellpadding="0" border="0">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
    <input id="btMerge" type="button" value="Merge cells" />
    <input id="btSplit" type="button" value="Split cells" />

CSS:
     table {
             width: 80%;
            }
            
            table td {
                border: 1px solid #97B4D1;
                text-align: center;
                cursor: pointer;
            }


JavaScript
JavaScript:
    var StartTD = null;
    var StartIndex = null;
    var EndTD = null;
    var EndIndex = null;
    $().ready(function() {
        $("td").unbind("mousedown").bind("mousedown", function() {
            $("table td").css("background-color", "");
            StartTD = $(this);
            var y = $(this).index();
            var x = $(this).parent().index();
            StartIndex = {
                x: x,
                y: y
            };
        });
        $("td").unbind("mouseup").bind("mouseup", function() {
            EndTD = $(this);
            var y = $(this).index();
            var x = $(this).parent().index();
            EndIndex = {
                x: x,
                y: y
            };
            SelectTD(StartIndex, EndIndex, "green");
        });
        $("#btMerge").click(function() {
            MergeCell(StartIndex, EndIndex);
            MergeCell(EndIndex, StartIndex);
        });
    });

    function SelectTD(StartIndex, EndIndex, Color) {
        var MinX = null;
        var MaxX = null;
        var MinY = null;
        var MaxY = null;
        if (StartIndex.x < EndIndex.x) {
            MinX = StartIndex.x;
            MaxX = EndIndex.x;

        } else {
            MinX = EndIndex.x;
            MaxX = StartIndex.x;
        };
        if (StartIndex.y < EndIndex.y) {
            MinY = StartIndex.y;
            MaxY = EndIndex.y;
        } else {
            MinY = EndIndex.y;
            MaxY = StartIndex.y;
        };
        StartIndex = {
            x: MinX,
            y: MinY
        };
        EndIndex = {
            x: MaxX,
            y: MaxY
        };
        for (var i = MinX; i <= MaxX; i++) {
            for (var j = MinY; j <= MaxY; j++) {
                var SelectTR = $("table tr").eq(i);
                $("td", SelectTR).eq(j).css("background-color", Color);
            }
        }
    }

    function MergeCell(StartIndex, EndIndex) {
        var Colspan = null;
        var Rowspan = null;
        console.log(StartIndex, EndIndex)
        Rowspan = EndIndex.x - StartIndex.x + 1;
        Colspan = EndIndex.y - StartIndex.y + 1;
        var IndexTR = $("table tr").eq(StartIndex.x);
        $("td", IndexTR).eq(StartIndex.y).attr("colspan", Colspan).attr("rowspan", 
Rowspan);

        for (var i = StartIndex.x; i <= EndIndex.x; i++) {
            for (var j = StartIndex.y; j <= EndIndex.y; j++) {
                if (i == StartIndex.x && j == StartIndex.y) continue;
                var SelectTR = $("table tr").eq(i);
                $("td", SelectTR).eq(j).hide();
            }
        }

    }
Posted
Updated 25-Sep-18 3:21am

1 solution

Add function to split the cells:

function SplitCell(StartIndex){
    // get our starting row index
    var i = StartIndex.x;
    // select the first row to process
    var SelectTR = $("table tr").eq(i);
    while (SelectTR.length > 0) {
        // get our starting column index
        var j = StartIndex.y;
        // select first cell to process
        var target = $("td", SelectTR).eq(j);
        // while we have a valid cell
        while(target.length > 0){
            // reset our spans
            target.attr("colspan", 1).attr("rowspan", 1);
            // reset the none, so cell will display
            if(target.css('display') == 'none') {
                target.css("display","");
            }
            // step to the next sibling td cell
            j = j+1;
            target = $("td", SelectTR).eq(j);
        }
        // step to the next table row
        i = i+1;
        SelectTR = $("table tr").eq(i);
    }
}


And add a button call to the new function in the ready:

$("#btSplit").click(function() {
    SplitCell(StartIndex);
});
 
Share this answer
 
Comments
Syf AK 10-Oct-18 5:57am    
Its completely work fine, but it splits all merged cells at once. Like if i merge column1 cells and column4 cells simultaneously and apply split functionality on column1 then it will splits both column cells.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900