2018-05-28 20:46:25 +00:00
|
|
|
function setupFilters() {
|
2018-08-12 01:41:11 +00:00
|
|
|
var table = document.querySelector( 'table#companies-table' );
|
2018-05-28 20:46:25 +00:00
|
|
|
|
|
|
|
var headerCells = table.querySelectorAll( 'thead tr th' );
|
|
|
|
headerCells[ 0 ].innerHTML =
|
|
|
|
'<button class="sort" data-sort="company-name">Name</button>';
|
|
|
|
headerCells[ 1 ].innerHTML =
|
|
|
|
'<button class="sort" data-sort="company-website">Website</button>';
|
|
|
|
headerCells[ 2 ].innerHTML =
|
|
|
|
'<button class="sort" data-sort="company-region">Region</button>';
|
|
|
|
|
|
|
|
var tbody = table.querySelector( 'tbody' );
|
|
|
|
tbody.setAttribute( 'class', 'list' );
|
|
|
|
|
|
|
|
var filterInput = document.createElement( 'input' );
|
|
|
|
filterInput.type = 'text';
|
|
|
|
filterInput.placeholder = 'Filter Companies';
|
|
|
|
filterInput.id = 'company-filter';
|
|
|
|
filterInput.setAttribute( 'class', 'company-filter' );
|
|
|
|
|
2018-08-12 01:41:11 +00:00
|
|
|
var companiesHeading = document.querySelector( 'h2#companies' );
|
|
|
|
companiesHeading.appendChild( filterInput );
|
2018-05-28 20:46:25 +00:00
|
|
|
|
2018-08-12 01:41:11 +00:00
|
|
|
var filtersExplanation = document.createElement( 'p' );
|
|
|
|
filtersExplanation.id = 'filters-explanation';
|
|
|
|
filtersExplanation.innerHTML = (
|
|
|
|
'Use the text box above to filter the list of companies, '
|
|
|
|
+ 'or click a column heading to sort by that column.'
|
|
|
|
);
|
|
|
|
table.parentNode.insertBefore( filtersExplanation, table );
|
2018-05-28 20:46:25 +00:00
|
|
|
|
|
|
|
window.tableFilter = new List(
|
2018-08-12 01:41:11 +00:00
|
|
|
'main', // element ID that contains everything
|
2018-05-28 20:46:25 +00:00
|
|
|
{
|
|
|
|
valueNames: [
|
|
|
|
'company-name',
|
|
|
|
'company-website',
|
|
|
|
'company-region'
|
|
|
|
],
|
|
|
|
searchClass: 'company-filter',
|
|
|
|
}
|
|
|
|
);
|
2018-08-12 01:41:11 +00:00
|
|
|
|
2018-08-13 04:12:18 +00:00
|
|
|
document.body.setAttribute(
|
|
|
|
'class',
|
|
|
|
document.body.getAttribute( 'class' ) + ' filters-enabled'
|
|
|
|
);
|
2018-05-28 20:46:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 01:41:11 +00:00
|
|
|
document.addEventListener( 'DOMContentLoaded', function( event ) {
|
|
|
|
setupFilters();
|
|
|
|
} );
|