Listview Widget


Listview Widgetversion added: 1.0

Description: Creates a listview widget

QuickNavExamples

Events

Basic linked lists

A listview is coded as a simple unordered list containing linked list items with a data-role="listview" attribute. jQuery Mobile will apply all the necessary styles to transform the list into a mobile-friendly listview with right arrow indicator that fills the full width of the browser window. When you tap on the list item, the framework will trigger a click on the first link inside the list item, issue an Ajax request for the URL in the link, create the new page in the DOM, then kick off a page transition. View the data- attribute reference to see all the possible attributes you can add to listviews.

Here is the HTML markup for a basic linked list.

1
2
3
4
5
<ul data-role="listview">
<li><a href="acura.html">Acura</a></li>
<li><a href="audi.html">Audi</a></li>
<li><a href="bmw.html">BMW</a></li>
</ul>

Style note on non-inset lists: all standard, non-inset lists have a -15px margin to negate the 15px of padding on the content area to make lists extend to the edges of the screen. If you add other widgets above or below a list, the negative margin may make these elements overlap so you'll need to add additional spacing in your custom CSS.

Nested lists

By nesting child ul or ol inside list items, you can create nested lists. When a list item with a child list is clicked, the framework will show a new page populated with the title of the parent in the header and the list of child elements. These dynamic nested lists are styled with the "b" theme swatch (blue in the default theme) to indicate that you are in a secondary level of navigation. Lists can be nested multiple levels deep and all pages and linking will be automatically handled by the framework. Note that the page of each nested listview is created when the parent page loads and for each nested page the page-events will trigger. Please also note that the title of subpages will contain the text of the first element in the parent list item including its elements. So make sure to use a single element for the list item caption and put other elements like count bubbles behind that caption as shown in the demo below.

To set the swatch color of the child listviews, set the data-theme attribute on each list inside.

Note: Nested lists are deprecated in version 1.3.0

Numbered lists

Lists can also be created from ordered lists (ol) which is useful when presenting items that are in a sequence such as search results or a movie queue. When the enhanced markup is applied to the listview, jQuery Mobile will try to first use CSS to add numbers to the list and, if not supported, will fall back to injecting numbers with JavaScript.

Read-only lists

Listviews can also be used to display a non-interactive list of items, usually as an inset list. This list is built from an unordered or ordered list that don't have linked list items. The framework styles the list items as buttons like the other list types (see theming lists), but without a gradient on the background.

Split button lists

In cases where there is more than one possible action per list item, a split button can be used to offer two independently clickable items - the list item and a small arrow icon in the far right. To make a split list item, simply add a second link inside the li and the framework will add a vertical divider line, style the link as an icon-only arrow button, and set the title attribute of the link to the text of the link for accessibility.

You can set the icon for the right split icon by specifying a data-split-icon attribute on the listview with an icon name you want. The default icon is "arrow-r" but can be configured with the splitIcon listview option. By adding a data-icon attribute to the list item, you can set individual icons for each split. The theme swatch color of the split button defaults to "b" (blue in the default theme) but can be set by specifying a swatch letter with the data-split-theme attribute at the listview level or for individual splits with the data-theme attribute at the link level.

List dividers

List items can be turned into dividers to organize and group the list items. This is done by adding the data-role="list-divider" to any list item. These items are styled with the bar swatch "b" by default (blue in the default theme) but you can specify a theme for dividers by adding the data-divider-theme attribute to the list element (ul or ol) and specifying a theme swatch letter.

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":

1
2
3
4
5
6
7
8
9
10
11
$( "#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;
}
});

Note that if you are using formatted list items that contain a floating element (for example ui-li-aside), this element precedes its siblings regardless of the order in your HTML markup. This results in the first character of the floating element being used as divider text. Therefore it is recommended to specify the divider text in this case.

If new list items are added to the list or removed from it, the dividers are not automatically updated: you should call refresh() on the listview to redraw the autodividers.

Search filter

jQuery Mobile provides a very easy way to filter a list with a simple client-side search feature. To make a list filterable, simply add the data-filter="true" attribute to the list. The framework will then append a search box above the list and add the behavior to filter out list items that don't contain the current search string as the user types. The input's placeholder text defaults to "Filter items...". To configure the placeholder text in the search input, you can either bind to the mobileinit event and set the $.mobile.listview.prototype.options.filterPlaceholder option to a string of your choosing, or use the data attribute data-filter-placeholder on your listview. By default the search box will inherit its theme from its parent. The search box theme can be configured using the data attribute data-filter-theme on your listview.

