473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Removing "nested" rows in a table

127 100+
Hi - I have a function which appends row(s) to the bottom of a table:

Expand|Select|Wrap|Line Numbers
  1. function mouseUpHandler() {
  2. var table = document.getElementById("mytable");
  3. var rowCount = table.rows.length;  
  4. var addRow = table.insertRow(rowCount);
  5. addRow.id = cnt;
  6. var addCell = addRow.insertCell(1);
  7. addCell.id = cnt;
  8. addCell.colSpan = 3;
  9. var element = document.createElement("input");
  10. element.type="button";
  11. element.value="Add fixed component";
  12. element.onclick=function(){
  13. addComponent(this)
  14. }
  15. addCell.appendChild(element);
  16. }
  17.  
The button within the(se) row(s) allows the user to repeatedly insert "nested" row(s) in between:

Expand|Select|Wrap|Line Numbers
  1. function addComponent(nSel)
  2. {
  3. var nrowIndex = nSel.parentNode.parentNode.rowIndex;
  4. var addRow = document.getElementById('mytable').insertRow(nrowIndex+1);
  5. addRow.id = nSel.parentNode.id;
  6. var addCell = addRow.insertCell(0);
  7. addCell.id = nSel.parentNode.id;
  8. addCell.colSpan = 3;
  9. addCell.innerHTML = 'Component:';
  10. }
  11.  
