forked from mleibman/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexample6-ajax-loading.html
172 lines (145 loc) · 5.05 KB
/
example6-ajax-loading.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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SlickGrid example 6: AJAX Load</title>
<link rel="stylesheet" href="../slick.grid.css" type="text/css"/>
<link rel="stylesheet" href="../css/smoothness/jquery-ui-1.8.24.custom.css" type="text/css"/>
<link rel="stylesheet" href="examples.css" type="text/css"/>
<style>
.cell-story {
white-space: normal !important;
line-height: 19px !important;
}
.loading-indicator {
display: inline-block;
padding: 12px;
background: white;
-opacity: 0.5;
color: black;
font-weight: bold;
z-index: 9999;
border: 1px solid red;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-moz-box-shadow: 0 0 5px red;
-webkit-box-shadow: 0px 0px 5px red;
-text-shadow: 1px 1px 1px white;
}
.loading-indicator label {
padding-left: 20px;
background: url('../images/ajax-loader-small.gif') no-repeat center left;
}
</style>
</head>
<body>
<div style="width:700px;float:left;">
<div class="grid-header" style="width:100%">
<label>Hackernews stories</label>
<span style="float:right;display:inline-block;">
Search:
<input type="text" id="txtSearch" value="github">
</span>
</div>
<div id="myGrid" style="width:100%;height:600px;"></div>
<div id="pager" style="width:100%;height:20px;"></div>
</div>
<div style="margin-left:750px;margin-top:40px;;">
<h2>Demonstrates:</h2>
<ul>
<li>loading data through AJAX</li>
<li>custom row height</li>
</ul>
<h2>WARNING:</h2>
<ul>
<li>API access to Hackernews provided through ThriftDB has some latency when paging through results. Be patient.
</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/ddomingues/X-SlickGrid/blob/gh-pages/liveDemo/examples/example6-ajax-loading.html"
target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
<script src="../lib/firebugx.js"></script>
<script src="../lib/jquery-1.7.min.js"></script>
<script src="../lib/jquery-ui-1.8.24.custom.min.js"></script>
<script src="../lib/jquery.event.drag-2.2.js"></script>
<script src="../lib/jquery.jsonp-2.4.min.js"></script>
<script src="../slick.core.js"></script>
<script src="../slick.remotemodel.js"></script>
<script src="../slick.grid.js"></script>
<script>
var grid;
var loader = new Slick.Data.RemoteModel();
var storyTitleFormatter = function (row, cell, value, columnDef, dataContext) {
s = "<b><a href='" + dataContext["url"] + "' target=_blank>" +
dataContext["title"] + "</a></b><br/>";
desc = dataContext["text"];
if (desc) { // on Hackernews many stories don't have a description
s += desc;
}
return s;
};
var dateFormatter = function (row, cell, value, columnDef, dataContext) {
return (value.getMonth() + 1) + "/" + value.getDate() + "/" + value.getFullYear();
};
var columns = [
{id: "num", name: "#", field: "index", width: 40},
{id: "story", name: "Story", width: 520, formatter: storyTitleFormatter, cssClass: "cell-story"},
{id: "date", name: "Date", field: "create_ts", width: 60, formatter: dateFormatter, sortable: true},
{id: "points", name: "Points", field: "points", width: 60, sortable: true}
];
var options = {
rowHeight: 64,
editable: false,
enableAddRow: false,
enableCellNavigation: false
};
var loadingIndicator = null;
$(function () {
grid = new Slick.Grid("#myGrid", loader.data, columns, options);
grid.onViewportChanged.subscribe(function (e, args) {
var vp = grid.getViewport();
loader.ensureData(vp.top, vp.bottom);
});
grid.onSort.subscribe(function (e, args) {
loader.setSort(args.sortCol.field, args.sortAsc ? 1 : -1);
var vp = grid.getViewport();
loader.ensureData(vp.top, vp.bottom);
});
loader.onDataLoading.subscribe(function () {
if (!loadingIndicator) {
loadingIndicator = $("<span class='loading-indicator'><label>Buffering...</label></span>").appendTo(document.body);
var $g = $("#myGrid");
loadingIndicator
.css("position", "absolute")
.css("top", $g.position().top + $g.height() / 2 - loadingIndicator.height() / 2)
.css("left", $g.position().left + $g.width() / 2 - loadingIndicator.width() / 2);
}
loadingIndicator.show();
});
loader.onDataLoaded.subscribe(function (e, args) {
for (var i = args.from; i <= args.to; i++) {
grid.invalidateRow(i);
}
grid.updateRowCount();
grid.render();
loadingIndicator.fadeOut();
});
$("#txtSearch").keyup(function (e) {
if (e.which == 13) {
loader.setSearch($(this).val());
var vp = grid.getViewport();
loader.ensureData(vp.top, vp.bottom);
}
});
loader.setSearch($("#txtSearch").val());
loader.setSort("create_ts", -1);
grid.setSortColumn("date", false);
// load the first page
grid.onViewportChanged.notify();
})
</script>
</body>
</html>