Skip to content

Commit a6ba9fb

Browse files
committed
Rewrote data model sorting demo to define a new widget inherting from
grid. Added another table to ensure that the widget works with more than one. Added some more comments about open issues.
1 parent 5acfcf3 commit a6ba9fb

File tree

1 file changed

+77
-26
lines changed

1 file changed

+77
-26
lines changed

tests/visual/grid/datasort.html

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,57 @@
1919
<script src="grid.js"></script>
2020
<script type="text/javascript">
2121
$(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+
}
4055
}
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();
4169
}
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();
5073
});
5174
</script>
5275
</head>
@@ -107,6 +130,34 @@
107130
</tr>
108131
</tbody>
109132
</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>
110161

111162
</body>
112163
</html>

0 commit comments

Comments
 (0)