dcsimg
adding dynamic rows to a table
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Simon_Neaves
Posted On:   Wednesday, December 6, 2006 01:34 PM

I am trying to add new rows to a table using ajax. I have the following javascript function for adding the row, but I need to get the data for the cells using AJAX (instead of the static 'data1-9)': function addOrderLine(){ var tbl = document.getElementById('mytable'); var lastRow = tbl.rows.length; var row = tbl.insertRow(lastRow); arr= new Array('data1','data2','data3','data4','data5','data6','data7','data8','data9'); for (r = 0; r < arr.length; r++) { var cell = row.insertCell(r); cell.innerHTML = arr[r]; } } I need data1-9 to be more complicated. I need the cells to contain various html elments including dynamically generated dropdown menus. eg arr= new Array(' val1   More>>

I am trying to add new rows to a table using ajax. I have the following javascript function for adding the row, but I need to get the data for the cells using AJAX (instead of the static 'data1-9)':



			
function addOrderLine(){ var tbl = document.getElementById('mytable'); var lastRow = tbl.rows.length; var row = tbl.insertRow(lastRow); arr= new Array('data1','data2','data3','data4','data5','data6','data7','data8','data9'); for (r = 0; r < arr.length; r++) { var cell = row.insertCell(r); cell.innerHTML = arr[r]; } }


I need data1-9 to be more complicated. I need the cells to contain various html elments including dynamically generated dropdown menus.


eg


			
arr= new Array(' ', 'data2', etc. );


...but only more complicated, and with serverside generated options.



I am hoping to run some ajax to get the HTML for each cell but I'm not sure how to get the html for each cell seperately...



I'm quite new to AJAX, but so far it seems to me that I have basically two options when getting data: 1) return HTML 2) return XML.



If I return HTML, I can just dump this into the cell with as many bells and whistles as I like. However, I can't work out how I would seperate the data for each of the cells unless I do 9 seperate http requests to 9 seperate pages for the 9 seperate cells.



If I return XML, I can seperate data for each of the cells by using seperate xml tags, however, I can't just dump html inside the XML elements because it will mess up the xml. Does this mean that I have to have seperate xml tags for every bit of data in the html (eg seperate xml tags for every option in the dropdown)? Isn't it gonna be a bit messy trying to convert all these xml tags to create the HTML...



I'm sure there must be a simple answer, but because I'm a bit new to AJAX, I'm not aware of all the options.



Any thoughts or things that I can look into are welcome. Thanks!

   <<Less

Re: adding dynamic rows to a table

Posted By:   Anonymous  
Posted On:   Wednesday, December 13, 2006 09:57 AM

Hi Simon,

My advice if you're new to AJAX and if you're using PHP, will be to execute all the work by a php page server-side and stock all the data in a string and echo it. Or another option will be if you have the php DOMXML installed on your server, to dynamically create an XML file using those methods http://ca.php.net/domxml and return the path and the filename to your xmlFile (you can always use the same) and decompile this XML using Javascript.

Hope it helps.
Email-me if not.

ciao

Re: adding dynamic rows to a table

Posted By:   Anonymous  
Posted On:   Wednesday, December 6, 2006 04:58 PM

I think HTML would be a better option in this case.You need to return the HTML for the complete row instead of each cell.


You can return a string with the complete HTML for the row and that can be directly inserted into the table using Java script (by creating a TR and setting the returned String value to TR.innerHTML)
About | Sitemap | Contact