Joe
Joe

Reputation: 1354

trouble styling dynamically created jquery mobile listview

I am trying to generate a jQuery mobile listview on the fly by pulling down movie data from TMDb and constructing each <li> and appending it to an already existing <ul> element. trouble is, is that I can't get it for format properly no matter what i do. I'm getting the data and everything from TMDb just fine but the list view isn't styling itself when i call $("#ul").listview("refresh"); here is some of my code ( the relevant stuff):

AJAX CALL THAT GRABS DATA AND BUILDS EACH <li>:

    $.ajax({
        type: "POST",
        data: {
            action: 'searchFilm',
            film: film
        },
        dataType: "json",
        url: "./php/functions.php",
        success: function(json) {
            console.log(json);
            for(var i = 0;  i < Object.size(json); i++){
                var film = json[i.toString()];
                $('#film-search-results').append("<li data-theme='j'><img src='" + film.poster_path + "' alt='Poster' /><a href=''><h3 class='ui-li-heading'>" + film.title + "</h3><p class='ui-li-desc'><strong>" + film.tagline + "</strong></p><p class='ui-li-desc'>" + film.overview + "</p></a></li>");
            }
        },
        error: function(jqxhr, status, text){
            console.log(jqxhr);
        }
    });

EXISTING CONTENT DIV THAT HOLDS THE <ul> ELEMENT:

 <div id="content-add-title" data-role="content">
    <ul id='film-search-results' data-role='listview'></ul>
 </div><!-- end content-add-title -->

AND THE FUNCTION THAT CALLS THE AJAX METHOD:

    $('#add-add-title-btn').click(function() {
        if ($('#add-title-type-select').val() === "movie") {
            $('#right-panel3').panel("close");
            $('#film-search-results').html("");
            searchFilms($('#add-title-name-field').val());
            $('#add-title-page').trigger("create");
            $('#film-search-results').listview("refresh");
        }
    });

really not sure why it won't style properly any one have any ideas?

Upvotes: 1

Views: 449

Answers (1)

Ross
Ross

Reputation: 3330

You need to refresh the listview in the success of your ajax call, not in your click event handler -

success: function(json) {
    console.log(json);
    for(var i = 0;  i < Object.size(json); i++){
        var film = json[i.toString()];
        $('#film-search-results').append("<li data-theme='j'><img src='" + film.poster_path + "' alt='Poster' /><a href=''><h3 class='ui-li-heading'>" + film.title + "</h3><p class='ui-li-desc'><strong>" + film.tagline + "</strong></p><p class='ui-li-desc'>" + film.overview + "</p></a></li>");
    }
    $('#add-title-page').trigger("pagecreate"); // <- trigger pagecreate here, shouldnt need listview refresh
},

I'm guessing that your ajax call is inside of the searchFilms function. When you call $('#film-search-results').listview("refresh"); and $('#add-title-page').trigger("create"); immediately after your call to searchFilms your ajax call isn't complete yet and hasn't populated the listview with the JSON response. This is why it is not working.

Upvotes: 1

Related Questions