jQuery tablesorter 2.0 - Table Reflow Widget

# tablesorter

## Table Reflow Widget

### Flexible client-side table sorting

Back to documentation

### Notes

- This widget will only work in tablesorter version 2.8+, jQuery version 1.7+ and HTML5 (make sure the doctype is set as `<!DOCTYPE html>`).
- The required css, shown below, is needed to make this widget work, but is completely customizable.
- This demo sets the reflow breakpoint to 35em; so you'll need to resize your browser window to see this widget in action.
- This widget is compatible with the column selector widget; but please note:
  - When a column is hidden, it will not show up when the table reflow occurs
  - For an example, see the second demo "Reflow + columnSelector widget" below.
  - The column selector widget, by default, has an additional breakpoint at "30em". So if the column selector is set to "Auto" and you continue to reduce the browser width after the table reflows, you'll see "Geometry" row disappear.
- It may not have been necessary to create two versions of this widget, but it was done because:
  - The first "reflow" widget does not add additional content to the tbody table cells, but it did not work well when attempting to add multiple content rows within the data-attribute. It would work if the css explicitly has the content set with a break like this (ref):

        content: "row1 \A row2";

    But sadly, this method does not work when a data-attribute with the same content is used:

        content: attr(data-title);

    The second method ignores the backslash and shows `"row1 \A row2"`
  - Update: After some discussion in bugzilla, I discovered that it is possible to add multiple lines of content using a data-attribute by setting a `white-space: pre;` css definition and using `&#10` instead of `\A` (demo), but I think I'll keep the second reflow version because it actually allows styling.
  - Adding bold elements to every table tbody cell is the method used by jQuery Mobile; so, a second reflow widget "reflow2" was created to do the same, and work properly with a thead containing multiple rows.
- If you do not want the sortable headers to show when the table reflows, include this css within the media query:

      table.ui-table-reflow thead { display: none; }

### Reflow Widget

- This version adds a `data-title` attribute (modifiable by the `reflow_dataAttrib` option), to every table body cell.
- The required css shows the contents of this data-attribute when the browser width reaches the set break point.
- So, this widget does not add any additional content to the table cells; which might be preferred if the table content should not be changed.
- Do not use this version of the widget for a table with more than one header row.

#### Reflow widget default options (added inside of tablesorter `widgetOptions`)

Option | Description
--- | ---
reflow_className | This class name is added to the table to make it responsive. The class name is the same as that used by jQuery Mobile.
    <table class="tablesorter tablesorter-blue ui-table-reflow">...</table>
Default value: `"ui-table-reflow"`
reflow_headerAttrib | Set this option to use your desired data-attribute. The set data-attribute contains the alternative header title added to every table body cell when the table reflows. For example, the "Sex" column below is renamed to "Gender" within the reflow cells:
    <th data-name="Gender">Sex</th>
Default value: `"data-name"`
reflow_dataAttrib | Set this option to use your desired data-attribute which is added to every table body cell. It will contain the header name which is shown once the table reflows. For example, all of table body cells will show the contents from the set data-attribute when the table reflows:
    <td data-title="Gender">female</td>
Default value: `"data-title"`

### Reflow2 Widget

- This version of the reflow widget adds a bold element (`<b>`), containing the header cell name, to every table body cell.
- The required css shows these elements when the browser width reaches the set break point.
- Use this version of the widget for a table with more than one header row.
- So, this widget does add additional content to the table tbody cells; this might not be preferred if the table content should not be changed.
- If an update occurs, this widget will remove all cell elements, and replace them, even if the headers have changed.
- In very large tables, initialization of the table may be slightly delayed.

#### Reflow2 widget default options (added inside of tablesorter `widgetOptions`)

Option | Description
--- | ---
reflow2_className | This class name is added to the table to make it responsive. The class name is the same as that used by jQuery Mobile.
    <table class="tablesorter tablesorter-blue ui-table-reflow">...</table>
Default value: `"ui-table-reflow"`
reflow2_classIgnore | Set this option to use your desired class name. Any header cell with this class name will have it's contents ignored. For example, this is the "Paris" header cell in table3 below:
    <th class="ui-table-reflow-ignore sorter-false">Paris</th>
Default value: `"ui-table-reflow-ignore"`
reflow2_headerAttrib | Set this option to use your desired data-attribute. The set data-attribute contains the alternative header title added to every table body cell when the table reflows. For example, the "Sex" column below is renamed to "Gender" within the reflow cells:
    <th data-name="Gender">Sex</th>
