Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-07-2012, 08:04 AM   PM User | #1
sunsin
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
sunsin is an unknown quantity at this point
xml data in a columns & row not just columns

Hi,
I'm loading xml data in a table which is in a div. I want each item to load in it's own column with only 3 columns per row. I can get the data to load in it's own column but only one column per row. I can also get it to load only three items from the xml file and then goto the next line but then I can't get them to load in columns.

Please help.
Here is my code:

Code:
function displayDOMlist(){
document.getElementById("cat_title").innerHTML="<img src='img/beer_dom_tl.png'>";
  x=xmlDoc.getElementsByTagName("BEER");
  t=document.getElementById("beerlist");
  t.innerHTML="<tr>";
  mltple=1;
  for (i=0;i<x.length;i++){
    naam=(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue);
    info=(x[i].getElementsByTagName("INFO")[0].childNodes[0].nodeValue);
    pic=(x[i].getElementsByTagName("PIC")[0].childNodes[0].nodeValue);
	if (mltple % 3){
		t.innerHTML+="<td width='100'><a href="+pic+">"+naam+"</a></td>";		
    } else {
    t.innerHTML+=naam+"</tr><tr>";
    }
    mltple++;    
  }
sunsin is offline   Reply With Quote
Old 08-07-2012, 01:19 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I haven't looked at this in detail but assume you need:

Code:
} else {
    t.innerHTML += "<td>" + naam + "</td></tr><tr>";
}
But you also need to finish the table by removing the extra <tr>. Instead of inserting it directly as innerHTML, perhaps collect it in a string:

Code:
var tblContent = '';

tblContent += // etc..;

tblContent = tblContent.substring(0, tblContent.length-5) + '</table>';
but this is untested.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 08-07-2012 at 01:21 PM..
AndrewGSW is offline   Reply With Quote
Old 08-07-2012, 03:39 PM   PM User | #3
sunsin
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
sunsin is an unknown quantity at this point
Thanks AndrewGSW,

I added 't.innerHTML += "<td>" + naam + "</td></tr><tr>";' but its still not working. I'm relatively new to javascript (and/or html), I don't understand what you mean by the following.


Quote:
Originally Posted by AndrewGSW View Post

Code:
var tblContent = '';

tblContent += // etc..;

tblContent = tblContent.substring(0, tblContent.length-5) + '</table>';
but this is untested.
sunsin is offline   Reply With Quote
Old 08-07-2012, 04:08 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Your current code ends up with an additional opening tr tag at the end, which might be causing the whole thing to collapse into one column. My code should remove this extraneous opening tag.

I've revised it here as I assume there is already a closing table tag somewhere.

Code:
var tblContent = '';

tblContent += // etc..;

tblContent = tblContent.substring(0, tblContent.length-5);
But this might not be the problem; you may just be inserting the content in the wrong place.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-07-2012, 04:39 PM   PM User | #5
sunsin
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
sunsin is an unknown quantity at this point
This is what i've in the html.

Code:
<table id="beerlist" border='1'></table>
sunsin is offline   Reply With Quote
Old 08-07-2012, 04:42 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by sunsin View Post
This is what i've in the html.

Code:
<table id="beerlist" border='1'></table>
That's fine, but I would put at least a word in there as a placeholder:

Code:
<table id="beerlist" border='1'>beers</table>
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-07-2012, 04:59 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You also need to ensure the href is quoted within the HTML:

Code:
t.innerHTML+="<td width='100'><a href='"+pic+"'>"+naam+"</a></td>";
// OR
t.innerHTML+="<td width='100'><a href=\""+pic+"\">"+naam+"</a></td>";
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:09 PM.


Advertisement
Log in to turn off these ads.