File: README.md

package info (click to toggle)
libjs-jquery-stupidtable 1.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 544 kB
  • ctags: 10
  • sloc: makefile: 7
file content (231 lines) | stat: -rw-r--r-- 6,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Stupid jQuery Table Sort
========================

This is a stupid jQuery table sorting plugin. Nothing fancy, nothing really
impressive. Overall, stupidly simple. Requires jQuery 1.7 or newer.

[View the demo here][0]

See the example.html document to see how to implement it.


Example Usage
-------------

The JS:

    $("table").stupidtable();

The HTML:

    <table>
      <thead>
        <tr>
          <th data-sort="int">int</th>
          <th data-sort="float">float</th>
          <th data-sort="string">string</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>15</td>
          <td>-.18</td>
          <td>banana</td>
        </tr>
        ...
        ...
        ...

The thead and tbody tags must be used.

Add a `data-sort` attribute of "DATATYPE" to the th elements to make them sortable
by that data type. If you don't want that column to be sortable, just omit the
`data-sort` attribute.


Predefined data types
---------------------

Our aim is to keep this plugin as lightweight as possible. Consequently, the
only predefined datatypes that you can pass to the th elements are

* `int`
* `float`
* `string` (case-sensitive)
* `string-ins` (case-insensitive)

These data types will be sufficient for many simple tables. However, if you need
different data types for sorting, you can easily create your own!

Data with multiple representations/predefined order
---------------------------------------------------

Stupid Table lets you sort a column by computer friendly values while displaying
human friendly values via the `data-sort-value` attribute on a td element. For
example, to sort timestamps (computer friendly) but display pretty formated
dates (human friendly)

    <table>
      <thead>
        <tr>
          <th data-sort="string">Name</th>
          <th data-sort="int">Birthday</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Joe McCullough</td>
          <td data-sort-value="672537600">April 25, 1991</td>
        </tr>
        <tr>
          <td>Clint Dempsey</td>
          <td data-sort-value="416016000">March 9, 1983</td>
        </tr>
        ...
        ...
        ...

In this example, Stupid Table will sort the Birthday column by the timestamps
provided in the `data-sort-value` attributes of the corresponding tds. Since
timestamps are integers, and that's what we're sorting the column by, we specify
the Birthday column as an `int` column in the `data-sort` value of the column
header.


Default sorting direction
-------------------------

By default, columns will sort ascending. You can specify a column to sort "asc"
or "desc" first.

    <table>
      <thead>
        <tr>
            <th data-sort="float" data-sort-default="desc">float</th>
            ...
        </tr>
      </thead>
    </table>

Sorting a column programatically
--------------------------------

After you have called `$("#mytable").stupidtable()`, if you wish to sort a
column without requiring the user to click on it, select the column th and call


    var $table = $("#mytable").stupidtable();
    var $th_to_sort = $table.find("thead th").eq(0);
    $th_to_sort.stupidsort();

    // You can also force a direction.
    $th_to_sort.stupidsort('asc');
    $th_to_sort.stupidsort('desc');

Updating a table cell's value
-----------------------------

If you wish for Stupid Table to respond to changes in the table cell values, you
must explicitely inform Stupid Table to update its cache with the new values. If
you update the table display/sort values without using this mechanism, your
newly updated table **will not sort correctly!**

    /*
     * Suppose $age_td is some td in a table under a column specified as an int
     * column. stupidtable() must already be called for this table.
     */
    $age_td.updateSortVal(23);

Note that this only changes the internal sort value (whether you specified a
`data-sort-value` or not). Use the standard jQuery `.text()` / `.html()` methods
if you wish to change the display values.


Callbacks
---------

To execute a callback function after a table column has been sorted, you can
bind on `aftertablesort`.

    var table = $("table").stupidtable();
    table.bind('aftertablesort', function (event, data) {
        // data.column - the index of the column sorted after a click
        // data.direction - the sorting direction (either asc or desc)
        // $(this) - this table object

        console.log("The sorting direction: " + data.direction);
        console.log("The column index: " + data.column);
    });

Similarly, to execute a callback before a table column has been sorted, you can
bind on `beforetablesort`.

See the complex_example.html file.

Creating your own data types
----------------------------

Sometimes you don't have control over the HTML produced by the backend. In the
event you need to sort complex data without a `data-sort-value` attribute, you
can create your own data type. Creating your own data type for sorting purposes
is easy as long as you are comfortable using custom functions for sorting.
Consult [Mozilla's Docs][1] if you're not.

Let's create an alphanum datatype for a User ID that takes strings in the form
"D10", "A40", and sorts the column based on the numbers in the string.

    <thead>
      <tr>
        <th data-sort="string">Name</th>
        <th data-sort="int">Age</th>
        <th data-sort="alphanum">UserID</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Joseph McCullough</td>
        <td>20</td>
        <td>D10</td>
      </tr>
      <tr>
        <td>Justin Edwards</td>
        <td>29</td>
        <td>A40</td>
      </tr>
      ...
      ...
      ...

Now we need to specify how the **alphanum** type will be sorted. To do that,
we do the following:

    $("table").stupidtable({
      "alphanum":function(a,b){

        var pattern = "^[A-Z](\\d+)$";
        var re = new RegExp(pattern);

        var aNum = re.exec(a).slice(1);
        var bNum = re.exec(b).slice(1);

        return parseInt(aNum,10) - parseInt(bNum,10);
      }
    });

This extracts the integers from the cell and compares them in the style
that sort functions use.

License
-------

The Stupid jQuery Plugin is licensed under the MIT license. See the LICENSE
file for full details.

Tests
-----

Visit `tests/test.html` in your browser to run the QUnit tests.


[0]: http://joequery.github.io/Stupid-Table-Plugin/
[1]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort