Skip to content

Commit 1f6f8c8

Browse files
committed
Update the docs and README based on the refactor
1 parent c9d97c2 commit 1f6f8c8

3 files changed

Lines changed: 70 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22

33
### [Demo](http://github.hubspot.com/sortable/docs/welcome)    [Documentation](http://github.hubspot.com/sortable)
44

5-
Sortable is an open-source javascript and CSS library which adds sorting functionality to tables. It is tiny (`<2kb`) and has no dependencies.
6-
7-
#### Credits
8-
9-
Loosely based on [Stuart Langridge](https://github.com/stuartlangridge)'s [SortTable](http://www.kryogenix.org/code/browser/sorttable/) ([History](https://github.com/HubSpot/sortable/commits/e068642453bb39eed676ed338e9bbeb372ca74c4/sorttable.js), [Original](https://github.com/HubSpot/sortable/blob/37894eb51bda9f0e438d735967f16eef0d403ef9/sorttable.js))
5+
Sortable is an open-source JavaScript and CSS library which adds sorting functionality to tables. It is tiny (`<2kb` min+gzip) and has no dependencies.

docs/api/1-Options.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Example:
1616

1717
All tables on the page will be automatically initted when the page is loaded.
1818

19-
If you add tables with javascript, call `init` after they are added to the page:
19+
If you add tables with JavaScript, call `init` after they are added to the page:
2020

2121
```coffeescript
2222
Sortable.init()
@@ -68,6 +68,68 @@ To disable sorting on a particular column, add `data-sortable="false"` to the `<
6868
</table>
6969
```
7070

71+
##### `th` `data-sortable-type="TYPE_NAME"`
72+
73+
By default, the `type` of data in each column is determined by reading the first cell of a column and trying to `match` it to the list of types. To specify a type directly, use `data-sortable-type`.
74+
75+
The default types supplied by Sortable are `alpha`, `numeric`, and `date`.
76+
77+
```html
78+
<table data-sortable>
79+
<thead>
80+
<tr>
81+
<th data-sortable-type="alpha">Numbers sorted alphabetically</th>
82+
<!-- ... -->
83+
</tr>
84+
</thead>
85+
<tbody>
86+
<tr>
87+
<td>10</td>
88+
<td>2</td>
89+
<td>312</td>
90+
<td>4</td>
91+
</tr>
92+
<!-- ... -->
93+
</tbody>
94+
</table>
95+
```
96+
97+
#### Custom Types
98+
99+
The default types supplied by Sortable are `alpha`, `numeric`, and `date`. To supply you own, call `Sortable.setupTypes(customTypesArray)` and pass in your custom types array.
100+
101+
Here’s how Sortable internally sets up the defaults.
102+
103+
```coffeescript
104+
sortable.setupTypes [{
105+
name: 'numeric'
106+
defaultSortDirection: 'descending'
107+
match: (a) -> a.match numberRegExp
108+
comparator: (a) -> parseFloat(a.replace(/[^0-9.-]/g, ''), 10) or 0
109+
}, {
110+
name: 'date'
111+
defaultSortDirection: 'ascending'
112+
reverse: true
113+
match: (a) -> not isNaN Date.parse a
114+
comparator: (a) -> Date.parse(a) or 0
115+
}, {
116+
name: 'alpha'
117+
defaultSortDirection: 'ascending'
118+
match: -> true
119+
compare: (a, b) -> a.localeCompare b
120+
}]
121+
```
122+
123+
Each type must specify the following:
124+
125+
- A `name` which is used to identify the type, in particular the `data-sortable-type="NAME"` attibute which can be specified on a `th`.
126+
- A `defaultSortDirection` which can be either `ascending` or `descending`.
127+
- A `match` function which is used to decide which columns are which types (unless `data-sortable-type` is specified).
128+
- Either a `comparator` or a `compare` function for the custom sorting handled by this type:
129+
- `comparator` is used when you want to simply write a function to process the values before the each sort comparison is made by Sortable.
130+
- `compare` is used when you want to actually do handle the entire sort comparison yourself.
131+
- Optionally specify `reverse` to change what `ascending` and `descending` mean with respect to the sort direction of the data for this column type.
132+
71133
<!-- Resources for the demos -->
72134
<p style="-webkit-transform: translateZ(0)"></p>
73135
<script src="/sortable/js/sortable.js"></script>

docs/intro.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Sortable
22

3-
Sortable is an open-source javascript and CSS library which adds sorting functionality to tables. It is tiny (<code>&lt;2kb</code>) and has no dependencies.
3+
Sortable is an open-source JavaScript and CSS library which adds sorting functionality to tables. It is tiny (<code>&lt;2kb</code> min+gzip) and has no dependencies.
44

55
#### Demo
66

7-
Here is a demo of the "Bootstrap" theme.
7+
Here is a demo of the Bootstrap theme.
88

99
<p style="display: none"></p>
1010
<table class="sortable-theme-bootstrap" data-sortable> <thead> <tr> <th data-sortable="false">Browser</th> <th data-sorted="true" data-sorted-direction="descending">Usage</th> <th>Initial release</th> <th>Stable version</th> </tr> </thead> <tbody> <tr> <td>Chrome</td> <td>42.68%</td> <td data-value="2008">September 2, 2008</td> <td>31.0.1650.57</td> </tr> <tr> <td>Internet Explorer</td> <td>25.44%</td> <td data-value="1995">August 16, 1995</td> <td>11.0.1</td> </tr> <tr> <td>Firefox</td> <td>20.01%</td> <td data-value="2002">September 23, 2002</td> <td>25.0.1</td> </tr> <tr> <td>Safari</td> <td>8.39%</td> <td data-value="2003">January 7, 2003</td> <td>7.0</td> </tr> <tr> <td>Opera</td> <td>1.03%</td> <td data-value="1994">Late 1994</td> <td>18.0.1284.49</td> </tr> <tr> <td>Other</td> <td data-value="0">2.44%</td> <td data-value="-1"></td> <td></td> </tr> </tbody> </table>
@@ -14,7 +14,7 @@ Here is a demo of the "Bootstrap" theme.
1414

1515
- Drop-in script and styles
1616
- 6 beautiful CSS themes
17-
- Tiny footprint (<code>&lt;2kb</code>) and no dependencies
17+
- Tiny footprint (<code>&lt;2kb</code> min+gzip) and no dependencies
1818
- Looks and behaves great on mobile devices
1919

2020
#### Requirements
@@ -30,22 +30,22 @@ Here is a demo of the "Bootstrap" theme.
3030

3131
#### Including
3232

33-
For the most common usage of sortable, you'll want to include following:
33+
For the most common usage of sortable, youll want to include following:
3434

3535
```html
3636
<script src="sortable.min.js"></script>
3737
<link rel="stylesheet" href="sortable-theme-bootstrap.css" />
3838
```
3939

40-
Now any table with the attribute `sortable` will be made sortable. To get the styling, you'll also need to add a class name to the table to match the theme you chose:
40+
Now any table with the attribute `sortable` will be made sortable. To get the styling, youll also need to add a class name to the table to match the theme you chose:
4141

4242
```html
4343
<table class="sortable-theme-bootstrap" data-sortable>
4444
<!-- ... -->
4545
</table>
4646
```
4747

48-
This example uses the "bootstrap" theme. For a full list of supported themes, check out [Themes](/sortable/api/themes).
48+
This example uses the “Bootstrap” theme. For a full list of supported themes, check out [Themes](/sortable/api/themes).
4949

5050
#### Learn More
5151

0 commit comments

Comments
 (0)