Skip to content

Commit eb0af8e

Browse files
committed
Grid SPF: Replace sort menu with header clicks, along with icons
1 parent 59cfa18 commit eb0af8e

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

grid-spf/datasource.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $.widget( "ui.datasource", {
1515
return this.data;
1616
},
1717

18+
// TODO this needs to be applied to init options as well, to work sort: "prop" work
1819
_setOption: function( key, value ) {
1920
// reset offset to 0 when changing limit
2021
// TODO actually only necessary when offset > offset + new limit

grid-spf/grid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $.widget( "ui.grid", {
2121
this._columns();
2222
this._rowTemplate();
2323
this.element.addClass( "ui-widget" );
24-
this.element.find( "th" ).addClass( "ui-widget-header" );
24+
this.element.find( "th" ).addClass( "ui-state-default" );
2525
this.element.delegate( "tbody > tr", "click", function( event ) {
2626
that._trigger( "select", event, {
2727
item: $( this ).data( "grid-item" )

grid-spf/menugrid.html

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,18 @@
6868
});
6969
</script>
7070
<style>
71-
.ui-menu {
72-
position: absolute;
71+
thead th {
72+
cursor: default;
73+
}
74+
thead th .ui-icon {
75+
display: none;
76+
height: 12px;
77+
}
78+
thead th.sorted .ui-icon-asc {
79+
display: inline-block;
80+
}
81+
thead th.sorted-desc .ui-icon-desc {
82+
display: inline-block;
7383
}
7484
tfoot td {
7585
text-align: center;
@@ -81,17 +91,6 @@
8191
</head>
8292
<body>
8393

84-
<script type="text/x-jquery-tmpl" id="menu-tmpl">
85-
<ul>
86-
<li>
87-
<a href="#" data-sort="asc">Sort Ascending</a>
88-
</li>
89-
<li>
90-
<a href="#" data-sort="desc">Sort Descending</a>
91-
</li>
92-
</ul>
93-
</script>
94-
9594
<h2>Movies! Click header for sort menu, use inputs for filtering, pager for paging</h2>
9695
<table id="movies">
9796
<thead>

grid-spf/menugrid.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
$.widget("spf.menugrid", {
22
_create: function() {
3-
var source = this.element.data("grid").options.source;
4-
this.element.find( "th" ).each(function() {
5-
var header = $( this ),
6-
field = header.data( "field" );
7-
var menu = $( "#menu-tmpl" ).tmpl().appendTo( this ).menu({
8-
select: function( event, ui ) {
9-
source
10-
.option( "sort",
11-
ui.item.find( "a" ).data( "sort" ) === "asc" ? field : "-" + field )
12-
.refresh();
13-
menu.hide();
14-
}
15-
}).popup({
16-
trigger: header
17-
});
18-
});
3+
var grid = this.element.data( "grid" );
4+
var source = grid.options.source;
5+
var headers = this.element.find( "th" );
6+
this._hoverable( headers );
7+
headers.click( function() {
8+
headers.not( this ).removeClass( "sorted sorted-desc" );
9+
var column = grid.options.columns[ this.cellIndex ];
10+
var sorted = $(this).hasClass("sorted");
11+
$( this ).toggleClass("sorted", !sorted).toggleClass("sorted-desc", sorted);
12+
source
13+
.option( "sort", ( sorted ? "-" : "" ) + column.property )
14+
.refresh();
15+
}).append( '<span class="ui-icon-asc ui-icon ui-icon-carat-1-n"></span><span class="ui-icon-desc ui-icon ui-icon-carat-1-s"></span>' );
1916

2017
var thead = this.element.find( "thead" );
2118
var inputs = thead.children()

0 commit comments

Comments
 (0)