【问题标题】:how to refresh jquery mobile listviews如何刷新jquery mobile listviews
【发布时间】:2011-12-03 04:18:00
【问题描述】:

我在更改其中的列表项后尝试刷新listview,但它一直失败并显示消息:

"未捕获不能在初始化之前调用listview上的方法; 试图调用方法“刷新””。

列表是 div 中的无序列表,带有data-role="dialog" 属性。该列表有data-role="listview"。更新后,如果我打电话给$('#theULInQuestion').listview('refresh'),我会收到错误消息。什么给了?

谢谢, 抢

【问题讨论】:

    标签: jquery-mobile


    【解决方案1】:

    jQuery Mobile 框架中有很多页面事件,这使得很难确定哪个是正确的绑定。我绑定到pageshow 事件,因为在该事件触发时,DOM 已准备好(jQuery Mobile 已初始化所有内容)。

    但是,每当我在为 jQuery Mobile 小部件的 refresh 计时时遇到问题时,我都会检查它是否已初始化并运行必要的代码到 initializerefresh 小部件。

    $(document).delegate('#my-dialog-id', '<page-event>', function () {
        var $the_ul = $('#theULInQuestion');
        //add your rows to the listview here
        $the_ul.append('<li>a row</li><li>another row</li>');
        if ($the_ul.hasClass('ui-listview')) {
            $the_ul.listview('refresh');
        } else {
            $the_ul.trigger('create');
        }
    });
    

    每个 jQuery Mobile 小部件都有特定的类添加到该类型的小部件中。因此,您可以检查(在附加行之后)小部件是否具有 jQuery Mobile 类;如果有则refresh它,如果没有则initialize它。

    这是一个 jsfiddle:http://jsfiddle.net/jasper/urTeW/1/

    请注意,您可以将条件语句放在代码中的任何位置refresh/initialize,当page-event 触发时,它不必发生。

    【讨论】:

    • 我似乎根本无法访问页面显示。我在这个jsp中有两个页面。既不加载jsp,也不在页面之间来回切换。我也尝试使用 pagebeforeshow、pagecreate 和 pagebeforecreate,但没有成功。
    猜你喜欢
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多