ons-list-item

Component that represents each item in a list. The list item is composed of four parts that are represented with the left, center, right and expandable-content classes. These classes can be used to ensure that the content of the list items is properly aligned.

  <ons-list-item>
    <div class="left">Left</div>
    <div class="center">Center</div>
    <div class="right">Right</div>
    <div class="expandable-content">Expandable content</div>
  </ons-list-item>

There are also a number of classes (prefixed with list-item__*) that help when putting things like icons and thumbnails into the list items.

Tutorial

Lists

Creating lists in Onsen UI is very simple and they are one of the most important UI components in mobile apps.

Onsen UI provides some elements for creating lists:

  • <ons-list>
  • <ons-list-item>
  • <ons-list-header>
  • <ons-list-title>

Markup

The <ons-list> is used as the parent and for every item one <ons-list-item> element is used:

<ons-list>
  <ons-list-item>Item A</ons-list-item>
  <ons-list-item>Item B</ons-list-item>
  <ons-list-item>Item C</ons-list-item>
</ons-list>

This will render as a list with three items.

Headers and titles

To group items together you can use the <ons-list-header> element to create a header with text content. The whole list can be preceded by a <ons-list-title> element.

<ons-list-title>People I know</ons-list-title>
<ons-list>
  <ons-list-header>My friends</ons-list-header>
  <ons-list-item>Alice</ons-list-item>
  <ons-list-item>Bob</ons-list-item>
  <ons-list-item>Eve</ons-list-item>

  <ons-list-header>My enemies</ons-list-header>
  <ons-list-item>Alice</ons-list-item>
  <ons-list-item>Bob</ons-list-item>
  <ons-list-item>Eve</ons-list-item>
</ons-list>

You can use as many headers as you want in the same list.

List item structure

In the previous examples the list items only contained simple text content. Onsen UI list items provide a secondary syntax where the list item is divided into four sections.

This can be used to add icons, thumbnails or even form elements to your list items:

<ons-list-item>
  <div class="left">
    <ons-icon icon="md-face" class="list-item__icon"></ons-icon>
  </div>
  <div class="center">
    Icon and switch
  </div>
  <div class="right">
    <ons-switch></ons-switch>
  </div>
  <div class="expandable-content">
    Expandable content
  </div>
</ons-list-item>

The child elements can also be label or even ons-if.

Apart from this, some extra classes are provided to style different parts of the list items:

  • list-item__title: Title inside the central part. Modifies font and position.
  • list-item__subtitle: Subtitle inside the central part. Modifies font and position.
  • list-item__icon: List item icon. Modifies size and alignment.
  • list-item__thumbnail: List item thumbnail. Modifies size, borders and alignment.
<ons-list-item>
  <div class="left">
    <img class="list-item__thumbnail" src="https://placekitten.com/g/40/40">
  </div>
  <div class="center">
    <span class="list-item__title">Cutest kitty</span>
    <span class="list-item__subtitle">On the Internet</span>
  </div>
</ons-list-item>

Expandable list items

You can create list items that expand to reveal extra content when tapped by setting the expandable attribute. The content that is shown when the list item is expanded is defined in div.expandable-content.

<ons-list-item expandable>
  Tap to expand
  <div class="expandable-content">This is shown when expanded</div>
</ons-list-item>

If div.right is not defined, a dropdown arrow is automatically added to the right side of the expandable list item.

The list item can also be expanded and contracted with the showExpansion and hideExpansion methods.

The tappable attribute

The tappable attribute is used to give the user an indication when they tap a list item. In iOS the background color will change when tapped and on Android it will display the Material Design ripple effect.

<ons-list-item tappable>Tap me!</ons-list-item>

This is very useful in apps that perform some action or navigate to a new page when a list item is tapped.

In order to prevent tappable effect when tapping on specific child elements like custom buttons, prevent-tap attribute can be added to any of the children. ons-* elements like ons-button or ons-checkbox are ignored by default.

Modifiers

There are a bunch of modifiers that can be used to customize the look of the lists and list items.

In Onsen UI modifiers are applied by adding the modifier attribute to an element. More than one can be added by separating them with spaces.

For example, to create an inset list you can use the inset modifier on the <ons-list> element:

<ons-list modifier="inset">
  ...
</ons-list>

Changing the divider

The longdivider and nodivider modifiers can be used to change the length or remove the divider (horizontal line) between list items completely. The default style for list items is a divider that doesn’t completely cover the whole screen. Instead, it has some padding on the left to make it look more similar to native lists.

By adding the nodivider modifier the divider can be removed:

<ons-list>
  <ons-list-item modifier="nodivider">Item A</ons-list-item>
  <ons-list-item modifier="nodivider">Item B</ons-list-item>
