Latest web development tutorials
 

jQuery Mobile Tables


Responsive Tables

Responsive design is useful when you want the content of your mobile web page to respond to the user's device, such as its screen size and orientation.

With a simple class name, jQuery Mobile is aware of the user's available screen size and automatically resizes itself to show content that is relevant for that particular user.

Responsive tables allow us to display a large set of tabular data that will look attractive for both mobiles and desktops.

There are two types of responsive tables: reflow and column toggle.


Reflow Table

The reflow mode positions the table data horizontally until it reaches a minimum size, then all rows are grouped together vertically.

Create a table, add the data-role="table" and a class of "ui-responsive" on the <table> element:

Example

<table data-role="table" class="ui-responsive">
Try it Yourself »

For the responsive table to work correctly, you must include the <thead> and <tbody> elements.
Do not use rowspan or colspan attributes; they are not supported in responsive tables.


Column Toggle Table

The "column toggle" mode will hide columns when there is not enough width to display the data.

To create a column toggle table, add the following to the <table> element:

<table data-role="table" data-mode="columntoggle" class="ui-responsive" id="myTable">

By default, jQuery Mobile will hide columns from the right side of the table. However, you are allowed to specify which column that should be hidden or shown in a specific order. Add the data-priority attribute to the table's header (<th>) and specify a number between 1 (highest priority) to 6 (lowest priority):

<th>I will never be hidden</th>
<th data-priority="1">I am very important - I will probably not be hidden</th>
<th data-priority="3">I am less important - I could be hidden</th>
<th data-priority="5">I am not that important - there is a good chance that I will be hidden</th>

If you do not specify a priority for a column, the column will be persistent and not available for hiding.

Put it all together and you have created a column toggle table! Notice that the framework automatically adds a button in the top right corner of the table. This lets the user to choose which column to display in the table:

Example

<table data-role="table" data-mode="columntoggle" class="ui-responsive" id="myTable">
  <thead>
    <tr>
      <th data-priority="6">CustomerID</th>
      <th>CustomerName</th>
      <th data-priority="1">ContactName</th>
      <th data-priority="2">Address</th>
      <th data-priority="3">City</th>
      <th data-priority="4">PostalCode</th>
      <th data-priority="5">Country</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Alfreds Futterkiste</td>
      <td>Maria Anders</td>
      <td>Obere Str. 57</td>
      <td>Berlin</td>
      <td>12209</td>
      <td>Germany</td>
    </tr>
  </tbody>
</table>
Try it Yourself »

To change the toggle table button text, use the data-column-btn-text attribute:

Example

<table data-role="table" data-mode="columntoggle" class="ui-responsive" data-column-btn-text="Click me to hide or show columns!" id="myTable">
Try it Yourself »

Styling Tables

Use the "ui-shadow" class to add shadows to the table:

Add shadow

<table data-role="table" data-mode="columntoggle" class="ui-responsive ui-shadow" id="myTable">
Try it Yourself »

For further table styling, use CSS:

Add a bottom border to all table rows

<style>
tr {
    border-bottom: 1px solid #d6d6d6;
}
</style>
Try it Yourself »

Add a bottom border to all <th> elements and a background color to all even table rows

<style>
th {
    border-bottom: 1px solid #d6d6d6;
}

tr:nth-child(even) {
    background: #e9e9e9;
}
</style>
Try it Yourself »