-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableview.filter.js
executable file
·118 lines (91 loc) · 3.55 KB
/
tableview.filter.js
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
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
//>>description: Extends the listview to add a search box to filter lists
//>>label: Listview: Filter
//>>group: Widgets
//define( [ "jquery", "./listview", "./forms/textinput" ], function( $ ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, undefined ) {
$.mobile.tableview.prototype.options.filter = false;
$.mobile.tableview.prototype.options.filterPlaceholder = "Filter items...";
$.mobile.tableview.prototype.options.filterSlot = 2;
var defaultFilterCallback = function( text, searchValue, item ) {
return text.toString().toLowerCase().indexOf( searchValue ) === -1;
};
$.mobile.tableview.prototype.options.filterCallback = defaultFilterCallback;
$( document ).delegate( ":jqmData(role='tableview')", "tableviewcreate", function() {
var table = $( this ),
tableview = table.data( "tableview" );
if ( !tableview.options.filter ) {
return;
}
var wrapper = $( "<form>", {
"class": "ui-tableview-filter ui-bar-" + tableview.options.wrapperTheme,
"role": "search"
}),
search = $( "<input>", {
placeholder: tableview.options.filterPlaceholder
})
.attr( "data-" + $.mobile.ns + "type", "search" )
.jqmData( "lastval", "" )
.bind( "keyup change", function() {
var $this = $( this ),
val = this.value.toLowerCase(),
listItems = null,
lastval = $this.jqmData( "lastval" ) + "",
childItems = false,
itemtext = "",
item, change, tableItems;
// Check if a custom filter callback applies
isCustomFilterCallback = tableview.options.filterCallback !== defaultFilterCallback,
tableview._trigger( "beforefilter", "beforefilter", { input: this } );
// Change val as lastval for next execution
$this.jqmData( "lastval" , val );
if ( isCustomFilterCallback || val.length < lastval.length || val.indexOf( lastval ) !== 0 ) {
// Custom filter callback applies or removed chars or pasted something totally different, check all items
tableItems = table.find("tbody tr");
} else {
// Only chars added, not removed, only use visible subset
tableItems = table.find("tbody tr:not(.ui-screen-hidden)" );
}
if ( val ) {
// This handles hiding regular rows without the text we search for
// and any list dividers without regular rows shown under it
for ( var i = tableItems.length - 1; i >= 0; i-- ) {
item = $( tableItems[ i ] );
itemtext = item.jqmData( "filtertext" ) || item.text();
if ( tableview.options.filterCallback( itemtext, val ) ) {
//mark to be hidden
item.toggleClass( "ui-filter-hidequeue" , true );
} else {
// There's a shown item in the bucket
childItems = true;
}
}
// Show items, not marked to be hidden
tableItems
.filter( ":not(.ui-filter-hidequeue)" )
.toggleClass( "ui-screen-hidden", false );
// Hide items, marked to be hidden
tableItems
.filter( ".ui-filter-hidequeue" )
.toggleClass( "ui-screen-hidden", true )
.toggleClass( "ui-filter-hidequeue", false );
} else {
//filtervalue is empty => show all
tableItems.toggleClass( "ui-screen-hidden", false );
}
})
.appendTo( wrapper )
.textinput();
if ( tableview.options.inset ) {
wrapper.addClass( "ui-tableview-filter-inset" );
}
wrapper.bind( "submit", function() {
return false;
})
$('.table-top-wrapper, .table-bottom-wrapper').children('div').eq(tableview.options.filterSlot-1).append(wrapper);
});
})( jQuery );
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
// });
//>>excludeEnd("jqmBuildExclude");