Skip to content

Commit

Permalink
Improved tablesearch. Added tablesort
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicklee committed May 30, 2019
1 parent b774ed9 commit 55ffe87
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -17,4 +17,10 @@ Refer to the `index.html` page for examples. To use this JS function, you simply

For example, if you want to search for last names, which are in column 2 (counting from 0), you can search for items in a specific column like this: `onkeyup="searchTable(this.value, 'list', 2)"`

If you want to search in two specific columns (e.g. first and last name) which are respectively in columns 1 and 2, then you can use more specific arguments: `onkeyup="searchTable(this.value, 'list', 1, 2)"`
If you want to search in two specific columns (e.g. first and last name) which are respectively in columns 1 and 2, then you can use more specific arguments: `onkeyup="searchTable(this.value, 'list', 1, 2)"`

Likewise, if you want to search in three specific columns (e.g. first, middle, and last name) which are respectively in columns 1, 2, and 3, then you can use this argument: `onkeyup="searchTable(this.value, 'list', 1, 2, 3)"`

## Sorting table rows numerically

An added feature is the `tablesort.js` which has been added. This will allow you to sort through a table on the column of index 0. Obviously, the column index can be changed as well as the table name as needed. Use an argument like: `sortTable("myTable", 0)`
18 changes: 15 additions & 3 deletions tablesearch.js
@@ -1,4 +1,4 @@
function searchTable(input, tableId, searchColumn = -1, searchColumn2 = -1)
function searchTable(input, tableId, searchColumn = -1, searchColumn2 = -1, searchColumn3 = -1)
{
var filter = input.toUpperCase();
var table = document.getElementById(tableId);
Expand All @@ -21,8 +21,20 @@ function searchTable(input, tableId, searchColumn = -1, searchColumn2 = -1)
else
{
if (i != 0) { //don't perform search on the first row header

if (searchColumn2 != -1) { //2 category search

if (searchColumn3 != -1) { //3 category search
rowText = row.cells[searchColumn].innerHTML; //get text for the 1st column
rowText2 = row.cells[searchColumn2].innerHTML; //get text for the 2nd column
rowText3 = row.cells[searchColumn3].innerHTML; //get text for the 3rd column
if (rowText.toUpperCase().indexOf(filter) > -1 || rowText2.toUpperCase().indexOf(filter) > -1 || rowText3.toUpperCase().indexOf(filter) > -1) { //if upper case matches any part of filter
row.style.display = "";
}
else {
row.style.display = "none";
}

}
else if (searchColumn2 != -1) { //2 category search
rowText = row.cells[searchColumn].innerHTML; //get text for the 1st column
rowText2 = row.cells[searchColumn2].innerHTML; //get text for the 2nd column
if (rowText.toUpperCase().indexOf(filter) > -1 || rowText2.toUpperCase().indexOf(filter) > -1) { //if upper case matches any part of filter
Expand Down
34 changes: 34 additions & 0 deletions tablesort.js
@@ -0,0 +1,34 @@
function sortTable(tableID, columnIndex = 0) {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById(tableID); //change to table name
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[columnIndex];
y = rows[i + 1].getElementsByTagName("TD")[columnIndex];
//check if the two rows should switch place:
if (Number(x.innerHTML) > Number(y.innerHTML)) { //< sign for large to small, > for small to large
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}

0 comments on commit 55ffe87

Please sign in to comment.