Jquery mobile listview - check initialization is complete

前端 未结 4 707
野性不改
野性不改 2020-12-11 15:24

I\'ve coded my way into a corner.

I need to call listview(\"refresh\") on a list, however it may not have been initialized at the point I am calling the

相关标签:
4条回答
  • 2020-12-11 15:53

    Try

    $list.listview();
    $list.listview('refresh');
    
    0 讨论(0)
  • 2020-12-11 15:57

    you can simply create a global variable

    var is_mobile_init = false;

    at the top of your scripts. then set it to true after you include jquery mobile.

    then before refreshing write,

    if (is_mobile_init)
        $myUL.listview('refresh');
    

    this way, you don't need to keep track of all the various classes and it is intuitive

    0 讨论(0)
  • 2020-12-11 15:59

    When a listview widget is initialized, it's given the ui-listview class, so we can test for this class to see if it has been initialized:

    //select the listview
    var $myUL = $('#my-ul-element');
    
    //add a list-item to the listview
    $myUL.append('<li>I\'m New!</li>');
    
    //check if the listview has the ui-listview class
    if ($myUL.hasClass('ui-listview')) {
    
        //this listview has already been initialized, so refresh it
        $myUL.listview('refresh');
    } else {
    
        //this listview has not yet been initialized, so it gets initialized
        $myUL.listview();//or you can use .trigger('create');
    }
    

    This should help alleviate the error you're getting.

    Also, the .hasClass('[class]') function returns true/false if the element has the [class]: http://api.jquery.com/hasClass

    0 讨论(0)
  • 2020-12-11 16:09

    For me the solution to this message was to add data-role="listview" to my ul element. I could then use the method .listview('refresh') in my event handlers.

    0 讨论(0)
提交回复
热议问题