Suhas
Suhas

Reputation: 37

How To Create jquery code for creating Ul dynamically

I want jquery code to create following Ul in div tag dynamically name and time are different.

I have the name and date from database in json format and I want to create menu from the data using Jquery.

    <div data-role="content">
                <ul data-role="listview" data-divider-theme="b" data-inset="false">
                    <li data-role="list-divider" role="heading"></li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Verinoff,James <span class="patient-list_txt2-red">04/13/2011 12:12</span></a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Parker,Tollin <span class="patient-list_txt2-yellow">04/13/2011 12:10</span> </a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Michael,John <span class="patient-list_txt2-green">04/13/2011 12:04</span></a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Jonah <span class="patient-list_txt2-yellow">04/13/2011 12:03</span></a> </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Jaffee,Dillon <span class="patient-list_txt2-red">04/13/2011 12:02</span></a>
                    </li>
                    <li data-theme="c"><a data-transition="slide" onclick="javascript:navigation_message_details()">
                        Denny Stephen <span class="patient-list_txt2-blue">04/13/2011 12:01</span></a>
                    </li>
                </ul>
            </div>

Upvotes: 1

Views: 186

Answers (2)

phemt.latd
phemt.latd

Reputation: 1803

Try this code, for example this can be your json (you take that from ajax server call):

var yourJson = { "data" : [{"name" : "Paolo", "dateTime" : "04/13/2011 12:01"}] };

And then you can iterate on it:

$.each(yourJson.data, function(i)
{
    var li = $('<li/>')
    .addClass('yourCssClass')
    .attr('data-theme', 'c')
    .appendTo(list);

    var aaa = $('<a/>')
    .html(yourJson.data[i].name + " <span class='patient-list_txt2-blue'>" + yourJson.data[i].dateTime + "</span>")
    .attr('data-transition', 'slide')
    .appendTo(li);
});

And this is a fiddle: http://jsfiddle.net/pbBav/

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

$.each(json, function() {
   $("div[data-role=content] ul")
     .append(
        $("<li>").data('theme', 'c')
           .append($("<a>").data('transition', 'slide')
              .click(navigation_message_details)
              .text(this.name)
              .append($("<span>").addClass('patient-list_txt2-' + this.class)
                 .text(this.date)
              )
           )
        )
     ;
});

I don't know where the color for the class is coming from, so I just assumed it was part of the json.

Upvotes: 2

Related Questions