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" />
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..
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.
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
} );