# tablesorter

## Align Character Widget (beta)

### Flexible client-side table sorting

Back to documentation

### Notes

- This widget will only work in tablesorter version 2.8+ and jQuery version 1.7+.
- The widget is still in "beta", so options & functionality may change without warning; and, it has not been thoroughly tested!
- Make sure to include the CSS shown below to maintain the alignment.
- *NOTE* if the table cell becomes too narrow, the alignment will not be maintained & some content may overflow into the next/previous cell (the widget css does include `overflow:hidden` on the wrapper, so some content will be hidden instead of overflowing).
- Until the day that `text-align: '.';` becomes standard (ref), if ever, this widget might prove useful.

### Options

### Align Character (alignChar) widget default options (added inside of tablesorter `widgetOptions`)

Option | Default | Description
--- | --- | ---

alignChar_wrap

`''`

This option allows you to wrap the alignment character in any HTML:
When the widget completes the alignment, it is basically splitting the content of the table cell into two halves, and wrapping it in a span element. Below is an example of a modified cell showing all contents; it's from the "Animals" column (the cell that contains "Ox = stinky")

    <td>
    	<span class="ts-align-wrap">
    		<span class="ts-align-left" style="min-width:37%">Ox&nbsp;</span>
    		<span class="ts-align-right" style="min-width:63%">=&nbsp;stinky</span>
    	</span>
    </td>

When this option is set with some HTML:

    alignChar_wrap : '<i/>'

it results in this layout:

    <td>
    	<span class="ts-align-wrap">
    		<span class="ts-align-left" style="min-width:37%">Ox&nbsp;</span>
    		<span class="ts-align-right" style="min-width:63%">
    			<i>=</i>&nbsp;stinky
    		</span>
    	</span>
    </td>

alignChar_charAttrib

`'data-align-char'`

This option points to the header data-attribute which contains the desired alignment character.
Add it to the header as follows:

    <th data-align-char=".">Numeric</th>

The `data-align-char` data-attribute contains the actual character to align. There is no default alignment character, so if this data-attribute is undefined, the column will be ignored.
*NOTE* if wanting to align the content on a space (see the "AlphaNumeric" column), use `&nbsp;` instead of a regular space because all spaces within the content are replaced by non-breaking spaces.

    <th data-align-char="&nbsp;">AlphaNumeric</th>

alignChar_indexAttrib

`'data-align-index'`

This option points to the header data-attribute which contains the desired alignment character index (one-based index).

- What that means is if your data has multiple periods, like in the URLs of the sites column below, you can tell the widget which period to line-up on.
- The alignment may seem to act incorrectly, if your data doesn't have enough periods to line up on, but it's doing the best it can! For example, in the sites column below, a table cell contains `"rental.nytimes.com/index.html"`. This URL has three periods, while all other cells in that column only contain two. When the `data-align-index` is set to `"3"`, the widget aligns the content on that third period. All of the other columns only have two, so they all align on their last period. Does that make it clear?
- Try adjusting the sliders in this demo to change the `data-align-index` of the "Animals" and "Sites" columns to see how this works.

Add this attribute to the header as follows:

    <th data-align-char="=" data-align-index="1">Animals</th>

The `data-align-index` data-attribute contains the actual character index. The index defaults to "1" if this data-attribute is undefined.

alignChar_adjustAttrib

`'data-align-adjust'`

This option allows you to tweak the horizontal position of the aligned cell content.

- Here is an example of a modified cell showing its contents; it's from the "Numeric" column (the cell that contains "87.20000"):

      <td>
      		<span class="ts-align-wrap">
      			<span class="ts-align-left" style="min-width:36%">87</span>
      			<span class="ts-align-right" style="min-width:64%">.20000</span>
      		</span>
      	</td>

  As you can see, the "87" (integer-part) is in a "ts-align-left" span, set with a `text-align: right;` (it's on the left, but right aligned)
  The ".20000" (fractional-part) is in a "ts-align-right" span, set with a `text-align: left;` (opposite of the other one)!
- So in the above example, the left side is set to a width of "36%" and the right to "64%" (adding up to 100%), which "sort-of" centers the aligned character.
- If you don't want to move the position of the aligned character, you can set the `data-align-adjust` data-attribute with a percentage change. This value (positive or negative) will be added to the "ts-align-left" span, and subtracted from the "ts-align-right" span.
- Try adjusting the sliders in this demo to change the `data-align-adjust` of the "Sites" column to see how this works.

Add this attribute to the header as follows:

    <th data-align-char="." data-align-adjust="10">Numeric</th>

The `data-align-adjust` data-attribute contains a percentage value (without the percent sign). The adjustment defaults to "0" if this data-attribute is undefined.

### CSS

The following is *required* css. Without it, the alignment will be all wrong.

# Demo

Animals align index:
1 (only indexes of 1 or 2 will work)
Sites align index:
1 (only indexes of 1 - 3 will work
Sites adjust value:
0

AlphaNumeric | Numeric | Animals | Sites
--- | --- | --- | ---
abc 123 | .423475 | Koala = cute = cudley | search.google.com
abc 1 | 23.4 | Ox = stinky | mail.yahoo.com
abc 9 | 1.0 | Girafee = tall | http://www.facebook.com
zyx 24 | 7.67 | Bison = burger | http://internship.whitehouse.gov/
abc 11 | 3000 | Chimp = banana lover | about.ucla.edu/
abc 2 | 56.5 | Elephant = unforgetable | http://www.wikipedia.org/
abc 9 | 15.5 | Lion = rawr | rental.nytimes.com/index.html
ABC 10 | 87.20000 | Zebra = stripey | http://android.google.com
zyx 1 | 999.1 | Koala = cute, again! | http://irsmrt.mit.edu/
zyx 12 | 0.2 | Llama = llove it | http://aliens.nasa.gov/

# Page Header

    <!-- Tablesorter: required -->
    <link href="../css/theme.blue.css" rel="stylesheet">
    <script src="../js/jquery.tablesorter.js"></script>
    <script src="../js/jquery.tablesorter.widgets.js"></script>

    <!-- Align Character widget -->
    <script src="../js/widgets/widget-alignChar.js"></script>

# Script

# CSS

# HTML

    <table class="tablesorter">
      <thead>
        <tr>
          <th data-align-char="&nbsp;">AlphaNumeric</th>
          <th data-align-char="." data-align-adjust="0">Numeric</th>
          <th data-align-char="=" data-align-index="1">Animals</th>
          <th data-align-char="." data-align-index="1">Sites</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>abc 123</td>
          <td>.423475</td>
          <td>Koala = cute = cudley</td>
          <td>search.google.com</td>
        </tr>
        <tr>
          <td>abc 1</td>
          <td>23.4</td>
          <td>Ox = stinky</td>
          <td>mail.yahoo.com</td>
        </tr>
        <tr>
          <td>abc 9</td>
          <td>1.0</td>
          <td>Girafee = tall</td>
          <td>http://www.facebook.com</td>
        </tr>
        <tr>
          <td>zyx 24</td>
          <td>7.67</td>
          <td>Bison = burger</td>
          <td>http://internship.whitehouse.gov/</td>
        </tr>
        <tr>
          <td>abc 11</td>
          <td>3000</td>
          <td>Chimp = banana lover</td>
          <td>about.ucla.edu/</td>
        </tr>
        <tr>
          <td>abc 2</td>
          <td>56.5</td>
          <td>Elephant = unforgetable</td>
          <td>http://www.wikipedia.org/</td>
        </tr>
        <tr>
          <td>abc 9</td>
          <td>15.5</td>
          <td>Lion = rawr</td>
          <td>rental.nytimes.com/index.html</td>
        </tr>
        <tr>
          <td>ABC 10</td>
          <td>87.20000</td>
          <td>Zebra = stripey</td>
          <td>http://android.google.com</td>
        </tr>
        <tr>
          <td>zyx 1</td>
          <td>999.1</td>
          <td>Koala = cute, again!</td>
          <td>http://irsmrt.mit.edu/</td>
        </tr>
        <tr>
          <td>zyx 12</td>
          <td>0.2</td>
          <td>Llama = llove it</td>
          <td>http://aliens.nasa.gov/</td>
        </tr>
      </tbody>
    </table>