1

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>

2 Answers 2

2
$.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.

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

Comments

0

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/

11 Comments

how can i add an line after every row?
A line? you mean a div styled as a line or an <hr> tag? Do you want a separator for the list?
If you want a line separator i have updated the fiddle: jsfiddle.net/pbBav/4 If you find it usefull update my answer ;)
thanks a lot for your great answer.but when i put hr my datetime span overlapping line.
probably this is a problem related to the html/css of your page... if you give me some more of this code i can give you more help to find a solutions!
|

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.