简体   繁体   English

如何在循环中填充HTML列表

[英]How to fill HTML list in a loop

I have an Html list, I receive data as JSON dictionary from a REST call, I traverse and get their values. 我有一个HTML列表,我从REST调用中以JSON字典的形式接收数据,遍历并获取其值。 But I couldn't find how to generate the Html below with items in a loop. 但是我找不到如何使用循环生成下面的HTML。

<div class="container">
<ul role="list">
    <li class="infoBasic" role="listitem">
        <div class="date">20-12-2012</div>
        <div class="ammount">-&euro;4,25</div>
    </li>
    <li class="infoDetails" role="listitem">
        <div class="place">data</div>
        <div class="category">data</div>
        <div class="description_title">data</div>
        <div class="description">data</div>
    </li>

   //repeat this for multi items received from REST

</ul>

$.ajax
  ({

   success: function (data){                    
        var transactions=data['transactions'];      

        $.each( transactions, function( key, value ) {
             $.each( value, function( key, value) {

                //here how to set/create the appropriate div elements(like above) 
                // with the values I get here in a loop? 

             });                        
      }); 

EDIT some solutions below is fine, but I dont want all the values from my object array, how can I get only these items(date, amount, place..etc) to the html div and ignore other items in the dictionary 编辑下面的一些解决方案很好,但是我不希望从对象数组中获得所有值,我如何只将这些项目(日期,金额,位置..etc)获取到html div中,而忽略字典中的其他项目

$.each( value, function( key, value) {

            //here how to set/create the appropriate div elements(like above) 
            // with the values I get here in a loop? 

            $(".infoDetails").append("<div class='" + key + "'>" + value + "</div>");
         }); 

you can use.. 您可以使用..

     var data = "";

   $.each( transactions, function( key1, value1 ) {
         data +="<li class ='"+ key1 +"' role='listitem'>";

             $.each( value1, function( key, value) {
                 data +="<div class ='"+ key +"'>"+value+"</div>"; 
             });
     });  


   $("ul").html(data); // selecting ul element and then embedding html into it..

try this...take a variable says html...iterate through json array...add items in list...and the put entire html inside a div.. 试试这个...采取一个变量说html ...遍历json数组...在列表中添加项目...并将整个html放入div ..

var html='<ul>';
$.ajax
  ({

   success: function (data){                    
        var transactions=data['transactions'];      

        $.each( transactions, function( key, value ) {
             $.each( value, function( key, value) { 
                html+='<li class="'+key+'">'+value+'</li>';
             });                        
      });
       } 
 }); 
html+='</ul>';
$('#idofdiv').html(html);

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

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