Refresh jquery mobile listview after ajax call in complete function doesn't work

。_饼干妹妹 提交于 2019-12-05 16:27:31

Listview refresh should be executed after ul element has been populated, so do it like this:

items='';    
$.each(data.pois, function() {
    items += '<li><a href="#"><h3>'+this.name+'</h3></a><a href="tel:\''+this.tel+'\'" >call</a></li>';
});

$('#listview1').html(items);
$('#listview1').listview('refresh');

This way you can be sure that listview is going to be refreshed only after content has been appended.

EDIT :

Problem mentioned in the comment can be solved like this:

items='';    
$.each(data.pois, function() {
    items += '<li><a href="#"><h3>'+this.name+'</h3></a><a href="tel:\''+this.tel+'\'" >call</a></li>';
});

$('#listview1').html(items);
$('#listview1').listview().listview('refresh');

First listview call will initialize widget and a second will will refresh it.

You have to wait for list view to be created completely. Taken from@gajotres' example :

items='';    
$.each(data.pois, function() {
    items += '<li><a href="#"><h3>'+this.name+'</h3></a><a href="tel:\''+this.tel+'\'" >call</a></li>';
});

$('#listview1').html(items).promise().done(function () {
  $(this).attr("data-role", "list view").listview().listview('refresh');
});

You could make the refresh wait by waiting for the done method of promise.

I recently had nearly the same kind of problem, solved it like this:

$.each(data.itemList, function(index, item) {
    console.log('index: ' + index);
    html += '<li><a href="#" class="detailslink" id="item_' 
         + index + '">' + item.SI_REGNBR + '<h2>Name: ' 
         + item.SI_NAME_EN + '</h2><p>Type: <strong>' 
         + item.SI_TYPE + '</strong></p></a></li>';
});

$('#dataView').empty();
$('#dataView').append($(html));
$('#dataView').trigger('create');
$('#dataView').listview('refresh');
$('#dataView ul').listview('refresh');

This was the success: part of the $.ajax call. The HTML looked like this:

<ul data-role="listview" id="dataView" data-inset="true" data-filter-reveal="false" data-filter-placeholder="Search items..." data-filter="true"></ul>

I hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!