0

Suppose this is the json file you get when going to a url:

[
    {
        "id": 1,
        "date": "2015-06-23",
        "day_todo_set": [
            {
                "id":5
                "name": "Bloom",
                "very_important": True,
                "finished": False
            },
            {
                "id":4
                "name": "Shopping",
                "very_important": True,
                "finished": True
            }
        ]
    },
    {
        "id": 2,
        "date": "2015-06-22",
        "day_todo_set": [
            {
                "id":3
                "name": "REST",
                "very_important": True,
                "finished": True
            }
        ]
    }
]

How can I go through each day object and display it in a list with each of its todo from day_todo_set in its own day list using jquery, something like this.

$(function () {
    var $days = $('#days')

    $.ajax({
        type: 'GET',
        url: '/days/',
        success: function(days) {
            $.each(days, function(i, day) {

                // APPEND IN #days
                 $days.append('<li>date: '+ day.date+ '</li>')

            });
        }
    });
})

So as to get an output like this:

<ul id="days">
    <li class="day">
        <h2>22, June 2015</h2>
          <ul>
              <li class="todo">
                  <p class="todo_name">Bloom</p>
                  <p class="finished">Finished</p>
                  <span class="clear_both"></span>
              </li>
              <li class="todo">
                  <p class="todo_name">Shopping</p>
                  <p class="finished">Pending...</p>
                  <span class="clear_both"></span>
              </li>
          </ul>
    </li>

    <li class="day">
        <h2>22, June 2015</h2>
          <ul>
              <li class="todo">
                  <p class="todo_name">REST</p>
                  <p class="finished">Finished</p>
                  <span class="clear_both"></span>
              </li>
          </ul>
    </li>
</ul> 
2
  • what is your issue please define in subject??? Commented Jun 23, 2015 at 11:09
  • @JqueryKing I would like to place each object from day_todo_set inside a list for its parent list day object. I hope I was clear, if not please ask. Thank you. Commented Jun 23, 2015 at 11:14

2 Answers 2

1

try this:-

var ul = $('.days');
ul.empty();
$.each(data, function (i, d) {
    var li = $('<li/>', { class: 'day' });
    li.append('<h2>' + d.date + '</h2>');
    var innerUl = $('<ul/>');
    $.each(d.day_todo_set, function (ii, dd) {
        var finished = dd.very_important == true ? "Finished" : "Un Finished";
        innerUl.append('<li class="todo"><p class="todo_name">' + dd.name + '</p><p class="finished">'+finished+'</p><span class="clear_both"></span></li>');
  li.append(innerUl);
    });
 ul.append(li);
});

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

the given demo link is fake link.
@AnkitKathiriya not fake refresh answer
Yes, its working. But why does the appended contents does not show in the page source.
@Kakar no it should be display in page source. did you check carefully :p
try to use inspect element option
0

i think this code will help you a lot.

$(function () {
    var $days = $('#days')

    $.ajax({
        type: 'GET',
        url: '/days/',
        success: function (days) {
            var ul = '';
            $.each(days, function (i, day) {
                ul += '<li class="day">';
                ul += ' <h2>' + day.date + '</h2>';
                ul += ' <ul>'
                $.each(day.day_todo_set, function (i, set) {
                    ul += '      <li class="todo">';
                    ul += '          <p class="todo_name">' + set.name + '</p>';
                    ul += '          <p class="finished">' + set.finished + '</p>';
                    ul += '          <span class="clear_both"></span>';
                    ul += '      </li>';
                });
                ul += '  </ul>';
            });
            $days.html(ul);
        }
    });
});

4 Comments

Yes this is what actually needed.
I am getting an error: Uncaught TypeError: Cannot read property 'length' of undefined
Its working now. But when I view the page source the appended contents are not there.
to show content that you appended you need to add add-one (developer option) enabled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.