Help with dynamic rows in table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • truptivk
    New Member
    • Sep 2009
    • 14

    Help with dynamic rows in table

    Hello all,

    I have a question and I'd be glad if any of you could help me :) I have written code to add rows dynamically to an existing table. When the user first comes to the page, there are 2 rows in the table already. If he so wishes, he can add additional rows. Whilst creating the rows, I have set the 'id' and 'onchange' attributes.

    Now when he comes back to this page after going forward 1 page, the table is not loading with all values. Here's the problem. It only loads the first 2 rows, a.k.a, the rows which were present static in the table.

    Any ideas on why is this happening, and how can I get the table to load all rows?

    I have pasted relevant sections of my code below:

    Code:
    /* **** Code Start **** */
    
    /* function to add rows to the table dynamically */
    function addRow(tableID) {  
       
     this.count++;
     	
     var table = document.getElementById(tableID);
    	
     var rowCount = table.rows.length;
    	
     var row = table.insertRow(rowCount);
            
     var cell1 = row.insertCell(0);  
     var element1 = document.createTextNode("Provider:");  
     element1.type = "text";
     cell1.appendChild(element1);
        
     var cell2 = row.insertCell(1);
     var provID = document.createElement("select");
     provID.setAttribute('id', ('document.circuits.providers' + (rowCount - 2)));
     name = provID.setAttribute("onchange", 'provider(this);');
     loadPopup (provID, Organisations, "a Circuit Provider");  //funciton to populate the select element
    cell2.appendChild(provID);
        
    alert("the id is: " + provID.id); //correctly prints document.circuits.providers1 
    alert("provID: " + provID); // correctly prints HTMLSelectElement
    }
    
    /* function to load the page on entry */
    function loadPage ()
         {
    	 
    	 
    	 var t = "";
    	 
    // it comes to this part when user comes back to this page
    if ((top.providers).length > 0) {
         	
         	for (i = 0; i < (top.providers).length; i++) {
         		var y = 'document.circuits.providers' + i;
         		alert("y is: " + y);
         		t = eval('document.circuits.providers' + i); // works with only 0
         		alert("now t is: " + t);  //'t' prints as 'undefined'
            	top.providerNames[i] = top.LoadPopup (t, Organisations, "a Circuit Provider", top.providers[i]);
         	}
         }
         else { // comes to this part when user first comes to this page
         	
         	// provide for one circuit provider info to be entered - user can add more if he needs
         	
       		top.allProviderIDs = "";
         		top.allProviderNames = "";
         		
         		t = eval('document.circuits.providers' + 0);
         		alert("t is: " + t);     		
         		top.providerNames[0] = top.LoadPopup (t, Organisations, "a Circuit Provider", top.providers[0]);		
         			
         }
    }
    
    <body onload="loadPage();">
    
    <div id="mainbody">
    
    <form>
    <table>
    <!-- etc etc -->
    </table>
    </form>
    </div>
    </body>
    </html>
    
    /* **** Code End **** */
    There's no syntactical error so I know the code is fine. Just the id on revisiting the page is messed up. Please help, I'd really appreciate it.

    Thanks!
    - Trupti
    Last edited by gits; Sep 1 '09, 11:50 AM. Reason: added code tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Do you mean that when a user is using the back button in the web-browser, the original 2 rows are being shown? If so, that's because the browser is caching what is first loaded by the browser, i.e. those 2 rows. What you add after the page load, dynamically, is not cached.

    You could overcome this by setting a cookie that stores information such as the amount of rows and the data stored by the rows. There may be a more elegant solution to this, though.

    Mark.

    Comment

    • truptivk
      New Member
      • Sep 2009
      • 14

      #3
      Hello Mark,

      Thanks for your reply. No I don't mean the browser's back button. I have buttons on my page to move back and forward.

      - Trupti

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        but is that an entire page-reload during the operations? JavaScript-variables don't persist after unloading a page ... but you might pass the values from page to page

        Comment

        • truptivk
          New Member
          • Sep 2009
          • 14

          #5
          Gits,

          Yes it is a reload of values chosen the first time around. I have a way of passing the values from page to page; whats missing is how to get to display the values previously chosen in the dynamically added rows. And yes, when I come back to the page, I have the values. I just need a way of displaying them.

          When adding rows dynamically for the first time, if you see the code, I have used setAttribute to set the id to 'document.circu its.providers' + (rowCount-2) - which will be document.circui ts.providers1, document.circui ts.providers2 etc. document.circui ts.providers0 is the static row, which is set in loadPage().

          Problem is - when I come back to same page, I need to reconstruct the table. I can show the row with id document.circui ts.providers0, but not dynamically added ones like document.circui ts.providers1, document.circui ts.providers2 etc. Once again, to avoid confusion, I would like to say that I have the values that need to be shown in document.circui ts.providers1, document.circui ts.providers2 etc. Building the table with the id is proving a hurdle.

          Hope I'm clear :) Thanks for all your patience.

          - Trupti

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            i just guess that you add the rows with the addRow() method of your posted code? ... so basicly you would need to call that method and add the corresponding values to the created cells? it seems that you loop already through the data in question ... so just pass the values to the function and add it during the creation/add-process in addRow()

            aah ... and where does the tableID come from?

            Comment

            Working...