jQuery Mobile - Column Toggle


The column toggle puts the columns in a hiding place and allows the user to select columns as per their choice using the data-mode = "columntoggle" attribute.


Following example demonstrates the use of column toggle in the jQuery Mobile Framework.

<!DOCTYPE html>
      <title>Table Column Toggle</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src = "https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

      <div data-role = "header">
      <table data-role = "table" id = "table-column-toggle" data-mode = "columntoggle" 
         class = "ui-responsive table-stripe" data-column-btn-theme = "b" 
         data-column-btn-text = "Click here to display columns...">
               <th data-priority = "1">Position</th>
               <th data-priority = "2">Rating</th>
               <th data-priority = "3">Points</th>
               <td>SOUTH AFRICA</td>
               <td>NEW ZEALAND</td>
               <td>SRI LANKA</td>
               <td>WEST INDIES</td>
      <div data-role = "footer">


Let's carry out the following steps to see how the above code works −

  • Save the above html code as jqm_table_column_toggle.html file in your server root folder.

  • Open this HTML file as http://localhost/jqm_table_column_toggle.html and the following output will be displayed.

Column Chooser Mode and Setting Column Priority

You can display the table element using data-role = "table" and data-mode = "columntoggle" attributes as shown in the following tag.

<table data-role = "table" id = "table-column-toggle" data-mode = "columntoggle">

You can specify which column should be hidden or shown using the data-priority attribute and assign the priority value from 1 to 6. The table header which will be having the data-priority attribute, will be available in the column menu.

Theme and Customization

By default, the text on the button is "Columns..." however you can change the text using the data-column-btn-text attribute to the table. You can also set the theme for button or column picker menu using the data-column-btn-theme attribute. The rows can be displayed in a striped format by adding table-stripe class to the table element.

Making Tables Responsive

You can make the tables responsive according to screen size and orientation. The media queries specify the responsive behavior to columns using priority and writes the media query using min-width attribute. The widths which are set using em units, will respond to font size changes and you can calculate the value of pixel in em units by dividing the target width by 16 pixels.

Applying a Preset Breakpoint

You can apply the custom breakpoints for each priority levels by adding the class = "ui-responsive" to the table element as shown below −

<table data-role = "table" class = "ui-responsive" id = "table-column-toggle" data-mod 
   e ="columntoggle">

You can use following preset styles for each priority levels −

  • data-priority = "1" − It is used to display the column at 320px(20em).

  • data-priority = "2" − It is used to display the column at 480px(30em).

  • data-priority = "3" − It is used to display the column at 640px(40em).

  • data-priority = "4" − It is used to display the column at 800px(50em).

  • data-priority = "5" − It is used to display the column at 960px(60em).

  • data-priority = "6" − It is used to display the column at 1120px(70em).

Grouped Column Headers

The column data can be shown under each grouped heading for financial data. You can use the priority attribute to parse the specified row and display the heading groups as options. Any table header given a priority will be available in the column picker menu.

Kickstart Your Career

Get certified by completing the course

Get Started