0

This question has been asked lots and have spent the last 3 days going through a number of different 'solutions', none of which I can get to work.

I have a huge JSON file, some 150k entries, that I want to view as a ListVIew in JQuery Mobile. (I will be using the Filter to actually use the data)

The best I have come up with is this

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/jqm-docs.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>




</head>

<body>
    <div data-role="page">
    <div data-role="content">
        <div id="output">
            <ul data-role="listview" data-inset="true" data-filter="true">

            </ul>
        </div>
    </div>
</div>


<script type="text/javascript">
        //simulating the JSON coming from the server
var json = '["City1","City2","City3"]';
//jQuery getJSON will do this step
var data = $.parseJSON(json);

//and this is your code
$.each(data, function (index, value) {
        $('#output').children('ul').append('<p>'+value+'</p>').listview('refresh');
});

</script> 

</body>
</html>

If I remove the .listview('refresh') then all three JSON entries are listed in the same ListView field. I obviously want them seperated.

Can anyone advise on how to do this?

With thanks

Tim

3 Answers 3

2

In order to use $.parseJSON first you need to have the proper JSON string format

so I guess your variable

var json = '["City1","City2","City3"]';

should look more like:

var json = '{"city":"City1"},{"city":"City2"},{"city":"City2"}';

then before to convert it to JSON, you would rather want to split it first

var jsonSplit = json.split(',');

and convert to JSON every separated part within an array

var data = new Array(), i;
for(i in jsonSplit){
 if(jsonSplit[i].length){ //remove last empty element after .split()
  data[i] = $.parseJSON(jsonSplit[i]);
 }
}

then you can manipulate data as a javascript object

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

Comments

1

You are missing li elements.

$.each(data, function (index, value) {
        $('#output').children('ul').append('<li><p>'+value+'</p></li>').listview('refresh');
});

Comments

1
$.each(data, function (index, value) {
        $('#output').children('ul').append('<li><p>'+value+'</p></li>').listview('refresh');
});

Remove Listview(refresh) based on what you are doing here you don't need a refresh each time. If you are dynamically adding it through say php... You would need to refresh at the end.

$.each(data, function (index, value) {
        $('#output').children('ul').append('<li><p>'+value+'</p></li>');
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.