在ajax之后刷新jQuery mobile listview

时间:2012-07-20 09:12:58

标签: listview jquery jquery-mobile

我试图在ajax帖子之后刷新jQuery移动列表视图,我一直在尝试使用.trigger(“create”)这样做:

<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>

我也尝试过使用$('。video-list')。listview('refresh');但这也行不通。它正在刷新JSON数据,但它没有应用jquery移动CSS类,因此我失去了listview样式。有什么想法??

由于

2 个答案:

答案 0 :(得分:4)

解决方法是在文档准备好时没有调用DrawPageContent()。一旦改变了,我就可以使用.listview(“刷新”):

<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");
        }
    });
}

感谢您的所有输入。

答案 1 :(得分:0)

我认为您可以使用id而不是类,因为您可以在多个控件中使用此类,因此请按照下面的说明尝试标记的ID

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

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