Default value: `"data-name"`
reflow2_labelClass | Set this option to use your desired class name. When the table reflows, the bold element (`<b>`) with this class name will be revealed; it must also be defined within the media query to display this element as an "inline-block", so it shares the row with the cell content #### CSS
    /* additional selectors needed to increase css specificity */
    table.ui-table-reflow .ui-table-cell-label {
      padding: .4em;
      min-width: 30%;
      display: inline-block;
      margin: -.4em 1em -.4em -.4em;
    }
#### HTML
    <th>
    	<b class="remove-me ui-table-cell-label">Month</b>
    	January
    </th>
Default value: `"ui-table-cell-label"`
reflow2_labelTop | Set this option to use your desired class name. When the table reflows, the all upper header cell content is also included in every corresponding column table cell within a bold element (`<b>`); it must also be defined within the media query to display this element as a "block" #### CSS
    /* additional selectors needed to increase css specificity */
    table.ui-table-reflow .ui-table-cell-label.ui-table-cell-label-top {
      display: block;
      padding: .4em 0;
      margin: .4em 0;
      text-transform: uppercase;
      font-size: .9em;
      font-weight: 400;
    }
#### HTML
    <td>
    	<b class="remove-me ui-table-cell-label ui-table-cell-label-top">Average Temperatures (C)</b>
    	<b class="remove-me ui-table-cell-label">Minimum Temp</b>
    	3
    </td>
Default value: `"ui-table-cell-label-top"`

### Required CSS

The reflow widgets require the following css to hide/show header cell content within each table cell. Remove the css for whichever reflow widget that is not being used, as desired.

    /* REQUIRED CSS: change your reflow breakpoint here (35em below) */
    @media ( max-width: 35em ) {

    	/* uncomment out the line below if you don't want the sortable headers to show */
    	/* table.ui-table-reflow thead { display: none; } */

    	/* css for reflow & reflow2 widgets */
    	.ui-table-reflow td,
    	.ui-table-reflow th {
    		-webkit-box-sizing: border-box;
    		-moz-box-sizing: border-box;
    		box-sizing: border-box;
    		float: right;
    		/* if not using the stickyHeaders widget (not the css3 version)
    		 * the "!important" flag, and "height: auto" can be removed */
    		width: 100% !important;
    		height: auto !important;
    	}

    	/* reflow widget only */
    	.ui-table-reflow tbody td[data-title]:before {
    		color: #469;
    		font-size: .9em;
    		content: attr(data-title);
    		float: left;
    		width: 50%;
    		white-space: pre-wrap;
    		text-align: bottom;
    		display: inline-block;
    	}

    	/* reflow2 widget only */
    	table.ui-table-reflow .ui-table-cell-label.ui-table-cell-label-top {
    		display: block;
    		padding: .4em 0;
    		margin: .4em 0;
    		text-transform: uppercase;
    		font-size: .9em;
    		font-weight: 400;
    	}
    	table.ui-table-reflow .ui-table-cell-label {
    		padding: .4em;
    		min-width: 30%;
    		display: inline-block;
    		margin: -.4em 1em -.4em -.4em;
    	}

    } /* end media query */

    /* reflow2 widget */
    .ui-table-reflow .ui-table-cell-label {
    	display: none;
    }

# Demo (Pick a table width or resize the browser window)

### Reflow widget only

Set table width:

- Auto

### Reflow + columnSelector widget

Set table width:

- Auto

### Reflow2 widget (multiple thead rows)

Set table width:

- Auto

