Added search functionality back in. It's much snappier now.

- Is now case-insensitive.
- And regex.
This commit is contained in:
David Roberts committed 2017-08-29 13:57:13 -07:00
1 parent bdf4225dec
commit 000b2fc147
2 files changed
+24 -4

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
<!doctype HTML>
<head>
<%- include("head.html") %>
<script src="storage.js"></script>
<script defer src="storage.js"></script>
</head>
<body>
+23 -3
View File
@@ -1,9 +1,20 @@
// function to draw data we recieve from ajax requests
_lastData = [];
function drawcontents(data) {
data = data || _lastData; //Cache data so we can drawcontents without waiting for the server, for the search box.
_lastData = data;
const search = new RegExp(document.querySelector("#search").value, 'i');
data = data.filter(function(item) {
return search.test(item.name);
})
sortByKey(data, "count");
const table = document.querySelector("#contents tbody"); //tables have tbody inserted automatically
const rows = table.children;
sortByKey(data, "count");
//update existing rows or create new ones
data.forEach(function(item, i) {
let row = rows[i];
if(!row) {
@@ -27,7 +38,12 @@ function drawcontents(data) {
if(count.textContent !== ''+item.count) {
count.textContent = item.count;
}
})
});
//remove excess rows, for example, after filtering
while (data.length < rows.length) {
table.removeChild(rows[data.length]);
}
}
// get cluster inventory from master
@@ -53,4 +69,8 @@ function sortByKey(array, key) {
array.sort(function(a, b) {
return b[key] - a[key];
});
}
}
document.querySelector("#search").addEventListener('input', function() {
drawcontents();
})