-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulateFilters.html
107 lines (89 loc) · 3.39 KB
/
populateFilters.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
</head>
<body>
<div id="jsGrid">
</div>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
<script>
function createTextFilterTemplate(propertyName, initialFilter) {
return function () {
if (!this.filtering)
return "";
var grid = this._grid,
$result = this.filterControl = this._createTextBox();
if (this.autosearch) {
$result.on("keypress",
function (e) {
if (e.which === 13) {
grid.search();
e.preventDefault();
}
});
}
var gridFilter = initialFilter;
if (gridFilter && gridFilter[propertyName]) {
$result.val(gridFilter[propertyName]);
}
return $result;
}
}
$(function() {
var initialFilter = { Name: 'Lemonade'};
$("#jsGrid").jsGrid({
width: "100%",
sorting: true,
paging: false,
filtering: true,
autoload: false,
controller: {
loadData: function(filter) {
var d = $.Deferred();
var url = 'http://services.odata.org/V3/(S(3mnweai3qldmghnzfshavfok))/OData/OData.svc/Products';
//this logic is specific to oData service and could be different for your implementation
//for example the filter could be passed as data object to server
if(filter){
if(filter.Name){
url += "?$filter=startswith(Name,'"+filter.Name+"') eq true";
}
}
//get data
$.ajax({
url: url,
dataType: "json"
}).done(function(response) {
//show data in grid
d.resolve(response.value);
});
return d.promise();
}
},
fields: [
{ name: "Name", type: "text", filterTemplate: createTextFilterTemplate("Name", initialFilter) },
{ name: "Description", type: "textarea", width: 150 },
{ name: "Rating", type: "number"}, //hide column
{ name: "Price", type: "number", width: 50,
itemTemplate: function(value) {
return value.toFixed(2) + "$"; }
}
]
});
var grid = $("#jsGrid").data("JSGrid");
if (initialFilter != null) {
grid.search(initialFilter);
} else {
grid.search();
}
});
</script>
</body>
</html>