Danny Brady
Danny Brady

Reputation: 1935

Refresh jQuery mobile listview after ajax

I am trying to refresh a jQuery mobile list view after an ajax post, I have been trying to use the .trigger("create") to do this like so:

<div data-role="content">


<div id="linksHolder" data-role="controlgroup" data-type="horizontal">
    <a id="most-played" href="#" data-role="button" data-mode="mostplayed">Most Played</a>
    <a id="latest-added" href="#" data-role="button" data-mode="latestadded">Latest Added</a>
    <a id="featured" href="#" data-role="button" data-mode="featured">Featured</a>
</div>

@Html.HiddenFor(model => model.Mode)
<ul class="video-list" data-role="listview" data-divider-theme="a" data-inset="true"></ul>

</div><!-- /content -->


<script class="videoTemplate" type="text/x-jQuery-tmpl"> 
    <li data-theme="c">
        <a href="${LinkToVideo}">
            <img src="${ThumbnailPath}" alt="video 1" />
            <div class="title">${Title}</div>
            <div class="description">${Description}</div>
            <div class="additional-details">
                <b>Category:</b> ${Category}<br />
                <b>Contributor:</b> ${Contributor}
            </div>
        </a>
    </li>  
</script>

<script type="text/javascript">
    DrawPageContent();

    // function to redraw the page content with the mode passed
    $(document).on("click", "#linksHolder a", function () {
            //alert("Inside link");
            var mode = $(this).attr("data-mode");
            $("#Mode").val(mode);
            DrawPageContent();
    });

    // Renders the JSON data into HTML and displayed through a jQuery template
    function DrawPageContent() {
        var mode = $("#Mode").val();
        var jsonUrl = "/mobile/GetVideos?mode=" + mode;

        $.ajax({
            'async': false,
            'global': false,
            'url': jsonUrl,
            'dataType': "json",
            'success': function (data) {
                // Render the videos using the template
                $(".video-list").html($(".videoTemplate").tmpl(data));
                $(".video-list").trigger("create");
            }
        });
    }
</script>

I also tried using $('.video-list').listview('refresh'); but this didn't work either. It is refreshing the JSON data fine, but it is not applying the jquery mobile CSS classes, thus I am losing the listview styles. Any thoughts??

Thanks

Upvotes: 2

Views: 10767

Answers (2)

Danny Brady
Danny Brady

Reputation: 1935

Solution to this was that DrawPageContent() was not being called when the document was ready. Once this was changed I could use .listview("refresh"):

<script type="text/javascript">
$(function () {
    DrawPageContent();
});

$(document).on("click", "#linksHolder a", function () {
    var mode = $(this).attr("data-mode");
    $("#Mode").val(mode);
    DrawPageContent();
});

function DrawPageContent() {
    var mode = $("#Mode").val();
    var jsonUrl = "/mobile/GetVideos?mode=" + mode;
    $.ajax({
        'async': false,
        'global': false,
        'url': jsonUrl,
        'dataType': "json",
        'success': function (data) {
            // Render the videos using the template
            $(".video-list").html($(".videoTemplate").tmpl(data));
            $(".video-list").listview("refresh");
        }
    });
}

Thanks for all the input.

Upvotes: 4

Hkachhia
Hkachhia

Reputation: 4539

I think you can use id instead of class because you can use this class in multiple control so try id of your tag as per given below

<ul id="vdo_list" class="video-list" data-role="listview" data-divider-theme="a" data-inset="true"></ul>

$("#vdo_list").listview('refresh');

Upvotes: 0

Related Questions