</ons-list>

Compilation

ons-list-item adds extra classes to avoid CSS collisions. The final results is as follows:

<ons-list-item class="list-item">
  <div class="top list-item__top">
    <div class="left list-item__left">Left content</div>
    <div class="center list-item__center">Center content</div>
    <div class="right list-item__right">Right content</div>
  </div>
  <div class="expandable-content list-item__expandable-content">Expandable content</div>
</ons-list-item>

If none of those sections is provided, ons-list-item will wrap all its content inside div.list-item__center.

See also

Attributes are added directly to the element. You can do this in HTML or JS.

HTML: <ons-list-item someAttribute="true" anotherAttribute><ons-list-item>
JS: document.querySelector('ons-list-item').setAttribute('someAttribute', 'true')

Name Type Description
modifier String The appearance of the list item. Optional.
lock-on-drag Boolean Prevent vertical scrolling when the user drags horizontally. Optional.
tappable Boolean Makes the element react to taps. prevent-tap attribute can be added to child elements like buttons or inputs to prevent this effect. ons-* elements are ignored by default. Optional.
tap-background-color Color Changes the background color when tapped. For this to work, the attribute “tappable” needs to be set. The default color is “#d9d9d9”. It will display as a ripple effect on Android. Optional.
keep-tap-background-color Boolean Prevent from clearing the background color on "touchmove", "touchcancel", "touchend", "touchleave", "mouseup", and "mouseout". For this to work, the attribute “tappable” needs to be set. Optional.
expandable Boolean Makes the element able to be expanded to reveal extra content. For this to work, the expandable content must be defined in div.expandable-content. Optional.
expanded Boolean For expandable list items, specifies whether the expandable content is expanded or not. Optional.
animation String
default
The animation used when showing and hiding the expandable content. Can be either "default" or "none". Optional.

Properties are accessed on the element through JS, and should be get and set directly. For example: document.querySelector('ons-list-item').lockOnDrag.

Name Description
lockOnDrag Prevent vertical scrolling when the user drags horizontally.
tappable Makes the element react to taps. prevent-tap attribute can be added to child elements like buttons or inputs to prevent this effect. ons-* elements are ignored by default.
tapBackgroundColor Changes the background color when tapped. For this to work, the attribute “tappable” needs to be set. The default color is “#d9d9d9”. It will display as a ripple effect on Android.
keepTapBackgroundColor Prevent from clearing the background color on "touchmove", "touchcancel", "touchend", "touchleave", "mouseup", and "mouseout". For this to work, the attribute “tappable” needs to be set.
expandable Makes the element able to be expanded to reveal extra content. For this to work, the expandable content must be defined in div.expandable-content.
expanded For expandable list items, specifies whether the expandable content is expanded or not.
animation The animation used when showing and hiding the expandable content. Can be either "default" or "none".

Modifiers are set in the modifier attribute. To use more than one, separate them by spaces. For example:
<ons-list-item modifier="tappable chevron"><ons-list-item>.

Name Description
tappable Make the list item change appearance when it’s tapped. On iOS it is better to use the “tappable” and “tap-background-color” attribute for better behavior when scrolling.
chevron Display a chevron at the right end of the list item and make it change appearance when tapped.
longdivider Displays a long horizontal divider between items.
nodivider Removes the divider between list items.
material Display a Material Design list item.

These methods are called directly on the DOM element. Get a reference to the element in JS, and the methods below will be available to call on it. For example: document.querySelector('ons-list-item').someMethod().

Signature Description
showExpansion() Show the expandable content if the element is expandable.
hideExpansion() Hide the expandable content if the element expandable.
clearTapBackgroundColor() Clear backgroundColor changed on tap or click. This method is helpful when keep-tap-background-color is true.
showExpansion()

Show the expandable content if the element is expandable.

hideExpansion()

Hide the expandable content if the element expandable.

clearTapBackgroundColor()

Clear backgroundColor changed on tap or click. This method is helpful when keep-tap-background-color is true.

To use these events, add event listeners to the elements as you would for native events, like click. For example: document.querySelector('ons-list-item').addEventListener('expand', function() { ... }).

Some Onsen UI components have overlapping event names. For example, ons-carousel and ons-navigator both emit postchange events. Stop overlapping events from propagating to avoid conflicts: document.querySelector('ons-carousel').on('postchange', e => e.stopPropagation()).

Name Description
expand For expandable list items, fires when the list item is expanded or contracted.
expand

For expandable list items, fires when the list item is expanded or contracted.

Parameters
Name Type Description

Need Help?

If you have any questions, use our Community Forum or talk to us on Discord chat. The Onsen UI team and your peers in the community will work together to help solve your issues.

For bug reports and feature requests use our GitHub Issues page.