2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-12-02 06:41:55 +00:00
troggle/media/scripts/TableSort.js

160 lines
3.9 KiB
JavaScript

function filterTable(tablename)
{
table = document.getElementById(tablename);
mindepth = document.getElementById("CaveDepthMin").value;
maxdepth = document.getElementById("CaveDepthMax").value;
if(mindepth==0)mindepth=-999999;
if(maxdepth==0)maxdepth= 999999;
minlength = document.getElementById("CaveLengthMin").value;
maxlength = document.getElementById("CaveLengthMax").value;
if(minlength==0)minlength=-999999;
if(maxlength==0)maxlength= 999999;
visitdate = document.getElementById("VisitDate").value;
visitor = document.getElementById("Visitor").value;
cavename = document.getElementById("CaveName").value.toLowerCase();
incomplete = document.getElementById("Incomplete").checked;
var regexmode = false;
if(visitor[0]=='/' && visitor[visitor.length-1]=='/')
{
regexmode = true;
visitor = new RegExp(visitor.substr(1,visitor.length-2));
}
else
{
visitor.toLowerCase();
}
rows = table.rows;
for(i=1; i< rows.length; i++)
{
name = (rows[i].getElementsByTagName("TD")[1]).innerHTML.toLowerCase();
depth = (rows[i].getElementsByTagName("TD")[2]).innerHTML.toLowerCase();
depth = Number(depth.replace(/[^0-9.]/g,''));
length = (rows[i].getElementsByTagName("TD")[3]).innerHTML.toLowerCase();
length = Number(length.replace(/[^0-9.]/g,''));
date = (rows[i].getElementsByTagName("TD")[4]).innerHTML.toLowerCase();
//recentvisitor = (rows[i].getElementsByTagName("TD")[4]).innerHTML.toLowerCase();
recentvisitor = ""
if(cavename != "" && !name.includes(cavename))
{
rows[i].style.visibility = "collapse";
}
if(depth<mindepth || depth>maxdepth)
{
rows[i].style.visibility = "collapse";
}
if(length<minlength || length>maxlength)
{
rows[i].style.visibility = "collapse";
}
if(date < visitdate)
{
rows[i].style.visibility = "collapse";
}
if(visitor != "" && regexmode && !visitor.test(recentvisitor))
{
rows[i].style.visibility = "collapse";
}
if(visitor != "" && !regexmode && !recentvisitor.includes(visitor))
{
rows[i].style.visibility = "collapse";
}
crow=rows[i].getElementsByTagName("TD");
for(var j=0; j<crow.length; j++)
{
if(crow[j].innerHTML == "" && incomplete)
{
rows[i].style.visibility = "collapse";
break;
}
}
}
}
function filterTableReset(tablename)
{
table = document.getElementById(tablename);
rows = table.rows;
for(i=1; i< rows.length; i++)
{
rows[i].style.visibility = "visible";
}
}
function isOrdered(kvarray,numeric)
{
for(var i=0;i<kvarray.length-1;i++)
{
if(numeric==1 && Number(kvarray[i][0])>Number(kvarray[i+1][0]))
{
return false;
}
if(numeric!=1 && kvarray[i][0]>kvarray[i+1][0])
{
return false;
}
}
return true;
}
function sortTable(n, tablename, numeric) {
table = document.getElementById(tablename);
rows = table.rows;
var ordering = [];
var i;
//construct key-value pairs for sorting
for(i = 1; i < rows.length; i++) //remember header rows
{
key = rows[i].getElementsByTagName("TD")[n];
key = key.innerHTML.toLowerCase();
if(numeric==1)
{
key=key.replace(/[^0-9.]/g,'')
}
ordering.push([key,i]);
}
var ascending = isOrdered(ordering,numeric);
//sort either numerically or alphabetically
if(numeric==1)
{
ordering.sort((x,y) => Number(x[0])-Number(y[0]));
}
else
{
ordering.sort(); //sorts alphabetically
}
if(ascending) ordering.reverse();
for(i = 0; i < ordering.length; i++) //add sorted list at the end of the table
{
var keyval = ordering[i];
id = keyval[1]; //get rownumber of n^th sorted value
cln = rows[id].cloneNode(true); //deep clone of current node
table.insertBefore(cln,null); //add n^th row at the end
}
for(i = 1; i < ordering.length+1; i++) //remove unsorted nodes
{
table.deleteRow(1);// 0 -> header; 1 -> first row
}
}