0

I used Json to get data off a site build in Wordpress (using the Json API plugin). I'm using jQuery mobile for the layout of the application in Phonegap. Getting the data to display in Phonegap wasn't the hardest thing to find (code below). But, is it possible to make a list of the titles of different posts and linking them to the specific article and loading the content in a page? In PHP you could just use an argument but is there a way to make something like this work in jQuery mobile?

Here's code I used. Also handy if someones happens to come across this post using google.

    <script>          
        $(document).ready(function(){
            var url="http://127.0.0.1:8888/wp/api/get_recent_posts";
            $.getJSON(url,function(json){
                $.each(json.posts,function(i,post){
                    $("#content").append(
                    '<div class="post">'+
                    '<h1>'+post.title+'</h1>'+
                    '<p>'+post.content+'</p>'+
                    '</div>'
                    );
                });
            });
        });
    </script> 

EDIT:

I'd like to thank shanabus again for helping me with this. This was the code I got it to work with:

    $(document).ready(function() {

        var url="http://127.0.0.1:8888/wpjson/api/get_recent_posts";
        var buttonHtmlString = "", pageHtmlString = "";

        var jsonResults;

        $.getJSON(url,function(data){
            jsonResults = data.posts;
            displayResults();       
        });

        function displayResults() {

            for (i = 0; i < jsonResults.length; i++) {
                buttonHtmlString += '<a href="#' + $.trim(jsonResults[i].title).toLowerCase().replace(/ /g,'') + '" data-role="button">' + jsonResults[i].title + '</a>';
                pageHtmlString += '<div data-role="page" id="' + $.trim(jsonResults[i].title).toLowerCase().replace(/ /g,'') + '">';
                pageHtmlString += '<div data-role="header"><h1>' + jsonResults[i].title + '</h1></div>';
                pageHtmlString += '<div data-role="content"><p>' + jsonResults[i].content + '</p></div>';
                pageHtmlString += '</div>';
            }

            $("#buttonGroup").append(buttonHtmlString);
            $("#buttonGroup a").button();
            $("#buttonGroup").controlgroup();
            $("#main").after(pageHtmlString);
        }

    });
Dirk
  • 3
  • 1
  • 3

1 Answers1

3

Yes, this is possible. Check out this example: http://jsfiddle.net/shanabus/nuWay/1/

There you will see that we take an object array, cycle through it and append new buttons (and jqm styling). Does this do what you are looking to do?

I would also recommend improving your javascript by removing the $.each and substituting it for the basic for loop:

for(i = 0; i < json.posts.length; i++)

This loop structure is known to perform better. Same with the append method. I've heard time and time again that its more efficient to build up a string variable and append it once rather than call append multiple times.

UPDATE

In response to your comment, I have posted a new solution that simulates loading a Json collection of content objects to dynamically add page elements to your application. It also dynamically generates the buttons to link to them.

This works if you do it in $(document).ready() and probably a few other jQM events, but you may have to check the documentation on that or call one of the refresh content methods to make the pages valid.

http://jsfiddle.net/nuWay/4/

Hope this helps!

Community
  • 1
  • 1
shanabus
  • 12,989
  • 6
  • 52
  • 78
  • 1
    Thank you for your response. I didn't know that using a loop would be better for performance. I'll be using that instead the append method. Thanks for the tip! But, I don't think your example was what I was looking for. I was trying to make a list of links with the titles linking to a new div/section with a data-role="page" attribute and a div with a data role of content within it. So I could dynamically load in post titles and linking them to their content within jQuery mobile. But it's pretty late now. I'll try again in the morning perhaps with fresh inspiration. – Dirk Feb 01 '12 at 22:02
  • @Dirk, please see update. This should give you a good start for dynamically adding pages to your app. Also, the `for` loop advantage may not be seen on small lists, it may not be a bid deal for your situation depending on how many times your looping. I just added that because when it comes to mobile apps, every bit of performance helps. – shanabus Feb 02 '12 at 03:44
  • Wow! Thank you so much for taking the time helping me with my issue. This was just what I was looking for. Cheers! – Dirk Feb 02 '12 at 08:28
  • I edited my post again with the final code. Could be handy for fellow googlers looking around on the web. And thanks again. You've been a great help. – Dirk Feb 02 '12 at 10:01
  • If my answer solves the question, would you kindly mark my answer? Thanks @Dirk! – shanabus Feb 02 '12 at 13:52