Skip to content

Commit 9631af8

Browse files
Sorting in tabular view
1 parent d1bf982 commit 9631af8

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iknow-entity-browser",
3-
"version": "0.5.10",
3+
"version": "0.5.11",
44
"description": "Visualizer for iKnow entities",
55
"main": "gulpfile.babel.js",
66
"scripts": {

src/static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
</label>
8787
</div>
8888
<div class="wrapper">
89-
<table>
89+
<table id="tabular">
9090
<thead>
9191
<tr>
9292
<th data-prop="id">ID</th>

src/static/js/tabular/index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import * as model from "../model";
33
import { onSelectionUpdate, updateSelection, getSelection } from "../selection";
44

55
let sorting = {
6-
enabled: false,
76
properties: ["entities", "0", "score"],
87
order: 1
98
};
109

1110
let sorter = (a, b) => {
1211
let i = 0;
1312
while (i < sorting.properties.length && typeof (a = a[sorting.properties[i]]) !== "undefined"
14-
&& typeof (b = b[sorting.properties[i]]) !== "undefined") { console.log(i); ++i }
15-
return a > b ? -sorting.order : a === b ? 0 : sorting.order;
13+
&& typeof (b = b[sorting.properties[i]]) !== "undefined") { ++i }
14+
return a > b ? sorting.order : a === b ? 0 : -sorting.order;
1615
};
1716

1817
function updateSelected () {
@@ -38,12 +37,42 @@ function updateOthers () {
3837

3938
}
4039

40+
function updateAll () {
41+
updateSelected();
42+
updateOthers();
43+
}
44+
4145
onSelectionUpdate(() => {
4246
if (!model.uiState.tabularToggled)
4347
return;
4448
updateSelected();
4549
});
4650

51+
/**
52+
* @this {HTMLElement} TH
53+
*/
54+
function columnClicked () {
55+
let attr = this.getAttribute("data-prop"),
56+
arr = attr.split(".");
57+
if (attr === sorting.properties.join("."))
58+
sorting.order = sorting.order === 1 ? -1 : sorting.order === -1 ? 0 : 1;
59+
else
60+
sorting.order = 1;
61+
sorting.properties = arr;
62+
updateAll();
63+
updateHeaders(attr);
64+
}
65+
66+
function updateHeaders (dataProp = undefined) {
67+
[].slice.call(document.querySelectorAll("#tabular thead th")).forEach((th) => {
68+
th.classList.remove("sort-up");
69+
th.classList.remove("sort-down");
70+
if (th.getAttribute("data-prop") !== dataProp)
71+
return;
72+
th.classList.toggle(`sort-${ sorting.order === 1 ? "up" : "down" }`, sorting.order !== 0);
73+
});
74+
}
75+
4776
export function init () {
4877

4978
d3.select("#tableToggle")
@@ -61,4 +90,11 @@ export function init () {
6190
));
6291
});
6392

93+
[].slice.call(document.querySelectorAll("#tabular thead th")).forEach((th) => {
94+
if (!th.getAttribute("data-prop")) return;
95+
th.addEventListener("click", columnClicked);
96+
});
97+
98+
updateHeaders(sorting.properties.join("."));
99+
64100
}

src/static/scss/tabular.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ $headerHeight: 36px;
4040

4141
}
4242

43+
}
44+
45+
#tabular th {
46+
47+
&.sort-up:after {
48+
content: "";
49+
}
50+
51+
&.sort-down:after {
52+
content: "";
53+
}
54+
4355
}

0 commit comments

Comments
 (0)