My problem is I want a function that deletes the last added row executed by "mouseUpHandler" and all the "nested" cells related to that "parent" cell (but not any "nested cells of earlier "parent" cells). My problem is that the value required by deleteRow varies every time this delete command is executed.

I have tried to circumvent this by naming all "nested" cells (and their corresponding rows) the same is their "parent" row. But I cannot figure out how to delete multiple rows (or cells) that have the same ID.

Any suggestions?

Thanks!
Apr 14 '09 #1
8 2718
dmjpro
2,476 2GB
in HTML same ID should not appear more than once. Actually what happens it takes the element which got the same ID lastly.
"deleteRow" needs the row number(starts from 0) which you want to delete.
Can't you get the row number?
One thing you can do .... take the row reference then remove it ..
Expand|Select|Wrap|Line Numbers
  1. row_reference.parentNode.removeChild(row_reference);
  2.  
Apr 15 '09 #2
phub11
127 100+
Thanks for the reply!

Since inserting "nested" cells changes row numbering, I'm starting to think I need an array which keeps track of each group of cells ("parent" and "nested" offspring). Is this the best approach?

I have added some code below which should demonstrate what I'm trying to do. Basically, I want the "Remove Last Group" button to delete the last "parent" cell, and all of it's corresponding "nested" cells.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. window.onload = function() {
  6. insRow();
  7. }
  8.  
  9. function insRow()
  10. {
  11. var myTab = document.getElementById('myTable');
  12. var rowLength=myTab.rows.length; 
  13. var addRow=myTab.insertRow(rowLength);
  14.  
  15. var addCell=addRow.insertCell(0);
  16. var element= document.createElement('input');
  17. element.type="text";
  18. addCell.appendChild(element); 
  19.  
  20. var addCell=addRow.insertCell(1);
  21. var element= document.createElement('input');
  22. element.type="button";
  23. element.value="Add Nested Row";
  24. element.onclick = function(){
  25. nestedRow(this)} 
  26. addCell.appendChild(element);
  27.  
  28. var addCell=addRow.insertCell(2);
  29. var element= document.createElement('input');
  30. element.type="button";
  31. element.value="Remove";
  32. element.onclick = function(){
  33. delRow(this)} 
  34. addCell.appendChild(element); 
  35. }
  36.  
  37. function nestedRow(thisRow)
  38. {
  39. var rowIndex = thisRow.parentNode.parentNode.rowIndex;
  40. var addRow = document.getElementById('myTable').insertRow(rowIndex+1);
  41. var addCell=addRow.insertCell(0);
  42. addCell.innerHTML = 'Nested Row';
  43.  
  44. var addCell=addRow.insertCell(1);
  45. var element= document.createElement('input');
  46. element.type="button";
  47. element.value="DEL. NESTED ROW";
  48. element.onclick = function(){
  49. delRow(this)} 
  50. addCell.appendChild(element);
  51. }
  52.  
  53. function delRow(thisRow)
  54. {
  55. var rowIndex = thisRow.parentNode.parentNode.rowIndex;
  56. document.getElementById('myTable').deleteRow(rowIndex);
  57. }
  58.  
  59. function removeLast()
  60. {
  61. alert("I'm stuck here!");
  62. }
  63. </script>
  64.  
  65. </head>
  66. <body>
  67. <br/>
  68. <input type="button" value="Add New Row" onclick="insRow()">
  69. <br/>
  70. <table id="myTable" border="1" cellspacing="5" cellpadding="5">
  71. <tr>
  72. <th>Name</th>
  73. <th></th>
  74. </tr>
  75. </table>
  76. <input type="button" value="Remove Last Group" onclick="removeLast();" />
  77. </body>
  78. </html>
  79.  
Thanks in advance for any help!
Apr 15 '09 #3
dmjpro
2,476 2GB
@phub11
What you want to do with the "removeLast"?
Do you want to delete the last row you add or want to remove the last row?
If you want to delete the last row then ...
Expand|Select|Wrap|Line Numbers
  1. tab_ref.deleteRow(tab_ref.rows.length-1);
  2.  
and if you want to delete the last added row then you track the last row index in a global variable when added then ...
Expand|Select|Wrap|Line Numbers
  1. tab_ref.deleteRow(saved_rowIndex);
  2.  
One more thing you can upgrade your "delRow" function.
Expand|Select|Wrap|Line Numbers
  1. function delRow(thisRow)
  2. {
  3. thisRow.parentNode.removeChild(thisRow);
  4. }
  5.  
Kind regrds
Debasis.
Apr 16 '09 #4
phub11
127 100+
Thanks for the reply.

I managed to figure out what I wanted to do (sorry if I wasn't clear). I've included the salient part below. It's probably not the best way - but I'm still a Javascript newbie, so I guess I'm excused! I'll try to incorporate your suggestions too.

Oh, and the "deleteLast" takes all values in the last element of the "cellsArray", makes that into a new array (containing all nested rows pertaining to the parent row), and deletes those rows.

Cheers!

Expand|Select|Wrap|Line Numbers
  1. rowCnt=0;
  2. nrowCnt=0;
  3. cellsArraylength = (cellsArray.length)
  4. //need to set "rowPrefix" aginst parent cell ID (only set during insCell)
  5. rowPrefix = cellsArray[parentRowID]
  6. alert("cellsArray:"+cellsArray+" parentRowID:"+parentRowID+" rowIndex:"+rowIndex+" rowPrefix:"+rowPrefix);
  7. cellsArray.splice(parentRowID,1,rowPrefix+'-'+(rowIndex+1));
  8. alert(cellsArray);
  9. for (rowCounter=0; rowCounter < (cellsArraylength); rowCounter++) {
  10. tempString = String(cellsArray[rowCounter]);
  11. //check if "tempString" contains more than 1 row in it's group (i.e., an "-")
  12. multiRowcheck = tempString.match("-");
  13. if (multiRowcheck=="null"){
  14. tempArray = parseInt(tempString);
  15. } else {
  16. tempArray = tempString.split( '-' );
  17. }
  18. tempArraylength = (tempArray.length);
  19. for (insCounter=0; insCounter < (tempArraylength); insCounter++) {
  20. tempArray[insCounter]=nrowCnt;
  21. nrowCnt++;
  22. }
  23. tempString = tempArray.join('-');
  24. cellsArray[rowCnt] = tempString;
  25. rowCnt++;
  26. }
  27.  
Apr 16 '09 #5
dmjpro
2,476 2GB
Now it's Working now or not?
Apr 17 '09 #6
phub11
127 100+
Yes, but.....

Adding a nested row produces the correct insertion into an element of an array, such that, with 3 parent rows (Array[] = 0,1,2), insertion into row 2 (or Array[1]) produces Array[]=0,1-2,3. (The "-" keeps track of nested rows).

However, deleting the newly nested row is proving difficult. For the above example, I would like to change Array[1] from "1-2" back to "1", and Array[2] from "3" back to "2". Below is my code.

Any help gratefully appreciated...

Expand|Select|Wrap|Line Numbers
  1. function delnestRow(thisRow)
  2. {
  3. rowCnt=0;
  4. nrowCnt=0;
  5. var rowIndex = thisRow.parentNode.parentNode.rowIndex;
  6. cellsArraylength = (cellsArray.length);
  7. for (rowCounter=0; rowCounter < (cellsArraylength); rowCounter++) {
  8. tempString = String(cellsArray[rowCounter]);
  9. //check if "tempString" contains more than 1 row in it's group (i.e., an "-")
  10. multiRowcheck = tempString.match("-");
  11. if (multiRowcheck=="null"){
  12. tempArray = parseInt(tempString);
  13. } else {
  14. tempArray = tempString.split( '-' );
  15. }
  16. tempArraylength = (tempArray.length);
  17. for (insCounter=0; insCounter < (tempArraylength); insCounter++) {
  18. tempArray[insCounter]=nrowCnt;
  19. alert('tempArray[insCounter]: '+tempArray[insCounter]+' nrowCnt: '+nrowCnt+' rowIndex: '+rowIndex);
  20. //If counter matches rowIndex, delete that element
  21. ////if (nrowCnt==rowIndex){
  22. //////tempArray.splice(insCounter,1);
  23. ////} else {
  24. ////nrowCnt++;
  25. ////}
  26. if (nrowCnt==rowIndex){
  27. tempArray[insCounter]="";
  28. nrowCnt--;
  29. } else {
  30. nrowCnt++;
  31. }
  32. }
  33. tempString = tempArray.join('-');
  34. cellsArray[rowCnt] = tempString;
  35. rowCnt++;
  36. }
  37. document.getElementById( 'selectedCells2' ).innerHTML = cellsArray;
  38. document.getElementById('myTable').deleteRow(rowIndex);
  39. }
  40.  
Cheers!
Apr 17 '09 #7
dmjpro
2,476 2GB
See ... First of all stop writing Cheers! It's so confusing :)
Anyway ... Do one thing keep the references of newly added nested row, no matter wherever it added.
Then delete all the nested rows from the Array and then clear the Array. The Array must be a global reference.

Expand|Select|Wrap|Line Numbers
  1. var nestedRows = [];
  2.  
  3. function addNestedRows(){
  4.  var row1 = ..... 
  5.  ....
  6.  ....
  7.  nestedRows[nestedRows.length] = row1;
  8.  var row2 = ..... 
  9.  ....
  10.  ....
  11.  nestedRows[nestedRows.length] = row2;
  12.  ....
  13.  ....
  14.  //Up to Nth Row
  15. }
  16.  
  17. function deleteAll(){
  18.  for(var i=0;i<nestedRows.length;i++) nestedRows[i].parentNode.removeChild(nestedRows[i]);
  19.  nestedRows.length = 0;
  20. }
  21.  
I think it would be better to delete the Nested Rows.
Apr 20 '09 #8
phub11
127 100+
Thanks!

Over the weekend I figured out a nice way of deleting groups of rows (i.e., the parent row and all of its daughters). Basically, assign a sequentially increased row ID to each newly added parent row. Then, each time a new "nested" row is inserted, sequentially renumber both the array (parent) and arrayed array (parent and daughters) according to rowIndex (i.e., 0,1-2-3,4 becomes 0-1,2-3-4,5). When deleting a nested row, loop through the array (and arrayed array) and splice off all rows where the selected rowIndex doesn't match the loop counter (i.e., all bar one); the splice position can be calculated by "rowIndex-parentRowID". Next renumber the array and arrayed array so that it sequentially increases again (i.e., 0-1,2-3-4,5 first changes to 0,2-3-4,5 and is then renumbered to 0,1-2-3,4).

Confused? Me too!

Che.... I mean, thanks!
Apr 20 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Average_Joe | last post by:
Hello PHP people, Was wondering if PHP5 had some sort of "nested class" functionality. I'm trying to figure out how to return an object (with a private constructor) that has access to...
0
by: NotGiven | last post by:
the code below assumes 3 result sets: rsAuthors - authors table rsBooks - book table rsBooksSummary - summary of book table, Count(bookid) grouped by authorID It displays something like this: ...
15
by: Robin Eidissen | last post by:
What I try to do is to iterate over two variables using template metaprogramming. I've specialized it such that when it reaches the end of a row ot starts on the next and when it reaches the last...
0
by: P. Emigh | last post by:
A client that synchronizes over the internet encountered Error #3003: "Could not start transaction; too many transactions already nested" when attempting to synchronize. I checked user groups...
8
by: Etienne Boucher | last post by:
Nested classes are usualy objects made to only live in their parent object instance. In other words... public class Outter { public class Inner { } }
19
by: (PeteCresswell) | last post by:
I'm going over an application written by somebody else and have encountered what looks to me like nested "With"s. Is this something new with MS Access 2003 or am I losing it? Seems to me that...
1
by: Roy | last post by:
Hey all. Below is the nested syntax on how to make a "codeless" nested gridview embedded within another gridviews templatefield column. Only problem is that it loads slow. REAL SLOW. There has to...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
0
by: G.Waleed Kavalec | last post by:
The nested option for WRITING XML is pretty clear. I have a problem trying to READ nested "tables".... <employer> <acct>1234</> </employees> <employee> <lastname>Smith</> <ssn>123456789</>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.