|
19 | 19 | <script src="grid.js"></script>
|
20 | 20 | <script type="text/javascript">
|
21 | 21 | $(function() {
|
22 |
| - var grid = $( "table" ).grid().delegate( "th", "click", function() { |
23 |
| - |
24 |
| - var header = $( this ), |
25 |
| - type = header.data( "type" ), |
26 |
| - culture = header.data( "culture" ), |
27 |
| - format = header.data( "format" ), |
28 |
| - order = header.data("sort-order") || 1; |
29 |
| - // TODO map this.cellIndex to field name instead |
30 |
| - var field = header.text().toLowerCase().replace( /\s|[^a-z0-9]/g, "_" ); |
31 |
| - var items = $.ui.datastore.main.get( grid.options.type ).options.items; |
32 |
| - function extract( row ) { |
33 |
| - var text = row.options.data[ field ]; |
34 |
| - if (type == "currency") { |
35 |
| - return $.global.parseFloat(text, culture); |
36 |
| - } else if (type == "date") { |
37 |
| - return $.global.parseDate(text, format, culture); |
38 |
| - } else { |
39 |
| - return text; |
| 22 | + // just a temporary design to show what a new widget inheriting from |
| 23 | + // grid could look like |
| 24 | + // metadata such as column type, culture, format and sort-order probably |
| 25 | + // belongs in the model |
| 26 | + // all that this widget should handle is the click on the column header, |
| 27 | + // creating a custom event with enough metadata to implement |
| 28 | + // sorting elsewhere |
| 29 | + $.widget( "ui.sortablegrid", $.ui.grid, { |
| 30 | + _create: function() { |
| 31 | + $.ui.grid.prototype._create.apply( this, arguments ); |
| 32 | + var that = this; |
| 33 | + this.element.delegate( "th", "click", function() { |
| 34 | + that._sort( $( this ) ); |
| 35 | + }); |
| 36 | + }, |
| 37 | + _sort: function( header ) { |
| 38 | + var type = header.data( "type" ), |
| 39 | + culture = header.data( "culture" ), |
| 40 | + format = header.data( "format" ), |
| 41 | + order = header.data("sort-order") || 1; |
| 42 | + |
| 43 | + // TODO map this.cellIndex to field name instead |
| 44 | + var field = header.text().toLowerCase().replace( /\s|[^a-z0-9]/g, "_" ); |
| 45 | + |
| 46 | + function extract( row ) { |
| 47 | + var text = row.options.data[ field ]; |
| 48 | + if ( type == "currency" ) { |
| 49 | + return $.global.parseFloat( text, culture ); |
| 50 | + } else if (type == "date") { |
| 51 | + return $.global.parseDate( text, format, culture ); |
| 52 | + } else { |
| 53 | + return text; |
| 54 | + } |
40 | 55 | }
|
| 56 | + |
| 57 | + // TODO should there be something more direct to access on grid? |
| 58 | + var items = $.ui.datastore.main.get( this.options.type ).options.items; |
| 59 | + |
| 60 | + // TODO sort actually modifies the input, so this works |
| 61 | + // a better implementation would seperate data extraction from sorting |
| 62 | + // that isn't possible here, as methods like .map() produce a new array |
| 63 | + items.sort(function( a, b ) { |
| 64 | + return ( extract( a ) < extract( b ) ? -1 : 1 ) * order; |
| 65 | + }); |
| 66 | + order *= -1; |
| 67 | + header.data( "sort-order", order ); |
| 68 | + this.refresh(); |
41 | 69 | }
|
42 |
| - items.sort(function(a, b) { |
43 |
| - // get property for field name and compare that |
44 |
| - return (extract(a) < extract(b) ? -1 : 1) * order; |
45 |
| - }); |
46 |
| - order *= -1; |
47 |
| - header.data("sort-order", order); |
48 |
| - grid.refresh(); |
49 |
| - }).data("grid"); |
| 70 | + }); |
| 71 | + |
| 72 | + $( "table" ).sortablegrid(); |
50 | 73 | });
|
51 | 74 | </script>
|
52 | 75 | </head>
|
|
107 | 130 | </tr>
|
108 | 131 | </tbody>
|
109 | 132 | </table>
|
| 133 | + |
| 134 | + <table id="developers-pe"> |
| 135 | + <thead> |
| 136 | + <tr> |
| 137 | + <th data-field="firstName">First Name</th> |
| 138 | + <th data-field="lastName">Last Name</th> |
| 139 | + <th data-field="country">Country</th> |
| 140 | + <th data-field="twitter">Twitter</th> |
| 141 | + <th data-field="github">GitHub</th> |
| 142 | + </tr> |
| 143 | + </thead> |
| 144 | + <tbody> |
| 145 | + <tr data-guid="john.resig"> |
| 146 | + <td>John</td> |
| 147 | + <td>Resig</td> |
| 148 | + <td>USA</td> |
| 149 | + <td>jeresig</td> |
| 150 | + <td>jeresig</td> |
| 151 | + </tr> |
| 152 | + <tr data-guid="scott.jehl"> |
| 153 | + <td>Scott</td> |
| 154 | + <td>Jehl</td> |
| 155 | + <td>USA</td> |
| 156 | + <td>filamentgroup</td> |
| 157 | + <td>scottjehl</td> |
| 158 | + </tr> |
| 159 | + </tbody> |
| 160 | + </table> |
110 | 161 |
|
111 | 162 | </body>
|
112 | 163 | </html>
|
0 commit comments