# HTML

    <h3>Reflow widget only</h3>
    <table id="table1">
      <thead>
        <tr>
          <th>Rank</th>
          <th>Age</th>
          <th>Total</th>
          <th>Discount</th>
          <th>Date</th>
        </tr>
      </thead>
      <tbody>
        <tr><td>1</td><td>25</td><td>$5.95</td><td>22%</td><td>Jun 26, 2013 7:22 AM</td></tr>
        <tr><td>11</td><td>12</td><td>$82.99</td><td>5%</td><td>Aug 21, 2013 12:21 PM</td></tr>
        <tr><td>12</td><td>51</td><td>$99.29</td><td>18%</td><td>Oct 13, 2013 1:15 PM</td></tr>
        <tr><td>51</td><td>28</td><td>$9.99</td><td>20%</td><td>Jul 6, 2013 8:14 AM</td></tr>
        <tr><td>21</td><td>33</td><td>$19.99</td><td>25%</td><td>Dec 10, 2012 5:14 AM</td></tr>
        <tr><td>013</td><td>18</td><td>$65.89</td><td>45%</td><td>Jan 12, 2013 11:14 AM</td></tr>
        <tr><td>005</td><td>45</td><td>$153.19</td><td>45%</td><td>Jan 18, 2014 9:12 AM</td></tr>
        <tr><td>10</td><td>3</td><td>$5.29</td><td>4%</td><td>Jan 8, 2013 5:11 PM</td></tr>
        <tr><td>16</td><td>24</td><td>$14.19</td><td>14%</td><td>Jan 14, 2014 11:23 AM</td></tr>
        <tr><td>66</td><td>22</td><td>$13.19</td><td>11%</td><td>Jan 18, 2013 9:12 AM</td></tr>
        <tr><td>100</td><td>18</td><td>$55.20</td><td>15%</td><td>Feb 12, 2013 7:23 PM</td></tr>
        <tr><td>55</td><td>65</td><td>$123.00</td><td>32%</td><td>Jan 20, 2014 1:12 PM</td></tr>
        <tr><td>9</td><td>25</td><td>$22.09</td><td>17%</td><td>Jun 11, 2013 10:55 AM</td></tr>
        <tr><td>13</td><td>12</td><td>$19.99</td><td>18%</td><td>Jan 20, 2014 7:45 PM</td></tr>
        <tr><td>73</td><td>58</td><td>$129.19</td><td>39%</td><td>Jan 20, 2014 10:11 AM</td></tr>
      </tbody>
    </table>

    <h3>Reflow   columnSelector widget</h3>
    <!-- This selector markup is completely customizable -->
    <div class="columnSelectorWrapper">
      <input id="colSelect1" type="checkbox" class="hidden">
      <label class="columnSelectorButton" for="colSelect1">Column</label>
      <div id="columnSelector" class="columnSelector">
        <!-- this div is where the column selector is added -->
      </div>
    </div> (When "Auto" is set, the table becomes responsive; resize the browser window to see it work)

    <table id="table2">
      <thead>
        <tr>
          <th data-priority="critical">Name</th>
          <th data-priority="critical">Major</th>
          <th data-priority="6" data-name="Gender">Sex</th>
          <th data-priority="4">English</th>
          <th data-priority="5">Japanese</th>
          <th data-priority="3">Calculus</th>
          <th data-priority="2">Geometry</th>
        </tr>
      </thead>
      <tfoot>
        <tr><th>Name</th><th>Major</th><th>Sex</th><th>English</th><th>Japanese</th><th>Calculus</th><th>Geometry</th></tr>
      </tfoot>
      <tbody>
        <tr><td>Student03</td><td>Languages</td><td>female</td><td>85</td><td>95</td><td>80</td><td>85</td></tr>
        <tr><td>Student04</td><td>Languages</td><td>male</td><td>60</td><td>55</td><td>100</td><td>100</td></tr>
        <tr><td>Student05</td><td>Languages</td><td>female</td><td>68</td><td>80</td><td>95</td><td>80</td></tr>
        <tr><td>Student12</td><td>Mathematics</td><td>female</td><td>100</td><td>75</td><td>70</td><td>85</td></tr>
        <tr><td>Student13</td><td>Languages</td><td>female</td><td>100</td><td>80</td><td>100</td><td>90</td></tr>
        <tr><td>Student14</td><td>Languages</td><td>female</td><td>50</td><td>45</td><td>55</td><td>90</td></tr>
        <tr><td>Student15</td><td>Languages</td><td>male</td><td>95</td><td>35</td><td>100</td><td>90</td></tr>
        <tr><td>Student16</td><td>Languages</td><td>female</td><td>100</td><td>50</td><td>30</td><td>70</td></tr>
        <tr><td>Student17</td><td>Languages</td><td>female</td><td>80</td><td>100</td><td>55</td><td>65</td></tr>
        <tr><td>Student18</td><td>Mathematics</td><td>male</td><td>30</td><td>49</td><td>55</td><td>75</td></tr>
        <tr><td>Student19</td><td>Languages</td><td>male</td><td>68</td><td>90</td><td>88</td><td>70</td></tr>
        <tr><td>Student20</td><td>Mathematics</td><td>male</td><td>40</td><td>45</td><td>40</td><td>80</td></tr>
        <tr><td>Student01</td><td>Languages</td><td>male</td><td>80</td><td>70</td><td>75</td><td>80</td></tr>
        <tr><td>Student02</td><td>Mathematics</td><td>male</td><td>90</td><td>88</td><td>100</td><td>90</td></tr>
        <tr><td>Student06</td><td>Mathematics</td><td>male</td><td>100</td><td>99</td><td>100</td><td>90</td></tr>
        <tr><td>Student07</td><td>Mathematics</td><td>male</td><td>85</td><td>68</td><td>90</td><td>90</td></tr>
        <tr><td>Student08</td><td>Languages</td><td>male</td><td>100</td><td>90</td><td>90</td><td>85</td></tr>
        <tr><td>Student09</td><td>Mathematics</td><td>male</td><td>80</td><td>50</td><td>65</td><td>75</td></tr>
        <tr><td>Student10</td><td>Languages</td><td>male</td><td>85</td><td>100</td><td>100</td><td>90</td></tr>
        <tr><td>Student11</td><td>Languages</td><td>male</td><td>86</td><td>85</td><td>100</td><td>100</td></tr>
      </tbody>
    </table>

    <h3>Reflow2 widget (multiple thead rows)</h3>
    <table id="table3">
      <thead>
        <tr>
          <th class="ui-table-reflow-ignore sorter-false">Paris</th>
          <th colspan="2">Average Temperatures (C)</th>
          <th colspan="2">Average Rainfall</th>
        </tr>
        <tr>
          <th>Month</th>
          <th>Minimum Temp</th>
          <th>Maximum Temp</th>
          <th>Precipitaion (mm)</th>
          <th>Rainfall Days</th>
        </tr>
      </thead>
      <tbody>
        <tr><th>January</th><td>3</td><td>8</td><td>17.8</td><td>10</td></tr>
        <tr><th>February</th><td>2</td><td>9</td><td>21.7</td><td>9</td></tr>
        <tr><th>March</th><td>4</td><td>13</td><td>24.2</td><td>10</td></tr>
        <tr><th>April</th><td>6</td><td>15</td><td>24.6</td><td>11</td></tr>
        <tr><th>May</th><td>10</td><td>20</td><td>26.2</td><td>10</td></tr>
        <tr><th>June</th><td>13</td><td>23</td><td>25.1</td><td>9</td></tr>
        <tr><th>July</th><td>15</td><td>25</td><td>21.7</td><td>7</td></tr>
        <tr><th>August</th><td>15</td><td>25</td><td>21.4</td><td>7</td></tr>
        <tr><th>September</th><td>11</td><td>21</td><td>15.6</td><td>8</td></tr>
        <tr><th>October</th><td>9</td><td>17</td><td>25.3</td><td>11</td></tr>
        <tr><th>November</th><td>5</td><td>11</td><td>22.4</td><td>12</td></tr>
        <tr><th>December</th><td>3</td><td>8</td><td>26.6</td><td>12</td></tr>
      </tbody>
    </table>

