comparison default/node_modules/tablesaw/README.md @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 # Tablesaw
2
3 [![npm version](https://badge.fury.io/js/tablesaw.svg)](https://badge.fury.io/js/tablesaw)
4 [![Build Status](https://img.shields.io/travis/filamentgroup/tablesaw/master.svg)](https://travis-ci.org/filamentgroup/tablesaw)
5 [![Dependency Status](https://david-dm.org/filamentgroup/tablesaw.svg?theme=shields.io)](https://david-dm.org/filamentgroup/tablesaw)
6
7 [![Filament Group](http://filamentgroup.com/images/fg-logo-positive-sm-crop.png) ](http://www.filamentgroup.com/)
8
9 A set of plugins for responsive tables.
10
11 * [Getting Started](#getting-started)
12 * [Stack Mode](#stack-mode)
13 * [Column Toggle Mode](#column-toggle-mode)
14 * [Swipe Mode](#swipe-mode)
15 * [Mini-Map](#mini-map)
16 * [Mode Switcher](#mode-switcher)
17 * [Sortable](#sortable)
18 * [Kitchen Sink Example](http://filamentgroup.github.io/tablesaw/demo/kitchensink.html)
19 * [Check All](#check-all)
20 * [Internationalization i18n](#internationalization-i18n)
21 * [Limitations](#limitations)
22 * [Run Tests](http://filamentgroup.github.io/tablesaw/test-qunit/tablesaw.html)
23 * [Browser Support](#browser-support)
24 * [Bundler Compatibility](#bundler-compatibility)
25
26 ## Stack Mode
27
28 * [Stack Demo](http://filamentgroup.github.io/tablesaw/demo/stack.html) and [Stack-Only Demo](http://filamentgroup.github.io/tablesaw/demo/stackonly.html)
29
30 The Stack Table stacks the table headers to a two column layout with headers on the left when the viewport width is less than `40em` (`640px`).
31
32 ![](docs/stack.gif)
33
34 ```html
35 <table class="tablesaw tablesaw-stack" data-tablesaw-mode="stack">
36 ```
37
38 If you only want to use the Stack Table and don’t want all the extra features below (save yourself some bytes), Tablesaw provides a Stack-Only version.
39
40 | Option | Description |
41 | --- | --- |
42 | Opt out of inline labels | To opt-out of inline label creation (the table header cell text that shows at small breakpoints) on a per-table basis, use `<table data-tablesaw-no-labels>`; on a per-row basis, use `<tr data-tablesaw-no-labels>`. |
43 | Hide headers for empty body cells | When the table cell is empty, use `<table data-tablesaw-hide-empty>` to hide the header when stacked. |
44
45 ## Column Toggle Mode
46
47 * [Column Toggle Demo](http://filamentgroup.github.io/tablesaw/demo/toggle.html)
48
49 The Column Toggle Table allows the user to select which columns they want to be visible.
50
51 ![](docs/columntoggle-minimap.gif)
52
53 ```html
54 <table data-tablesaw-mode="columntoggle">
55 ```
56
57 | Option | Description |
58 | --- | --- |
59 | Add a Mini-Map | The little dots that appear next to the column toggle popup. Use the `data-tablesaw-minimap` attribute: `<table data-tablesaw-mode="columntoggle" data-tablesaw-minimap>` |
60
61 The user always has the option to select all columns. If the table gets too wide for the viewport, it can overflow and cause a page-level scrollbar. To combat this issue, we recommend wrapping your table in a `<div class="tablesaw-overflow">` element to restrict scrolling to the table-only. The [toggle demo](http://filamentgroup.github.io/tablesaw/demo/toggle.html) has one such example.
62
63 <details>
64 <summary><em>Advanced Option</em>: Prioritize Columns</summary>
65
66 Table headers must have a `data-tablesaw-priority` attribute to be eligible to toggle. `data-tablesaw-priority` is a numeric value from 1 to 6, which determine default breakpoints at which a column will show. The breakpoint defaults are:
67
68 ```html
69 <th data-tablesaw-priority="persist"><!-- Not eligible for toggle, always shows --></th>
70 <th data-tablesaw-priority="0"><!-- Hidden at all breakpoints by default, must be toggled back on manually --></th>
71 <th data-tablesaw-priority="1"><!-- Shows at (min-width: 20em) (320px) --></th>
72 <th data-tablesaw-priority="2"><!-- Shows at (min-width: 30em) (480px) --></th>
73 <th data-tablesaw-priority="3"><!-- Shows at (min-width: 40em) (640px) --></th>
74 <th data-tablesaw-priority="4"><!-- Shows at (min-width: 50em) (800px) --></th>
75 <th data-tablesaw-priority="5"><!-- Shows at (min-width: 60em) (960px) --></th>
76 <th data-tablesaw-priority="6"><!-- Shows at (min-width: 70em) (1120px) --></th>
77 ```
78
79 Keep in mind that the priorities are not exclusive—multiple columns can reuse the same priority value.
80
81 </details>
82
83 ## Swipe Mode
84
85 * [Swipe Demo](http://filamentgroup.github.io/tablesaw/demo/swipe.html)
86
87 Allows the user to use the swipe gesture (or use the left and right buttons) to navigate the columns.
88
89 ![](docs/swipe-minimap.gif)
90
91 ```html
92 <table data-tablesaw-mode="swipe">
93 ```
94
95
96 | Options | Description |
97 | --- | --- |
98 | Persist a Column | Columns also respect the `data-tablesaw-priority="persist"` attribute: `<th data-tablesaw-priority="persist"><!-- Always shows --></th>` |
99 | Add a Mini-Map | The little dots that appear next to the column navigation buttons. Use the `data-tablesaw-minimap` attribute: `<table data-tablesaw-mode="swipe" data-tablesaw-minimap>` |
100 | All columns visible class | Tablesaw also exposes a `tablesaw-all-cols-visible` class that is toggled on when all of the table columns are visible (and off when not). You can use this in CSS to hide the minimap or navigation buttons if needed. |
101 | Disable swipe touch events | Use the `<table data-tablesaw-no-touch>` attribute to opt-out of swiping left or right to navigate columns. Users will need to use the provided buttons instead. |
102
103 <details>
104 <summary><em>Advanced Option</em>: Configure Swipe Thresholds</summary>
105
106 Add a `TablesawConfig` object to your page in a `<script>` element. It doesn’t matter if it’s declared before or after the Tablesaw JavaScript.
107
108 ```js
109 <script>
110 TablesawConfig = {
111 swipeHorizontalThreshold: 15,
112 swipeVerticalThreshold: 20
113 };
114 </script>
115 ```
116
117 * [Configure Swipe Threshold Demo](http://filamentgroup.github.io/tablesaw/demo/swipe-config.html)
118
119 </details>
120
121 ## Mini Map
122
123 Use `data-tablesaw-minimap` to add a series of small dots to show which columns are currently visible and which are hidden. Only available on `swipe` and `columntoggle` tables. Examples available above.
124
125 ## Mode Switcher
126
127 * [Mode Switcher Demo](http://filamentgroup.github.io/tablesaw/demo/modeswitch.html)
128
129 ![](docs/mode-switch.gif)
130
131 ```html
132 <table data-tablesaw-mode-switch>
133
134 <!-- With a different default mode -->
135 <table data-tablesaw-mode-switch data-tablesaw-mode="swipe">
136
137 <!-- Exclude a mode from the switcher -->
138 <table data-tablesaw-mode-switch data-tablesaw-mode-exclude="columntoggle">
139 ```
140
141 ## Sortable
142
143 * [Sortable Demo](http://filamentgroup.github.io/tablesaw/demo/sort.html)
144
145 The “sortable” option allows the user to sort the table data by clicking on the table headers. Since all the columns may not be visible on smaller breakpoints (or not there at all if using the “stack” table mode), relying solely on the column headers to choose the table sort isn’t practical. To address this, there is an optional `data-tablesaw-sortable-switch` attribute on the table that adds a select menu auto-populated with the names of each column in the table with options for choosing ascending or descending sort direction. Data options on table headers can be used to control which columns are sortable (`data-tablesaw-sortable-col`) and the default sort order (`data-tablesaw-sortable-default-col`).
146
147 ```html
148 <table data-tablesaw-sortable>
149 <thead>
150 <tr>
151 <!-- Default column -->
152 <th data-tablesaw-sortable-col data-tablesaw-sortable-default-col>Rank</th>
153 <th data-tablesaw-sortable-col>Movie Title</th>
154 <th data-tablesaw-sortable-col data-tablesaw-sortable-numeric>Year</th>
155 <th data-tablesaw-sortable-col data-tablesaw-sortable-numeric><abbr title="Rotten Tomato Rating">Rating</abbr></th>
156 <!-- Unsortable column -->
157 <th>Reviews</th>
158 </tr>
159 </thead>
160 ...
161 ```
162
163 Use `data-tablesaw-sortable-switch` to add a select form element to manually choose the sort order.
164
165 ```html
166 <table data-tablesaw-sortable data-tablesaw-sortable-switch>
167 ```
168
169 ![](docs/sortable.png)
170
171 <details>
172 <summary><em>Advanced Option</em>: Custom Sort Functions</summary>
173
174 Tablesaw provides two methods of sorting built-in: string and numeric. To use numeric sort, use the `data-tablesaw-sortable-numeric` class as shown in the above sorting markup example. Otherwise, tablesaw uses a case insensitive string sort.
175
176 All other types of sorting must use a Custom Sort function on the individual columns ([working example](http://filamentgroup.github.io/tablesaw/demo/sort-custom.html)). In the contrived example below, we want to sort full dates (e.g. `12/02/2014`) just on the year.
177
178 ```
179 // Add a data function to the table header cell
180 $( "th#custom-sort" ).data( "tablesaw-sort", function( ascending ) {
181 // return a function
182 return function( a, b ) {
183 // Ignore rows with data-tablesaw-ignorerow (leave them where they were)
184 if( a.ignored || b.ignored ) {
185 return 0;
186 }
187
188 // use a.cell and b.cell for cell values
189 var dateA = a.cell.split( "/" ),
190 dateB = b.cell.split( "/" ),
191 yearA = parseInt( dateA[ 2 ], 10 ),
192 yearB = parseInt( dateB[ 2 ], 10 );
193
194 if( ascending ) {
195 return yearA >= yearB ? 1 : -1;
196 } else { // descending
197 return yearA < yearB ? 1 : -1;
198 }
199 };
200 });
201 ```
202
203 </details>
204
205 ## Kitchen ~~Table~~ Sink
206
207 * [Kitchen Sink Demo](http://filamentgroup.github.io/tablesaw/demo/kitchensink.html)
208
209 All of the above options combined into a single table.
210
211 ## Check All
212
213 _Added in 3.0.1._ Add the `data-tablesaw-checkall` to a checkbox in a `thead` cell to enable that checkbox to toggle the other checkboxes in the same column.
214
215 * [Check All Demo](http://filamentgroup.github.io/tablesaw/demo/checkall.html)
216
217 ## Internationalization i18n
218
219 _Added in 3.0.2._ Use the `TablesawConfig` global on your page to override internationalization strings. It doesn’t matter if it’s declared before or after the Tablesaw JavaScript library.
220
221 ```js
222 <script>
223 TablesawConfig = {
224 i18n: {
225 modeStack: 'Stack',
226 modeSwipe: 'Swipe',
227 modeToggle: 'Toggle',
228 modeSwitchColumnsAbbreviated: 'Cols',
229 modeSwitchColumns: 'Columns',
230 columnToggleButton: 'Columns',
231 columnToggleError: 'No eligible columns.',
232 sort: 'Sort',
233 swipePreviousColumn: 'Previous column',
234 swipeNextColumn: 'Next column'
235 }
236 };
237 </script>
238 ```
239
240 ## Getting Started
241
242 Available through npm:
243
244 ```
245 npm install tablesaw
246 ```
247
248 ### The Full Tablesaw
249
250 <details open>
251 <summary>Tablesaw (no dependencies)</summary>
252
253 ```html
254 <link rel="stylesheet" href="tablesaw.css">
255 <script src="tablesaw.js"></script>
256 <script src="tablesaw-init.js"></script>
257 ```
258
259 </details>
260
261 <details open>
262 <summary>or Tablesaw (jQuery Plugin)</summary>
263
264 ```html
265 <link rel="stylesheet" href="tablesaw.css">
266 <!-- load your own jQuery -->
267 <script src="jquery.js"></script>
268 <script src="tablesaw.jquery.js"></script>
269 <script src="tablesaw-init.js"></script>
270 ```
271
272 </details>
273
274 Don’t forget to add your table markup! For a stack table, this is how it’d look:
275
276 ```html
277 <table class="tablesaw tablesaw-stack" data-tablesaw-mode="stack">
278 ```
279
280 The demos above include full markup examples for all of the Tablesaw types.
281
282 #### Manual initialization of Tablesaw Components
283
284 If you want to initialize your Tablesaw tables manually, don’t include `<script src="tablesaw-init.js">` in your markup. Instead, you can use `Tablesaw.init()`. This will scan the tree for any Tablesaw tables and initialize them for you.
285
286 *Tables must be visible for proper initialization.*
287
288 ```js
289 Tablesaw.init();
290 Tablesaw.init( myElement ); // OR pass an element to only init within a context
291 ```
292
293 ### Using Stack-Only Tablesaw
294
295 * [Stack-Only Demo](http://filamentgroup.github.io/tablesaw/demo/stackonly.html)
296
297 As shown above, we provide a Stack-mode-only package of Tablesaw. It’s a barebones version that doesn’t include any of the other features above.
298
299 <details open>
300 <summary>Stack-only Tablesaw (no dependencies)</summary>
301
302 ```html
303 <link rel="stylesheet" href="tablesaw.css">
304 <script src="stackonly/tablesaw.stackonly.js"></script>
305 <script src="tablesaw-init.js"></script>
306 ```
307
308 </details>
309
310 <details open>
311 <summary>or just Stack-only Tablesaw (jQuery Plugin)</summary>
312
313 ```html
314 <link rel="stylesheet" href="tablesaw.css">
315 <!-- load your own jQuery -->
316 <script src="jquery.js"></script>
317 <script src="stackonly/tablesaw.stackonly.jquery.js"></script>
318 <script src="tablesaw-init.js"></script>
319 ```
320
321 </details>
322
323 And then:
324
325 ```html
326 <table class="tablesaw tablesaw-stack" data-tablesaw-mode="stack">
327 ```
328
329 ### Using Stack-Only Tablesaw SCSS Mixin
330
331 To easily customize the breakpoint at which the stack table switches, use the SCSS mixin. First, include the `tablesaw.stackonly.scss` file instead of `tablesaw.stackonly.css` in your SASS. Then, use a parent selector on your table.
332
333 ```html
334 <div class="my-parent-selector">
335 <table class="tablesaw" data-tablesaw-mode="stack">
336 ```
337
338 Include the mixin like so:
339
340 ```scss
341 .my-parent-selector {
342 @include tablesaw-stack( 50em );
343 }
344 ```
345
346 The argument to `tablesaw-stack` is the breakpoint at which the table will switch from columns to stacked.
347
348 ### Default Styles
349
350 _Starting with Tablesaw 3.0, the “Bare”, or stripped down style version of Tablesaw has been made the default._
351
352 Some of the more intrusive default styles have instead moved to opt-in classes you can add to the `<table>` element:
353
354 * `tablesaw-row-border`: Adds a bottom border to each table row.
355 * `tablesaw-row-zebra`: Adds a light background color to every other table row.
356 * `tablesaw-swipe-shadow`: Adds the light shadow to the right of persistant columns to make them stand out a little more.
357
358 ## Limitations
359
360 * Simple `colspan` and `rowspan` are supported, in part thanks to a [lovely PR](https://github.com/filamentgroup/tablesaw/pull/225) from @jgibson.
361
362 | | Stack | Column Toggle | Swipe | Sortable |
363 | --- | --- | --- | --- | --- |
364 | `rowspan` | _Not yet supported_ ([#247](https://github.com/filamentgroup/tablesaw/issues/247)) | Supported | Supported | _Not yet supported_ ([#268](https://github.com/filamentgroup/tablesaw/issues/268)) |
365 | `colspan` | Supported | Supported | Supported | Supported |
366
367 ## [Tests](http://filamentgroup.github.io/tablesaw/test-qunit/tablesaw.html)
368
369 ## Browser Support
370
371 All major browsers (evergreens are not listed, but supported). Notably this project cuts the mustard for A-grade support with:
372
373 * Internet Explorer 9+
374 * Android Browser 2.3+
375 * Blackberry OS 6+
376
377 Other legacy browsers and Opera Mini receive unenhanced table markup.
378
379 ## Bundler Compatibility
380
381 * Added in `v3.0.6`: [tested to work in Webpack](./demo/webpack/).
382
383 ## Building the Project Locally
384
385 Run `npm install` to install dependencies and then `grunt` to build the project files into the `dist` folder.
386
387 ## Release Names
388
389 * [3.0.3: Cucumbertree](https://github.com/filamentgroup/tablesaw/releases/tag/v3.0.3)
390 * [3.0.2: Bald Cypress](https://github.com/filamentgroup/tablesaw/releases/tag/v3.0.2)
391 * [3.0.1: Cypress](https://github.com/filamentgroup/tablesaw/releases/tag/v3.0.1)
392 * [3.0.0: Rosewood](https://github.com/filamentgroup/tablesaw/releases/tag/v3.0.0)
393 * [2.0.1: Mountain Hemlock](https://github.com/filamentgroup/tablesaw/releases/tag/v2.0.1)
394 * [2.0.0: Hemlock](https://github.com/filamentgroup/tablesaw/releases/tag/v2.0.0)
395 * [1.0.5: Hickory](https://github.com/filamentgroup/tablesaw/releases/tag/v1.0.5)
396 * [1.0.4: Ironwood](https://github.com/filamentgroup/tablesaw/releases/tag/v1.0.4)
397 * [1.0.3: Red Mahogany](https://github.com/filamentgroup/tablesaw/releases/tag/v1.0.3)
398
399 _Previous versions didn’t have names._