开发者

parsing json in an unordered list

开发者 https://www.devze.com 2023-03-19 04:50 出处:网络
i\'m parsing a 开发者_JAVA技巧json file in an unordered list. the problem is that i need to paginate the resultsin \"ul\" containing only 3 items.

i'm parsing a 开发者_JAVA技巧json file in an unordered list. the problem is that i need to paginate the results in "ul" containing only 3 items. so i want to close the ul after 3 results and then reopen it.

here is my code:

$.getJSON('/it_IT/eni_nel_mondo/components/menu/comunicati.json', function(data) {

          var list = [];

          $.each(data.items, function(i, c) {
            list.push('<li><a href="#"><div class="title"><div class="data"><span class="number">'+c.day+'</span><span class="month">'+c.month+'</span></div><h4>'+c.title+'</h4></div><p>'+c.abstract+'</p></a></li>');
          });

          $('<ul/>', {
            'class': 'results',
            html: list.join('')
          }).appendTo('.wrap .items');
        });

any help?

i resolved like this:

$.getJSON('/it_IT/eni_nel_mondo/components/menu/comunicati.json', function (data) {
        var list = [];
        var counter = 0;
        $.each(data.items, function (i, c) {
            counter++;
            list.push('<li><a href="#"><div class="title"><div class="data"><span class="number">' + c.day + '</span><span class="month">' + c.month + '</span></div><h4>' + c.title + '</h4></div><p>' + c.abstract + '</p></a></li>');
            if (counter != 0 && counter % 3 == 0) {
                $('<ul/>', {
                    'class': 'results',
                    html: list.join('')
                }).appendTo('.wrap .items');
                list = [];
            }
        });
        if (counter % 3 != 0 && list.length > 0) {
            $('<ul/>', {
                'class': 'results',
                html: list.join('')
            }).appendTo('.wrap .items')
        }

i hope this could be useful to someone. thnx to Aaron Ray for the response, i've seen the last one when my work was already done.


I haven't personally worked with that plugin, but if you are confident that you have that working I think the problem is with this part of the code:

      $('<ul/>', {
        'class': 'results',
        html: list.join('')
      }).appendTo('.wrap .items');

I am assuming your list array contains all of the data, and it is not just returning 3 items at a time. So list.join('') will dump every single item into the html. You will have to loop through the list and use the modulus operator to create your ul's. Then append them to a container (probably your scrollable items container).

var html = "<ul>";
for(i = 0; i < list.length; i++)
{
    if(i % 3 == 0 && i > 0) {
        html += "</ul><ul>";
    }
    html += "<li>" + jsonData + "</li">;
}
html += "</ul>";

$("#items").append(html);
0

精彩评论

暂无评论...
验证码 换一张
取 消