-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjsontables.js
More file actions
122 lines (112 loc) · 3.07 KB
/
jsontables.js
File metadata and controls
122 lines (112 loc) · 3.07 KB
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
function JSONTable(tableObject)
{
this.table = tableObject
this.toJSON = function()
{
tableHeaderArray = []
tableHeader = this.table.find("th")
for (var hc = 0; hc < tableHeader.length; hc++)
{
tableHeaderArray.push($(tableHeader[hc]).text())
}
var tableJsonObjectList = []
tableRows = this.table.find("tr").has("td")
for (var rc = 0; rc < tableRows.length; rc++)
{
tableJsonObject = {}
tableRow = $(tableRows[rc]).children()
for (var cc = 0; cc < tableRow.length; cc++)
{
tableJsonObject[tableHeaderArray[cc]] = tableRow[cc].innerText
}
tableJsonObjectList.push(tableJsonObject)
}
return tableJsonObjectList
}
this.tableJSON = this.toJSON()
this.tableFullJSON = this.toJSON()
this.isTableFiltered = false
this.clearTable = function()
{
this.table.find("tr").has("td").remove()
}
this.fromJSON = function(jsonSourceData = this.tableJSON, setFullJSON = true)
{
if (jsonSourceData.length == 0)
{
this.clearTable()
return
}
rootTableObject = this.table.clone().empty().append('<thead><tr></tr></thead>')
rootHeaderRow = rootTableObject.find('tr')
tableHeaderKeyArray = []
tableHeaderKeys = Object.keys(jsonSourceData[0])
for (var kc = 0; kc < tableHeaderKeys.length; kc++)
{
tableHeaderKeyArray.push(tableHeaderKeys[kc])
$(rootHeaderRow).append('<th>'+tableHeaderKeys[kc]+'</th>')
}
rootTableObject.append("<tbody></tbody>")
for (var jr = 0; jr < jsonSourceData.length; jr++)
{
tableDataRow = $('<tr></tr>')
for (var ki = 0; ki < tableHeaderKeyArray.length; ki++)
{
tableDataRow.append('<td>'+jsonSourceData[jr][tableHeaderKeyArray[ki]])
}
rootTableObject.find("tbody").append(tableDataRow)
}
this.table.html(rootTableObject[0].innerHTML)
this.tableJSON = jsonSourceData
if (setFullJSON)
{
this.tableFullJSON = jsonSourceData
}
}
this.limitJSON = function(page = 0, limit = 25, updateTableDirectly = false, inputJSON = this.tableJSON)
{
return inputJSON.slice(page*limit, (page*limit)+limit)
}
this.filter = function(searchQuery)
{
this.isTableFiltered = true
resultList = []
searchQuery = searchQuery.toLowerCase()
sourceTableJSON = this.tableFullJSON
sourceTableJSONLength = sourceTableJSON.length
sourceTableKeys = Object.keys(sourceTableJSON[0])
sourceTableKeysLength = sourceTableKeys.length
searchQuerySplit = searchQuery.split(" ")
searchQuerySplitLength = searchQuerySplit.length
for (fj = 0; fj < sourceTableJSONLength; fj++)
{
tempResultListLength = 0
for (ql = 0; ql < searchQuerySplitLength; ql++)
{
for (tk = 0; tk < sourceTableKeysLength; tk++)
{
if (sourceTableJSON[fj][sourceTableKeys[tk]].toLowerCase().indexOf(searchQuerySplit[ql]) != -1)
{
tempResultListLength++
break
}
}
}
if ((tempResultListLength == searchQuerySplitLength))
{
resultList.push(sourceTableJSON[fj])
}
}
if (!searchQuery)
{
this.isTableFiltered = false
this.tableJSON = this.tableFullJSON
resultList = this.tableFullJSON
this.fromJSON(resultList)
}
else
{
this.fromJSON(resultList, false)
}
}
}