Skip to content

Commit 8e499dd

Browse files
committed
Add autodividers functionality for listviews
Can be used to autogenerate dividers for a listview by setting data-autodividers="alpha" (dividers are unique, uppercased single characters from list items) or data-autodividers="full" (unique full text strings selected from list items) on the ul element of the listview. It's also possible to apply a custom selector to find the elements to be used for divider text, with data-autodividers-selector="...". This relates to jquery-archive/jquery-mobile#2466, but is a different implementation which will automatically update the list dividers if the list elements change. It also has a fairly thorough test suite and documentation.
1 parent 9c5135a commit 8e499dd

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

js/index.php

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'jquery.mobile.navbar.js',
3636
'jquery.mobile.listview.js',
3737
'jquery.mobile.listview.filter.js',
38+
'jquery.mobile.listview.autodividers.js',
3839
'jquery.mobile.nojs.js',
3940
'jquery.mobile.forms.checkboxradio.js',
4041
'jquery.mobile.forms.button.js',
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2+
//>>description: Generates dividers for listview items
3+
//>>label: Listview Autodividers
4+
define( [ "jquery", "./jquery.mobile.listview" ], function( $ ) {
5+
//>>excludeEnd("jqmBuildExclude");
6+
(function( $, undefined ) {
7+
8+
$.mobile.listview.prototype.options.autodividers = false;
9+
$.mobile.listview.prototype.options.autodividersSelector = function( elt ) {
10+
// look for the first anchor in the item
11+
var text = elt.find( 'a' ).text() || elt.text() || null;
12+
13+
if ( !text ) {
14+
return null;
15+
}
16+
17+
// create the text for the divider (first uppercased letter)
18+
text = text.slice( 0, 1 ).toUpperCase();
19+
20+
return text;
21+
};
22+
23+
$( document ).on( "listviewcreate", "ul,ol", function() {
24+
25+
var list = $( this ),
26+
listview = list.data( "listview" );
27+
28+
if ( !listview.options.autodividers ) {
29+
return;
30+
}
31+
32+
var replaceDividers = function () {
33+
list.find( 'li:jqmData(role=list-divider)' ).remove();
34+
35+
var lis = list.find( 'li' );
36+
37+
var lastDividerText = null;
38+
var li;
39+
var dividerText;
40+
var i = 0;
41+
42+
for ( i ; i < lis.length ; i++ ) {
43+
li = lis[i];
44+
dividerText = listview.options.autodividersSelector( $( li ) );
45+
46+
if ( dividerText && lastDividerText !== dividerText ) {
47+
var divider = document.createElement( 'li' );
48+
divider.appendChild( document.createTextNode( dividerText ) );
49+
divider.setAttribute( 'data-' + $.mobile.ns + 'role', 'list-divider' );
50+
li.parentNode.insertBefore( divider, li );
51+
}
52+
53+
lastDividerText = dividerText;
54+
}
55+
};
56+
57+
var afterListviewRefresh = function () {
58+
list.off( 'listviewafterrefresh', afterListviewRefresh );
59+
replaceDividers();
60+
listview.refresh();
61+
list.on( 'listviewafterrefresh', afterListviewRefresh );
62+
};
63+
64+
afterListviewRefresh();
65+
66+
});
67+
68+
})( jQuery );
69+
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
70+
});
71+
//>>excludeEnd("jqmBuildExclude");

0 commit comments

Comments
 (0)