From 07fe15952a40eae54d718eda645cc919bbf57b0e Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 2 Nov 2018 11:20:04 +0100 Subject: [PATCH] Add column selection --- index.html | 7 ++-- js/table.js | 99 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index a666fff..5c7a510 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,6 @@ - @@ -66,12 +65,14 @@ At solmen va esser necessi far uniform grammatica, pronunciation e plu sommun paroles. jony_ipsum

- + + +

diff --git a/js/table.js b/js/table.js index 8e283e8..88e79bd 100644 --- a/js/table.js +++ b/js/table.js @@ -4,24 +4,40 @@ // Source: https://www.w3schools.com/howto/howto_js_sort_table.asp // Source: https://stackoverflow.com/questions/19105009/how-to-insert-variables-in-javascript-strings -function handleFile() { +function handleFile(path) { // Check for the various File API support. if (window.FileReader) { // FileReader are supported. var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (xmlhttp.status == 200 && xmlhttp.readyState == 4) { - processData(xmlhttp.responseText); + processData(xmlhttp.responseText, path); } }; - xmlhttp.open("GET", "resources/world_data_v1.csv", true); + xmlhttp.open("GET", path, true); xmlhttp.send(); } else { alert('FileReader are not supported in this browser.'); } } -function processData(csv) { +function rerequestFile(path, columns) { + if (window.FileReader) { + // FileReader are supported. + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function () { + if (xmlhttp.status == 200 && xmlhttp.readyState == 4) { + updateTable(xmlhttp.responseText, columns); + } + }; + xmlhttp.open("GET", path, true); + xmlhttp.send(); + } else { + alert('FileReader are not supported in this browser.'); + } +} + +function processData(csv, path) { var table = ""; var checkbox = ""; @@ -31,10 +47,6 @@ function processData(csv) { // create table header table += ""; - // create checkbox content - checkbox += "Select Columns"; - checkbox += ""; + // checkbox += ""; table += ""; // create table content @@ -70,19 +82,68 @@ function processData(csv) { document.getElementById('t01').innerHTML = table; document.getElementById('list1').innerHTML = checkbox; +} - // create callbacks for checkboxes - var checkList = document.getElementById('list1'); - checkList.getElementsByClassName('anchor')[0].onclick = function (evt) { - if (!checkList.classList.contains('visible')) - checkList.classList.remove('visible'); - else - checkList.classList.add('visible'); +function toggleColumn(file) { + var iterator = document.getElementById('list1').childNodes; + var activated_columns = []; + + for (i = 0; i < iterator.length; i++) { + var checkbox = iterator[i].children[0]; + + if (checkbox.checked) { + activated_columns.push(checkbox.id); + } } - checkList.onblur = function (evt) { - checkList.classList.remove('visible'); + rerequestFile(file, activated_columns); +} + + +function updateTable(csv, columns) { + var table = ""; + + // split rows + var allTextLines = csv.split(/\r\n|\n/); + + // create table header + table += ""; + + var data = allTextLines[0].split(","); + var discarded_indices = []; + + for (var j = 0; j < data.length; j++) { + // create table column header + if (columns.includes(data[j])) { + table += ""; + table += data[j]; + table += ""; + } else { + discarded_indices.push(j); + } } + + // checkbox += ""; + table += ""; + + // create table content + for (var i = 1; i < allTextLines.length; i++) { + var data = allTextLines[i].split(','); + + table += ""; + + for (var j = 0; j < data.length; j++) { + if (!discarded_indices.includes(j)) { + table += ""; + table += data[j]; + table += ""; + } + } + + table += ""; + } + + document.getElementById('t01').innerHTML = table; } function sortTable(n) {