This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
175 lines (168 loc) · 6.85 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Backgrid Select Filter</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<script src="3rd/jquery.min.js" type="text/javascript"></script>
<script src="3rd/bootstrap.min.js" type="text/javascript"></script>
<link href="3rd/bootstrap.min.css" rel="stylesheet" type="text/css" charset="utf-8">
<script src="3rd/underscore.js" type="text/javascript"></script>
<script src="3rd/backbone.js" type="text/javascript"></script>
<script src="3rd/backbone-pageable.js" type="text/javascript"></script>
<script src="3rd/backgrid.js" type="text/javascript"></script>
<script src="3rd/backgrid-paginator.js" type="text/javascript"></script>
<link href="3rd/backgrid-paginator.min.css" rel="stylesheet" type="text/css" charset="utf-8">
<script src="backgrid-select-filter.js" type="text/javascript"></script>
<script src="territories.js" type="text/javascript"></script>
<script src="example.js" type="text/javascript"></script>
<style>
header.navbar-inverse {border-radius: 0;}
footer.navbar-default {border-radius: 0; margin-bottom: 0;}
</style>
</head>
<body>
<header class="navbar navbar-inverse">
<div class="navbar-header">
<a class="navbar-brand" href="#">Backgrid Select Filter</a>
</div>
</header>
<div class="container">
<div class="row">
<div class="col-md-12">
<p>
Client side drop-down filter for <a href="https://github.com/wyuenho/backgrid">Backgrid</a>. Inspired from <a href="https://github.com/wyuenho/backgrid-filter">backgrid-filter</a>.
This Backgrid extension allows you to filter using a SELECT element. Works with pagination when passed a <a href="https://github.com/wyuenho/backbone-pageable/">Backbone.PageableCollection</a>. It is designed to work on the client-side but could be extended to work server-side as well.
</p>
<p>
This adds a new class <code>Backgrid.Extension.SelectFilter</code> which extends a <code>Backbone.View</code>. As such, you can pass its constructor default options like <code>className</code>.
</p>
<h3>Options</h3>
<ul>
<li><strong>collection:</strong> An instance of a Backbone.Collection or a Backbone.PageableCollection.</li>
<li><strong>field:</strong> The Backbone model attribute to filter on.</li>
<li><strong>selectOptions:</strong> An array of option objects to create OPTIONs in the SELECT. Each option object must contain <em>value</em> and <em>label</em>.</li>
<li><strong>clearValue:</strong> Optional. Value to clear the filter. Defaults to <code>null</code>.</li>
<li><strong>initialValue:</strong> Optional. Value first selected. Defaults to <strong>clearValue</strong>.</li>
<li>
<strong>makeMatcher:</strong> Optional.
Function which returns a function used in Backbone.Model.filter().
Takes as argument the <code>value</code> set in the selected OPTIONS element.
Defaults to a function which does an <code>==</code> comparison.
Overwrite this to provider your own matching function.
The matching function you return will be passed the <code>model</code> as argument, and <code>this</code> will be set to the SelectFilter instance.
</li>
</ul>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h3>Example</h3>
<p>Data for this example is bootstrap-loaded in <code>territories.js</code> and set in globals <code>_territories</code> and <code>_continents</code>.</p>
</div>
</div>
<br/>
<div class="row">
<div id="result" class="col-md-6">
<div class="form-group form-inline">
<label>Filter by Continent:</label>
<select id="filter" class="form-control"></select>
</div>
</div>
<div class="col-md-6">
<h4>Code</h4>
<pre>
// Collection
var Territories = Backbone.PageableCollection.extend({
state: {pageSize: 15},
mode: "client"
});
var territories = new Territories(_territories);
// Grid
var columns = [{name: "id", label: "ID", cell: "integer"},
{name: "name", label: "Name", cell: "string"},
{name: "code", label: "Code", cell: "string"},
{name: "continent_code", label: "Continent", cell: "string"}],
grid = new Backgrid.Grid({
columns: columns,
collection: territories,
className: "backgrid table"
}),
$grid = grid.render().$el;
$("#result").append($grid);
// Paginator
var paginator = new Backgrid.Extension.Paginator({
collection: territories
}),
$paginator = paginator.render().$el;
$("#result").append($paginator);
// Select filter
var filter = new Backgrid.Extension.SelectFilter({
className: "backgrid-filter form-control",
collection: territories,
field: "continent_code",
selectOptions: _.union([{label: "All", value: null}],
_.map(_continents, function(o) {return {label: o.name, value: o.code};}))
}),
$filter = filter.render().$el;
$("#filter").replaceWith($filter);
</pre>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h3>Custom Matcher</h3>
<p>
For more complex filtering, where your SELECT values describe a custom filter to apply, you can override the <code>makeMatcher</code> function.
For example, say you want a filter with Americas, Europe, Asia, and Others.
</p>
</div>
</div>
<div class="row">
<div id="result-2" class="col-md-6">
<div class="form-group form-inline">
<label>Filter:</label>
<select id="filter-2" class="form-control"></select>
</div>
</div>
<div class="col-md-6">
<h4>Code</h4>
<pre>
// Select filter
var filter2 = new Backgrid.Extension.SelectFilter({
collection: territories,
field: "continent_code",
selectOptions: [{
label: "All", value: null
}, {
label: "Americas", value: ["NA", "SA"]
}, {
label: "Europe", value: ["EU"]
}, {
label: "Asia", value: ["AS"]
}, {
label: "Other", value: ["AF", "OC", "AN"]
}],
makeMatcher: function(value) {
return function(model) {
return _.indexOf(this.selectOptions[value],
model.get(this.field)) >= 0;
}
}
}),
$filter2 = filter2.render().$el;
$("#filter-2").replaceWith($filter2);
</pre>
</div>
</div>
</div>
<br/>
<footer class="navbar navbar-default">
<p class="navbar-text navbar-left">© 2014 <a href="http://www.amilia.com/en">Amilia Inc.</a> <a href="https://github.com/AmiliaApp/backgrid-select-filter/blob/gh-pages/LICENSE">Licensed under MIT.</a></p>
<p class="navbar-text navbar-right">Written by <a href="http://martindrapeau.tumblr.com/">Martin Drapeau</a></p>
<p class="navbar-text navbar-right"> </p>
</footer>
<a href="https://github.com/AmiliaApp/backgrid-select-filter"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
</body>
</html>