简体   繁体   English

jQuery Mobile使用$ .each将数据附加到列表视图

[英]jQuery Mobile append data using $.each to listview

I tried to make a data reading mechanism using ajax. 我试图使用ajax建立数据读取机制。 But when all the data is displayed in the form of <ul> <li> did not generate the appropriate html tag I want. 但是,当所有数据以<ul> <li>的形式显示时,都没有生成我想要的适当html标签。

My code : 我的代码:

(function( $, undefined ) {

     $(document).on("pagecreate", ".jqm-demos", function(){
         startApp();
     });

    function startApp(){
          $.ajax({
             type : 'GET',
             url : 'http://www.mydomain.com/app/bacaberita/utama.html',
             async: true,
             dataType : 'json',
             success : function(result){
                 allData = result.items;
                   $.each(allData, function(i, data) {
                   $('#listdata').append(
                     '<li>' +
                         '<a href="#">' +
                            '<img src="'+ data.Img +'">' +
                            '<h2>'+ data.Title +'</h2>' +
                            '<p>'+ data.Post +'</p>' +
                         '</a>' +
                     '</li>'
              );
           });
    },
    error: function(jqXHR, exception) {
         alert('Error connection');       
    }
  });   
}

})(jQuery);

My html code : 我的html代码:

<ul data-role="listview" data-inset="true" id="listdata">
    <li>

    </li>
</ul>

The data is supposed to generate shaped like this : http://pastebin.com/VLevgVfx 数据应该生成如下所示的形状: http : //pastebin.com/VLevgVfx

But what I get from the ajax process is : http://pastebin.com/Rp0DSCYA 但是我从ajax流程中得到的是: http : //pastebin.com/Rp0DSCYA

As a result, the resulting irregular display. 结果,导致不规则显示。

No HTML is automatically added class="ui-li-has-thumb ui-first-child" and class="ui-btn ui-btn-icon-right ui-icon-carat-r" 没有自动添加HTML class="ui-li-has-thumb ui-first-child"class="ui-btn ui-btn-icon-right ui-icon-carat-r"

How do I resolve this problem??? 我该如何解决这个问题???

Thanks 谢谢

Add this at the end of the success function: 在成功函数的末尾添加:

$("#listdata").listview("refresh");

As you are adding elements dimamically, you need to ask jQM to refresh the element to parse and add the styles. 在以二维方式添加元素时,需要让jQM刷新元素以解析和添加样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM