From 000b2fc147c43e92ebc0848bc42613a675996d30 Mon Sep 17 00:00:00 2001 From: David Roberts Date: Tue, 29 Aug 2017 13:57:13 -0700 Subject: [PATCH] Added search functionality back in. It's much snappier now. - Is now case-insensitive. - And regex. --- static/index.html | 2 +- static/storage.js | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/static/index.html b/static/index.html index 72c62f14..f5a5559e 100644 --- a/static/index.html +++ b/static/index.html @@ -1,7 +1,7 @@ <%- include("head.html") %> - + diff --git a/static/storage.js b/static/storage.js index 7506dc0d..ee15e77d 100644 --- a/static/storage.js +++ b/static/storage.js @@ -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]; }); -} \ No newline at end of file +} + +document.querySelector("#search").addEventListener('input', function() { + drawcontents(); +}) \ No newline at end of file