Click here to Skip to main content
15,924,318 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have created a dynamic table and selecting(on clicking td) the table cells using jquery. Now i want to merge the selected table cells on button(merge) click. How can i do this.

What I have tried:

            <pre lang="HTML">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head id="Head1" runat="server">
            <title></title>
            <script src="jquery-3.3.1.min.js"></script>
            <link href="StyleSheet1.css" rel="stylesheet" />
            </head>
            function CreateTable() {
            var rowCtr;
            var cellCtr;
            var rowCnt;
            var cellCnt;
            var myTableDiv = document.getElementById("myDynamicTable");
            var table = document.createElement('Table');
            table.border = '1';
            table.id = 'myTable';
            var tableBody = document.createElement('Tbody');
            table.appendChild(tableBody);
            rowCnt = document.getElementById('txtrows').value;
            cellCnt = document.getElementById('txtcols').value;
            for (var rowCtr = 0; rowCtr < rowCnt; rowCtr++) {
                var tr = document.createElement('tr');
                tableBody.appendChild(tr);
                for (var cellCtr = 0; cellCtr < cellCnt; cellCtr++) {
                    var td = document.createElement('td');
                    td.width = '120';
                    td.appendChild(document.createTextNode("Row:" + rowCtr + " Column:" + cellCtr));
                    tr.appendChild(td);
                }
            }
            myTableDiv.appendChild(table);
            $(document).ready(function () {
                $('#myTable td').click(function () {
                    var selected = $(this).hasClass('highlited');
                    $('#myTable tr').removeClass('highlited');
                    if (!selected)
                        $(this).addClass('highlited');
                });
            });
        }
    <form id="form1" runat="server">
       <div id="myDynamicTable">
            Creating a dyanamic Table
           <table>
               <tr>
                   <td>Row:     
                        <asp:TextBox ID="txtrows" placeholder="No of Rows Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

                       <br />
                       <br />
                   </td>
               </tr>
               <tr>
                   <td>Coloum:
                        <asp:TextBox ID="txtcols" placeholder="No of Coloums Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox>

                       <br />
                       <br />
                   </td>
               </tr>
               <tr>
                   <td>
                       <asp:Button ID="btnCreateTable" Text="Create Table" runat="server" CssClass="btnStyle" OnClick="OnClickOfCreateTable" />
                   </td>
               </tr>
           </table>

           <br />
           <br />
           <asp:Button ID="btnMerge" Text="MergeCells" runat="server" CssClass="/>    
       </div>         
    </form>
</body>
</html>
Posted
Updated 11-Sep-18 9:21am
v3
Comments
littleGreenDude 11-Sep-18 12:11pm    
I don't see where you are calling CreateTable. If I'm correct, it looks like you are picking up the number of rows and columns from user entered fields, and then dynamically creating the table?

If so, the click function in document ready has nothing to attach to, and will never be fired. Try adding the event listener to the table creation logic.

tr.setAttribute('onclick', "(function(){ ChangeRow(); })()");

 
Share this answer
 
Comments
Syf AK 12-Sep-18 3:20am    
https://www.redips.net/javascript/table-td-merge-split This looks like a java script library. I ttying to do with some custom java script or jquery.
Hope this is what you were looking for, if not, it should get you close to where you want to go. I added comments around the the key statements.

HTML
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">

function CreateTable() {

	var rowCtr;
	var cellCtr;
	var rowCnt;
	var cellCnt;

	var myTableDiv = document.getElementById("myDynamicTable");

	var table = document.createElement('Table');
	table.border = '1';
	table.id = 'myTable';

	var tableBody = document.createElement('Tbody');
	// add onclick call to table body
	tableBody.onclick = function (e) {
		ChangeRow(e);
	};
	table.appendChild(tableBody);

	rowCnt = document.getElementById('txtrows').value;
	cellCnt = document.getElementById('txtcols').value;

	for (var rowCtr = 0; rowCtr < rowCnt; rowCtr++) {
		var tr = document.createElement('tr');
		tr.id = 'info';
		tableBody.appendChild(tr);		

		for (var cellCtr = 0; cellCtr < cellCnt; cellCtr++) {
			var td = document.createElement('td');
			td.width = '120';
			td.appendChild(document.createTextNode("Row:" + rowCtr + " Column:" + cellCtr));
			tr.appendChild(td);
		}
	}
	
	
	myTableDiv.appendChild(table);
}

function ChangeRow(e){
	e = e || window.event;
	var target = e.srcElement || e.target;
	// make sure our click targets a table cell
	while (target && target.nodeName !== "TD") {
		target = target.parentNode;
	}
	if (target) {
		var sibling = target.nextSibling;
		// make sure we have a sibling, the last column doesn't
		if(sibling) {
			// make current cell span 2 columns
			target.colSpan = "2";
			// merge sibling text to the first cell
			target.innerText = target.innerText + sibling.innerText;			
			// delete cell that was merged in to the first cell
			sibling.remove();
		}
	}
}

	$(document).ready(function () {
		
	});
 
</script>
<html>


<body>
  Rows: <input type="text" id="txtrows" size="2" ><br>
  Columns: <input type="text" id="txtcols" size="2" ><br>
<input type="button" value="Create The Table" onclick="CreateTable()">
<div id="myDynamicTable">
</div>

</body>
</html> 
 
Share this answer
 

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