GetListItems: Undefined variables

Apr 7, 2011 at 9:32 PM

 

I have to admit that I'm kind of a JavaScript/jQuery newbie so I apologize if this seems awfully basic.

All right, got a weird yet simple one for you experts. What I'm doing is using GetListItems to set a bunch of variables from an InfoPath list, then styling the text and outputting to a Web Part Page. Where I run into problems is when one of the fields is left blank. Because the field is blank, the XML completely removes the attribute from the row. For example:

<z:row ows_Summary="This is the summary." />

If the Summary field is left blank, the ows_Summary attribute vanishes from the XML, which means my code which is attempting to set the "itemSummary" variable ends up with an undefined value. Normally this wouldn't be a problem, but because these are multi-line text boxes with users typing in carriage returns, I need to use a regex to replace these carriage returns with br tags so that they will display correctly. Observe:

var itemSummary = $(this).attr("ows_Summary").replace(/\n/g,"<br>");

This works perfectly when the Summary field is filled out. Instead of the carriage returns being formatted as blank spaces, they are replaced with actual line breaks. The problem is that the .replace() regex bombs out if the attribute is nonexistent, erroring out my code completely. I've determined a workaround:

var itemSummaryFlat = $(this).attr("ows_Summary");
 if (itemSummaryFlat != undefined){
  var itemSummary = itemSummaryFlat.replace(/\n/g,"<br>");
 }
It just seems so sloppy to have to do this for every single one of my variables that needs to replace carriage returns - and there are quite a few. Am I missing something that would allow me to put this all in one line of code?

Apr 8, 2011 at 5:27 PM
Edited Apr 8, 2011 at 5:29 PM

All right, I have a new workaround that isn't quite as sloppy:

 

var itemSummaryFlat = " " + $(this).attr("ows_Summary");
var itemSummary = itemSummaryFlat.replace(/undefined/,"").replace(/\n/g,"<br>");

 

The blank space in itemSummaryFlat prevents the entire variable from being undefined - it just makes it the plain text "undefined," removed in the next line - which allows the regex to run without blowing up my code.

Would still like to know if I'm missing something vital/simple.

Coordinator
Apr 8, 2011 at 6:10 PM

CMK

XML's sparseness can be a little annoying, but it makes it efficient.

My suggestion would be to create a little function that does the conversion for you. It would take an attribute name or value and return the right value regardless.  Then you could do something like:

var itemSummary = convertMyValue($(this).attr("ows_Summary"));

Make sense?

M.

Apr 8, 2011 at 8:01 PM
Edited Apr 8, 2011 at 8:01 PM

As I said, I'm no expert - and I have no experience writing functions. I'm familiar with the purpose of functions, just not how to write them. I think your suggestion would save a lot of space, though.

Coordinator
Apr 8, 2011 at 8:32 PM

Something like this:

function convertMyValue(s) {
  if (s != undefined) {
    return s.replace(/\n/g,"<br>");
  } else {
    return "";
  }
}

So

var itemSummary = convertMyValue($(this).attr("ows_Summary"));

M.

Apr 8, 2011 at 9:02 PM

Beautiful. Learning every day. Thanks Marc, that should clean up my code significantly. Guess I need to get a hold of an "Intro to JavaScript" book.

Coordinator
Apr 8, 2011 at 9:03 PM

You bet. Nice little diversion form the ridiculously complex XSL I'm writing right now. ;+)

M.