how to create auto list dividers when dynamically creating a list in jquery mobile

白昼怎懂夜的黑 提交于 2019-12-25 02:04:11

问题


I've created a list of messages dynamically. I have a <ul> tag and I'm generating a list inside dynamically. However, I don't know how to add list dividers to this. Would there be a refresh method in js that I can call which would add dividers after I have my list?

This is what I have:

<ul id="messages" data-autodividers="true" data-role="listview" data-theme="c">

    //a list of <li> are dynamically generated here

</ul>

This only appends on list divider to the very top, I need several of them within the <li> tags. My list of <li>'s are messages. I want to be able to have dividers by Date so it makes it much easier to use.


回答1:


I've a jsfiddle http://jsfiddle.net/yVtVE/2/

The code is a bit different but it works

assuming a list like this

<ul data-role="listview" id="MessagesList" data-autodividers="true">
                <li date="2013-03-20"><a href="#">Event 1</a></li>
                <li date="2013-03-20"><a href="#">Event 2</a></li>
                <li date="2013-03-19"><a href="#">Event 3</a></li>
           </ul>

Your code would be

$("#MessagesList").listview({
            autodividers: true,
            autodividersSelector: function (li) {
                var out = li.attr('date');
                return out;
            }
        }).listview('refresh');

ORIGINAL

From the JQM docs http://api.jquerymobile.com/listview/

Autodividers

A listview can be configured to automatically generate dividers for its items. This is done by adding a data-autodividers="true" attribute to any listview.

By default, the text used to create dividers is the uppercased first letter of the item's text. Alternatively you can specify divider text by setting the autodividersSelector option on the listview programmatically. For example, to add a custom selector to the element with id="mylistview":

$( "#mylistview" ).listview({
autodividers: true,
// the selector function is passed a <li> element from the listview;
// it should return the appropriate divider text for that <li>
// element as a string
autodividersSelector: function ( li ) {
var out = /* generate a string based on the content of li */;
return out;
}
});

Let's say you have a date in your message your code would work something like this

$( "#mylistview" ).listview({
autodividers: true,
autodividersSelector: function ( li ) {
    var re = /\/\d{4}\/\d{2}\/\d{2}/i;
    var out = $(this).html().match(re);
    return out;
  }
});

I have not tested this code, it might have bugs, but it will give you an idea to try.



来源:https://stackoverflow.com/questions/15527014/how-to-create-auto-list-dividers-when-dynamically-creating-a-list-in-jquery-mobi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!