listview - ajax后刷新jQuery移动 ListView

标签 listview jquery jquery-mobile

我正在尝试在 ajax 发布后刷新 jQuery 移动 ListView ,我一直在尝试使用 .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 mobile CSS 类,因此我丢失了 listview 样式。有什么想法吗??

谢谢

最佳答案

解决这个问题的方法是当文档准备好时没有调用 DrawPageContent()。一旦更改,我就可以使用 .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");
        }
    });
}

感谢您的所有意见。

关于listview - ajax后刷新jQuery移动 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11576360/

相关文章:

android - 如何在android中的listview中删除复选框的焦点

javascript - 如何使用 JQuery 从 Laravel 5.2 中的某些复选框中获取选定的复选框 ID

php - javascript将图像调整为全屏并保持纵横比

javascript - jQuery Mobile 导航选项卡

jquery - 将 JQM 滑动事件添加到 ListView 链接

android - 根据参数在 ListView 中显示项目

android - 自定义 ListView 上的 setOnItemClickListener

android - 按需显示/隐藏 Listview SectionIndex

Javascript\Jquery 如何检测函数是否仍在运行?或者我可以 Hook 返回事件吗?

javascript - 模拟非 http url 的 .click()