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

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 02-19-2013, 03:06 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Having trouble with this logic. Where are the smart people?

Hello,

I am having problems with something that seemed really simple at first. I have an XML file (I am not including the entire file, it IS properly formed in its entirety). This file holds data for a glossary. What I am trying to do is populate a div with these terms. I can get it to show all of the terms, what I am having problems with is the 'A', 'B', 'C',.... title at the beginning of each block of terms. In other words, when the 'B' terms begin I want a row that says 'B'. When a letter is not present in the XML (no 'Y' words) I still want the 'Y' title to appear.

The code below is what I have so far and it works for the most part but I have redone it a few times and I am lost. Can someone please help me out here?

Code:
<item term="AAA" def="Not the American Automobile Association" rollover="Anti-aircraft Artillery" />

<item term="AC" def="Alternating Current" />

<!-- B -->

<item term="BAS" def="Building Automation System" />


<item term="BBC" def="British Broadcasting Company" />


<!-- C -->



<!-- D -->

<item term="DARPA" def="Defense Advanced Research Projects Agency" />


<item term="dB" def="Decibel" />
Code:
	function parseGlossXML(xml)
	{
		glossaryXML = xml;
		
		var thisLetter = $(glossaryXML).find(':first').attr('term').substr(0,1).toUpperCase();
		var letterArr2 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
		var letterArrIndex = 0;
		var continueLoop = true;
		
		for(var t = 0; t < letterArr2.length; t++)
		{
			if(thisLetter == letterArr2[letterArrIndex])// A == A
			{alert(thisLetter);
				$("#glossaryContent").append("<br /><span class='glossAcro'>" + "wtf1 " + thisLetter + "</span><br />");
				$(glossaryXML).find('item').each(function()
				{			
					if(continueLoop)
					{
						//$("#glossaryContent").append("<br /><span class='glossAcro'>" + thisLetter + "</span><br />");
						//letterArrIndex++;
						continueLoop = false;
					}
					else if(!continueLoop)
					{
						thisLetter = $(this).attr('term').substr(0,1).toUpperCase();
						//$("#glossaryContent").append("<br /><span class='glossAcro'>" + thisLetter + "</span><br />");
						//letterArrIndex++;
						
					}
					$("#glossaryContent").append("<br /><span class='glossAcro'>" + $(this).attr('term') + "</span>" + "<span class='glossDef'>" + $(this).attr('def') + "</span><br />");
					//letterArrIndex++;
				});
				letterArrIndex++;
			}
			else if(thisLetter != letterArr2[letterArrIndex])
			{
				$("#glossaryContent").append("<br /><span class='glossAcro'>" + "WTF2 " + letterArr2[letterArrIndex] + "</span><br />");
				letterArrIndex++;
			}
		}
}
m2244 is offline   Reply With Quote
Old 02-19-2013, 08:19 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What was wrong with the answer I gave you in your other thread?

http://www.codingforums.com/showthread.php?t=287631

If it didn't work, at least respond in that same thread and say why not and/or what happened.

If it did work, it would still be nice to get a response.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 02-19-2013 at 08:21 PM..
Old Pedant is offline   Reply With Quote
Old 02-19-2013, 08:33 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, I see the difference. Now you want the letter to appear even if there are no matches in the XML feed.

See, that would have been *SO EASY* for you to have said in response to my prior post. But for all I know, you didn't bother to read my prior post.

This looks trivial. Not much harder than the other problem, where I purposely omitted the missing letters.

Code:
    var curLetter = "@";
    $(glossaryXML).find('item').each(function()
    {
        var first = ($(this).attr('term').charAt(0).toUpperCase();
        while ( first > curLetter )
        {
            // bump the curLetter by one (NOTE: "@" + 1 ==>> "A")
            curLetter = String.fromCharCode( curLetter.charCodeAt(0) + 1 );

            if ( curLetter > "Z" ) break; // don't go past last letter (shouldn't happen)

            // output a header for curLetter
            $("#glossaryContent").append(... curLetter ... ); 
        }
        $("#glossaryContent").append(...); // output from XML
    } );
Maybe you could actually respond, this time?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 02-19-2013 at 08:35 PM..
Old Pedant is offline   Reply With Quote
Old 02-19-2013, 09:13 PM   PM User | #4
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Pedant,

I appologize. I forgot I posted that on Thursday. Around 3:30 that day someone found a problem so I had to jump through hoops correcting it and for got about the post.

Thanks for the help, I will test it now and get the working code back here.
m2244 is offline   Reply With Quote
Old 02-19-2013, 09:21 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by m2244 View Post
... I had to jump through hoops correcting it and for got about the post.
Well, that has certainly never happened to me. Today.

And I've never forgotten a post. In the last hour.

<grin/>

Understood. All's good. Hope this one works.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-19-2013, 09:31 PM   PM User | #6
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Pedant you son of a gun, it works.

Code:
var curLetter = "@";
		$(glossaryXML).find('item').each(function()
		{
			var first = ($(this).attr('term').charAt(0).toUpperCase());
			while ( first > curLetter )
			{
				// bump the curLetter by one (NOTE: "@" + 1 ==>> "A")
				curLetter = String.fromCharCode( curLetter.charCodeAt(0) + 1 );

				if ( curLetter > "Z" ) break; // don't go past last letter (shouldn't happen)

				// output a header for curLetter
				$("#glossaryContent").append("<span class='glossLetter'>" + curLetter + "</span>"); 
			}
			$("#glossaryContent").append("<span class='glossAcro'>" + $(this).attr('term') + "</span>" + "<span class='glossDef'>" + $(this).attr('def') + "</span>"); // output from XML
		} );
m2244 is offline   Reply With Quote
Old 02-19-2013, 09:36 PM   PM User | #7
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Nope. I missed something. The last 3 letters (x, y, z) do not show up. Let me play around with it.
m2244 is offline   Reply With Quote
Old 02-19-2013, 10:02 PM   PM User | #8
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
There is probably a more elegant way to do this but here is my fix. I put thisafter the 'find' loop above:

Code:
while(curLetter.charCodeAt(0) < 90)
		{
			curLetter = String.fromCharCode( curLetter.charCodeAt(0) + 1 );
			$("#glossaryContent").append("<span class='glossLetter'>" + curLetter + "</span>");
		}
m2244 is offline   Reply With Quote
Old 02-19-2013, 10:27 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No, I think that's a fine way to do it.

It's as short as I think you could make the code.

Only thing I (personally) might change:
Code:
while ( curLetter <= "Z" )
Just to be consistent with the other code. But it truly doesn't matter in the least.

Great!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant 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 07:32 AM.


Advertisement
Log in to turn off these ads.