-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtable.js
78 lines (69 loc) · 2.04 KB
/
loadtable.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
loadtable = {};
loadtable.REFERRER = "https://bigdeal.sparcopen.org"
loadtable.KEYS = ['institution', 'date', 'country', 'publisher', 'considerations', 'outcome', 'savings'];
loadtable.KEYMAP = {}
loadtable.DATA = {}
loadtable.init = function() {
loadtable.getData({
callback: loadtable.renderData
});
$('#date').on('keyup', loadtable.dateFilter);
}
loadtable.getData = function(params) {
let container = $("#sheet");
let sid = container.attr("data-sheetid");
let tid = container.attr("data-tab");
let key = container.attr("data-key");
let url = `https://sheets.googleapis.com/v4/spreadsheets/${sid}/values/${tid}?key=${key}`;
$.ajax({
url: url,
headers: {"referer": loadtable.REFERRER},
dataType: "jsonp",
success: function (data) {
let keys = data.values.shift();
for (let k of loadtable.KEYS) {
let i = keys.indexOf(k);
loadtable.KEYMAP[k] = i;
}
loadtable.DATA = data.values;
params.callback();
}
});
}
loadtable.renderData = function() {
let tbl = `<table style="width:100%"><thead>
<th><b>Institution/<br />Consortium</b></th>
<th><b>Date</b></th>
<th><b>Country</b></th>
<th><b>Publisher(s)</b></th>
<th><b>Strategic Considerations</b></th>
<th><b>Outcome</b></th>
<th><b>Estimated Annual Savings (USD)</b></th>
</thead>
<tbody>`;
for ( let r of loadtable.DATA ) {
tbl += '<tr>';
for ( let k of loadtable.KEYS) {
let dk = r[loadtable.KEYMAP[k]];
if (dk === undefined) { dk === ''; }
if (k === 'institution' && r.source) {
dk = `<a href="${r.source}">${dk}</a>`
}
tbl += `<td>${dk}</td>`;
}
tbl += '</tr>';
}
tbl += '</tbody></table>';
$("#sheet").html(tbl);
}
loadtable.dateFilter = function() {
$('#sheet tbody').children('tr').each(function() {
let date = $("#date").val().trim();
if (!date.length || date.length !== 4 || $(this).html().indexOf('>' + date + '<') !== -1) {
$(this).show();
} else {
$(this).hide();
}
});
}
loadtable.init();