If you want to change the way in which list items are filtered, i.e. fuzzy search or matching from the beginning of the string, you can configure the callback used internally by defining $.mobile.listview.prototype.options.filterCallback during mobileinit or after the widget has been created with $( "#mylist" ).listview( "option", "filterCallback", yourFilterFunction). Any function defined for the callback will be provided three arguments. First, the text of the current list item, second the value being searched for, and third the list item being filtered. A truthy value will result in a hidden list item. The default callback which filters entries without the searchValue as a substring is described below:

1
2
3
function( text, searchValue, item ) {
return text.toString().toLowerCase().indexOf( searchValue ) === -1;
};

To filter list items by values other than the text, add a data-filtertext attribute to the list item. The value of this attribute will be passed as the first argument to the filterCallback function instead of the text. Alternately you can use an attribute of your choosing as defined on the list item itself.

By default, the listview filter simply searches against the content in each list item. If you want the filter to search against different content, add the data-filtertext attribute to the item and populate it with one or many keywords and phrases that should be used to match against. Note that if this attribute is added, the contents of the list item are ignored.

This attribute is useful for dealing with allowing for ticker symbols and full company names to be searched, or for covering common spellings and abbreviations for countries.

1
2
<li data-filtertext="NASDAQ:AAPL Apple Inc."><a href="#">Apple</a></li>
<li data-filtertext="USA U.S.A. United States of America"><a href="#">United States</a></li>

Text formatting & counts

The framework includes text formatting conventions for common list patterns like header/descriptions, secondary information and counts through semantic HTML markup.

  • To add a count indicator to the right of the list item, wrap the number in an element with a class of ui-li-count
  • To add text hierarchy, use headings to increase font emphasis and use paragraphs to reduce emphasis.
  • Supplemental information can be added to the right of each list item by wrapping content in an element with a class of ui-li-aside

Thumbnails & icons

To add thumbnails to the left of a list item, simply add an image inside a list item as the first child element. The framework will scale the image to 80 pixels square. To use standard 16x16 pixel icons in list items, add the class of ui-li-icon to the image element.

Inset lists

If lists are embedded in a page with other types of content, an inset list packages the list into a block that sits inside the content area with a bit of margin and rounded corners (theme controlled). By adding the data-inset="true" attribute to the list (ul or ol), applies the inset appearance.

Filter reveal

The filter reveal feature makes is easy to build a simple autocomplete with local data. When a filterable list has the data-filter-reveal="true" attribute, it will auto-hide all the list items when the search field is blank. The data-filter-placeholder attribute can be added to specify the placeholder text for the filter. If you need to search against a long list of values, we provide a way to create a filter with a remote data source.

Remote autocomplete with listview filter

To use the listview filter as an autocomplete that taps into remote data sources, you can use the listviewbeforefilter event to dynamically populate a listview as a user types a search query. This is useful when you have a very large data set like cities, zip codes, or products that can't be loaded up-front locally. Use the view source button to see the JavaScript that powers this demo.

If you have a small list of items, you can use the listview filter reveal option to make an autocomplete with local listview data.

Cities worldwide

After you enter at least three characters the autocomplete function will show all possible matches.

Calling the listview plugin

You can directly call the listview plugin on any selector, just like any jQuery plugin:

1
$( "#mylist" ).listview();

Updating lists

If you add items to a listview, you'll need to call the refresh() method on it to update the styles and create any nested lists that are added. For example:

1
$( "#mylist" ).listview( "refresh" );

Note that the refresh() method only affects new nodes appended to a list. This is done for performance reasons. Any list items already enhanced will be ignored by the refresh process. This means that if you change the contents or attributes on an already enhanced list item, these won't be reflected. If you want a list item to be updated, replace it with fresh markup before calling refresh.

If you initially want to hide a list item you can do this by adding a class of ui-screen-hidden to the li element. Using this class ensures the corner styling is applied correctly as well as border-bottom on the last visible item.

Options

countTheme 

