473,769 Members | 3,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing "nested" rows in a table

127 New Member
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 2756
dmjpro
2,476 Top Contributor
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 New Member
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 Top Contributor
@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 New Member
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 Top Contributor
Now it's Working now or not?
Apr 17 '09 #6
phub11
127 New Member
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 Top Contributor
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 New Member
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
12629
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 variables in another class. Something like: $obj = $factory->getObject("1234");
0
1369
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: Jim Smith One who flew over the clock tower The hunting game
15
2538
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 row it stops.. At least that's what I thought I did, but VC71 says "warning C4717: 'LOOP<0,1>::DO' : recursive on all control paths, function will cause runtime stack overflow". What's wrong? Here's the code: template<int M, int N>
0
4040
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 and MS Knowledge Base and found nothing helpful. This didn't seem to be acknowledged as a synchroization error. Investigations revealed data errors that resulted in a cascade of related errors. Resolving the data errors apparently cured whatever...
8
2835
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
23211
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 when I tried nesting "With" clauses in MS Access 2000 it was a no-no. Some sample code - in which it looks like they're creating/populating a ..XLS spreadsheet. "Option Explicit" is not specified in the module......
1
1974
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 be a better way. Suggestions anyone? By the way, I'm not opposed to coding, it just seems like this should be easily doable on the aspx side of things. Summary: I'm stuffing the 3 three key fields from each row in the master gridview into...
18
3645
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 abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all combinations of bobs has to meet and since the are schizophrenic they can also meet themselves):
0
1086
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</> </employee>
0
9583
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.