4

I have an array from ajax and i need to create jQuery Mobile Listview. I don't found a method for this, so is it possible?

Gajotres
  • 57,309
  • 16
  • 102
  • 130
user1692333
  • 2,461
  • 5
  • 32
  • 64

1 Answers1

8

Here's a working example: http://jsfiddle.net/Gajotres/SS7vJ/

And another example with an array: http://jsfiddle.net/Gajotres/yHHWQ/

$(document).on('pagebeforeshow', '#index', function(){       
    $('<ul>').attr({'id':'test-listview','data-role':'listview', 'data-filter':'true','data-filter-placeholder':'Search...'}).appendTo('#index [data-role="content"]');
    $('<li>').append('<a href="#">Audi</a>').appendTo('#test-listview');
    $('<li>').append('<a href="#">Mercedes</a>').appendTo('#test-listview');
    $('<li>').append('<a href="#">Opel</a>').appendTo('#test-listview');
    $('#test-listview').listview().listview('refresh');
});

Also don't forget to call .listview( twice, first without refresh parameter, and second time with a refresh parameter. Without it you will receive this error:

cannot call methods on listview prior to initialization

If you want to find out more about how jQuery mobile handles dynamically added content and its markup take a look at this ARTICLE, to be transparent it is my personal blog, or find it HERE.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • 2
    also found other method, looks like `.append(sites).trigger('create');` – user1692333 Mar 29 '13 at 17:59
  • I would advise you against trigger('create') cause it is resource heavy. While listview('refresh') will style only listview, trigger('create') will restyle whole page content. Read about that in a link at the end of my answer. – Gajotres Mar 29 '13 at 18:02