Type: String
Default: "c"
Sets the color scheme (swatch) for list item count bubbles. It accepts a single letter from a-z that maps to one of the swatches included in your theme.

This option is also exposed as a data attribute: data-count-theme="a".

Code examples:

Initialize the listview with the countTheme option specified:

1
2
3
$( ".selector" ).listview({
countTheme: "a"
});

Get or set the countTheme option, after initialization:

1
2
3
4
5
// Getter
var countTheme = $( ".selector" ).listview( "option", "countTheme" );
// Setter
$( ".selector" ).listview( "option", "countTheme", "a" );

dividerTheme 

Type: String
Default: "b"
Sets the color scheme (swatch) for list dividers. It accepts a single letter from a-z that maps to one of the swatches included in your theme.

This option is also exposed as a data attribute: data-divider-theme="a".

Code examples:

Initialize the listview with the dividerTheme option specified:

1
2
3
$( ".selector" ).listview({
dividerTheme: "a"
});

Get or set the dividerTheme option, after initialization:

1
2
3
4
5
// Getter
var dividerTheme = $( ".selector" ).listview( "option", "dividerTheme" );
// Setter
$( ".selector" ).listview( "option", "dividerTheme", "a" );

filter 

Type: Boolean
Default: false
Adds a search filter bar to listviews.

This option is also exposed as a data attribute: data-filter="true".

Code examples:

Initialize the listview with the filter option specified:

1
2
3
$( ".selector" ).listview({
filter: true
});

Get or set the filter option, after initialization:

1
2
3
4
5
// Getter
var filter = $( ".selector" ).listview( "option", "filter" );
// Setter
$( ".selector" ).listview( "option", "filter", true );

filterCallback 

Type: Function()
Default: n/a
The function to determine which rows to hide when the search filter textbox changes. The function accepts two arguments -- the text of the list item (or data-filtertext value if present), and the search string. Return true to hide the item, false to leave it visible.
1
2
3
4
5
6
$( document ).on( "mobileinit", function() {
$.mobile.listview.prototype.options.filterCallback = function( text, searchValue ) {
// only show items that *begin* with the search string
return text.toLowerCase().substring( 0, searchValue.length ) !== searchValue;
};
});

filterPlaceholder 

Type: String
Default: "Filter items..."
The placeholder text used in search filter bars.

This option is also exposed as a data attribute: data-filter-placeholder="Search...".

Code examples:

Initialize the listview with the filterPlaceholder option specified:

1
2
3
$( ".selector" ).listview({
filterPlaceholder: Search..."
});

Get or set the filterPlaceholder option, after initialization:

