0

Before anyone points me to similiar questions here on stackoverflow, I have searched for and read and tried all the solutions that have been provided to no avail. By the way I understand it one would have an empty list like so:

<ul data-role='listview' data-theme='b' data-inset='true' id='resultsList'>
   <!-- keep empty for dynamically added items -->
</ul>

Then in your javascript/jquery you'll go like this(bearing in mind I get my data from a JSON object):

var resultLength = jsonObject.results.length;
var listItems = [];

for(var i=0;i<resultLength;i++){
   var firstname = jsonObject.results[i].FN;
   var lastname = jsonObject.results[i].SN;

   //Add result to array
   listItems.push("<li><a href='#'>"+firstname+" "+lastname+"</a></li>");
}

//Append array to list and refresh
$('#resultsList').append(listItems.join(' '));
$('#resultsList').listview('refresh');

But... It doesn't work. I'm new to jquery so it might be a real stupid mistake I'm making somewhere. If anyone has an idea of why this isn't working I'd appreciate it! Been struggling with this for 2days now. If any more information is required, please don't hesitate to ask.

Edit - Added JSON as requested

{"results":
[
 {   "GUID":"F45B0504-376C-4772-81C2-B920E23E1332",
     "I#":"4202265241081",
     "FN":"JOHN",
     "DOB":"1942-02-26",
     "SEX":"M",
     "SN":"KHUNOU",
     "U#":"VH00041750",
     "TYPE":"P"},

{    "GUID":"BB69F24F-424A-4B10-8A48-E94197894855",
     "I#":"3909035071082",
     "FN":"JOHN E",
     "DOB":"1939-09-03",
     "SEX":"M",
     "SN":"RIVETT CARNAC",
     "U#":"VH00156354",
     "TYPE":"P"
}
],
"ok":"true"}
7
  • 2
    is this typo? var listItems [] Commented May 25, 2012 at 6:51
  • I think NiftyDude has it (should be var listItems = []), but otherwise is your code in a document ready handler? What do you get if you try alert(listItems.join(' ')) just after the loop? Commented May 25, 2012 at 6:54
  • @NiftyDude Sorry yes that was a typo in my question. Fixed it now, thanks for pointing that out. Commented May 25, 2012 at 7:04
  • @DeanGrobler do some debugging, see what console.log(jsonObject) yield, see what console.log(listItems.join(' ')) yield. Otherwise your code looks ok Commented May 25, 2012 at 7:07
  • @nnnnnn I get the proper code I'd expect to get added e.g. "<li href='#'>John Smith</li> <li href='#' >Brandy Whiskers</li>". Also my code is in a javascript function called 'searchSubmission()' that gets referenced on a button click. Commented May 25, 2012 at 7:07

1 Answer 1

1

Your code looks fine and taking the JSON out of the equation shows that it works: http://jsfiddle.net/b5h5Y/

Since the code works, maybe post a sample of the JSON itself as that, or the way you are accessing it, seems to be the weak spot?

Sign up to request clarification or add additional context in comments.

4 Comments

That's for the effort of testing it in jsfiddle, I appreciate that. The JSON seems to work perfectly though. If I do an alert(listItems.join(' ')); before I append to the list, I get the expected result being the LI tags with the respected data inside each...
Okay I thought I got it working there for a second. I removed this piece of code: $('#resultsList').listview('refresh'); and it worked! Then I thought I'll run it again for a second time and it didn't work?? Doesn't make any sense!
@DeanGrobler Post the JSON. Or part of it anyway. Stuff in some fake data if necessary.
It works perfectly as is, DeanGrobler. Check it here: jsfiddle.net/b5h5Y/1 The code you present works without issue. Since your code works and your JSON is formatted correctly and being parsed correctly the problem must exist elsewhere. Check to see what the value of jsonObject is console.log(jsonObject).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.