# Javascript

    $(function() {

    	// simple reflow widget (table with 1 header row)
    	$("#table1").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra', 'reflow'],
    		widgetOptions : {
    			// class name added to make it responsive (class name within media query)
    			reflow_className    : 'ui-table-reflow',
    			// header attribute containing modified header name
    			reflow_headerAttrib : 'data-name',
    			// data attribute added to each tbody cell
    			// it contains the header cell text, visible upon reflow
    			reflow_dataAttrib   : 'data-title'
    		}
    	});

    	// simple reflow widget + columnSelector & stickyHeaders widgets
    	$("#table2").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra', 'reflow', 'columnSelector', 'stickyHeaders'],
    		widgetOptions : {
    			// target the column selector markup
    			columnSelector_container : $('#columnSelector'),
    			// data attribute containing column name to use in the selector container
    			// make it use the same as reflow_headerAttrib
    			columnSelector_name : 'data-name',

    			// header attribute containing modified header name
    			reflow_headerAttrib : 'data-name'
    		}
    	});

    	// reflow2 widget (table with multiple header rows)
    	$("#table3").tablesorter({
    		theme: 'blue',
    		widgets: ['zebra', 'reflow2'],
    		widgetOptions: {
    			// class name added to make it responsive (class name within media query)
    			reflow2_className    : 'ui-table-reflow',
    			// ignore header cell content with this class name
    			reflow2_classIgnore  : 'ui-table-reflow-ignore',
    			// header attribute containing modified header name
    			reflow2_headerAttrib : 'data-name',
    			// class name applied to thead labels
    			reflow2_labelClass   : 'ui-table-cell-label',
    			// class name applied to first row thead label
    			reflow2_labelTop     : 'ui-table-cell-label-top'
    		}
    	});

    });