1
2
3
4
5
// Getter
var filterPlaceholder = $( ".selector" ).listview( "option", "filterPlaceholder" );
// Setter
$( ".selector" ).listview( "option", "filterPlaceholder", Search..." );

filterTheme 

Type: String
Default: "c"
Sets the color scheme (swatch) for the search filter bar. It accepts a single letter from a-z that maps to one of the swatches included in your theme.

This option is also exposed as a data attribute: data-filter-theme="a".

Code examples:

Initialize the listview with the filterTheme option specified:

1
2
3
$( ".selector" ).listview({
filterTheme: "a"
});

Get or set the filterTheme option, after initialization:

1
2
3
4
5
// Getter
var filterTheme = $( ".selector" ).listview( "option", "filterTheme" );
// Setter
$( ".selector" ).listview( "option", "filterTheme", "a" );

headerTheme 

Type: String
Default: "c"
Sets the color scheme (swatch) for headers of nested list sub pages. It accepts a single letter from a-z that maps to one of the swatches included in your theme.

This option is also exposed as a data attribute: data-header-theme="a".

(version deprecated: 1.3)
Code examples:

Initialize the listview with the headerTheme option specified:

1
2
3
$( ".selector" ).listview({
headerTheme: "a"
});

Get or set the headerTheme option, after initialization:

1
2
3
4
5
// Getter
var headerTheme = $( ".selector" ).listview( "option", "headerTheme" );
// Setter
$( ".selector" ).listview( "option", "headerTheme", "a" );

icon 

Type: String
Default: "arrow-r"
Applies an icon from the icon set to all linked list buttons.

This option is also exposed as a data attribute: data-icon="star".

Code examples:

Initialize the listview with the icon option specified:

1
2
3
$( ".selector" ).listview({
icon: "star"
});

Get or set the icon option, after initialization:

1
2
3
4
5
// Getter
var icon = $( ".selector" ).listview( "option", "icon" );
// Setter
$( ".selector" ).listview( "option", "icon", "star" );

initSelector 

Default: :jqmData(role='listview')
This is used to define the selectors (element types, data roles, etc.) that will automatically be initialized as listviews.
1
2
3
$( document ).on( "mobileinit", function() {
$.mobile.listview.prototype.options.initSelector = ".mylistview";
});

inset 

Type: Boolean
Default: false
Adds inset list styles.

This option is also exposed as a data attribute: data-inset="true".

Code examples:

Initialize the listview with the inset option specified:

1
2
3
$( ".selector" ).listview({
inset: true
});

Get or set the inset option, after initialization:

1
2
3
4
5
// Getter
var inset = $( ".selector" ).listview( "option", "inset" );
// Setter
$( ".selector" ).listview( "option", "inset", true );

splitIcon 

Type: String
Default: "arrow-r"
Applies an icon from the icon set to all split list buttons.

This option is also exposed as a data attribute: data-split-icon="star".

Code examples:

Initialize the listview with the splitIcon option specified:

1
2
3
$( ".selector" ).listview({
splitIcon: "star"
});

Get or set the splitIcon option, after initialization:

1
2
3
4
5
// Getter
var splitIcon = $( ".selector" ).listview( "option", "splitIcon" );
// Setter
$( ".selector" ).listview( "option", "splitIcon", "star" );

splitTheme 

Type: String
Default: "b"
Sets the color scheme (swatch) for split list buttons. It accepts a single letter from a-z that maps to one of the swatches included in your theme.

This option is also exposed as a data attribute: data-split-theme="a".

Code examples:

Initialize the listview with the splitTheme option specified:

1
2
3
$( ".selector" ).listview({
splitTheme: "a"
});

Get or set the splitTheme option, after initialization:

1
2
3
4
5
// Getter
var splitTheme = $( ".selector" ).listview( "option", "splitTheme" );
// Setter
$( ".selector" ).listview( "option", "splitTheme", "a" );

theme 

Type: String
Default: null, inherited from parent
Sets the color scheme (swatch) for this widget. It accepts a single letter from a-z that maps to one of the swatches included in your theme. By default, it will inherit the same swatch color as its parent container if not explicitly set.

Possible values: swatch letter (a-z).

This option is also exposed as a data attribute: data-theme="a".

Code examples:

Initialize the listview with the theme option specified:

1
2
3
$( ".selector" ).listview({
theme: "a"
});

Get or set the theme option, after initialization:

1
2
3
4
5
// Getter
var theme = $( ".selector" ).listview( "option", "theme" );
// Setter
$( ".selector" ).listview( "option", "theme", "a" );

Methods

childPages()Returns: jQuery (plugin only)

retrieve the sub-pages.

This method returns a jQuery object containing all the immediate child pages of a nested list.

(version deprecated: 1.3)
  • This method does not accept any arguments.
Code examples:

Invoke the childPages method:

1
$( ".selector" ).listview( "childPages" );

refresh()Returns: jQuery (plugin only)

update the listview.

If you manipulate a listview via JavaScript (e.g. add new LI elements), you must call the refresh method on it to update the visual styling.

  • This method does not accept any arguments.
Code examples:

Invoke the refresh method:

1
$( ".selector" ).listview( "refresh" );

Events

create( event, ui )Type: listviewcreate

triggered when a listview is created

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the listview with the create callback specified:

1
2
3
$( ".selector" ).listview({
create: function( event, ui ) {}
});

Bind an event listener to the listviewcreate event:

1
$( ".selector" ).on( "listviewcreate", function( event, ui ) {} );

Example:

A basic example of a listview

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>listview demo</title>
<link rel="stylesheet" href="//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header">
<h1>jQuery Mobile Example</h1>
</div>
<div data-role="content">
<ul data-role="listview">
<li><a href="index.html">Acura</a></li>
<li><a href="index.html">Audi</a></li>
<li><a href="index.html">BMW</a></li>
<li><a href="index.html">Cadillac</a></li>
<li><a href="index.html">Chrysler</a></li>
</ul>
</div>
<div>
</body>
</html>

Demo: