More actions
(move DropdownFilter code to Gadget-DropdownFilter.js) |
m (trimmed whitespace around terms before attempting to match filters) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
$(document).ready(function() { | $(document).ready(function() { | ||
createDropdownSelects(); | createDropdownSelects(); | ||
window.filter = filter; //export filter function to globals so jquery can find it without scope problems | |||
}); | }); | ||
Line 61: | Line 62: | ||
for (var i = 0; i < elementsToFilter.length; i++) { | for (var i = 0; i < elementsToFilter.length; i++) { | ||
var data = elementsToFilter[i].getAttribute(attribute); | var data = elementsToFilter[i].getAttribute(attribute); | ||
var words = data.split(' '); | var words = data.split(';'); | ||
var showRow = false; | var showRow = false; | ||
for (var w in words) { | for (var w in words) { | ||
showRow = showRow || ( | var tw = $.trim(words[w]); | ||
// console.log(filterValue + " " + | showRow = showRow || (tw == filterValue); | ||
// console.log(filterValue + " " + tw + " " + showRow); | |||
} | } | ||
Line 74: | Line 76: | ||
} | } | ||
// This method handlecs the CSS changes if a | // This method handlecs the CSS changes if a specified element matches the filter | ||
function applyFilterAction(element, applyFilter) { | function applyFilterAction(element, applyFilter) { | ||
if (element.nodeName == "DIV") { | if (element.nodeName == "DIV" || element.nodeName == "TR" ) { | ||
if (!applyFilter) { | if (!applyFilter) { | ||
element.style.display = "none"; | element.style.display = "none"; | ||
} else { | } else { | ||
element.style.display = " | element.style.display = ""; /* reset to default display */ | ||
} | } | ||
} | } |
Latest revision as of 00:32, 24 March 2021
/* For filtering items with Template:DropdownFilter */
var elementsToFilter = {};
$(document).ready(function() {
createDropdownSelects();
window.filter = filter; //export filter function to globals so jquery can find it without scope problems
});
function createDropdownSelects() {
var selectionParameters = document.getElementById("selectionOptions");
var selectRowElement = document.getElementById("selectRow");
if (!selectRowElement || !selectionParameters) {
return;
}
selectRowElement.setAttribute("style", "padding: 6px;");
var dropdownSelectionOptions = selectionParameters.innerHTML;
createDropdownSelect(dropdownSelectionOptions);
}
function createDropdownSelect(optionList) {
var i = 0;
var dropdown = JSON.parse(optionList);
/* read options from the JSON */
var dropdownDiv = document.getElementById(dropdown.divID);
elementsToFilter = document.getElementsByClassName(dropdown.applyToElementsWithThisClass);
/* create the options to populate the dropdown with */
var html = "<select id=\"" + dropdown.name + "\" name=\"" + dropdown.name + "\" class=\"form-control\" onchange=\"filter('" + dropdown.name + "','" + dropdown.attribute + "'" + ")\">";
for (i; i < dropdown.options.length; i++) {
html += "<option ";
if (i === 0) {
html += "selected=\"selected\"";
}
if (dropdown.options[i][1] !== "") {
html += "disabled=\"" + dropdown.options[i][1] + "\"";
}
html += "value=\"" + dropdown.options[i][0] + "\">"
+ dropdown.options[i][0] + "</option>";
}
/* populate the dropdown */
dropdownDiv.innerHTML += html;
}
/*
* Called from the onChange() event of a select element.
* Checks each row or element to see if its attribute contains at least one match with the selected value of the dropdown
* Filters only one dropdown
*/
function filter(filterDiv, attribute) {
resetFiltersApplied();
var filterValue = document.getElementById(filterDiv).value;
if (!filterValue) /* selected option is undefined, so do nothing (rather than hiding everything) */ return;
// Go through all of the items to filter through
for (var i = 0; i < elementsToFilter.length; i++) {
var data = elementsToFilter[i].getAttribute(attribute);
var words = data.split(';');
var showRow = false;
for (var w in words) {
var tw = $.trim(words[w]);
showRow = showRow || (tw == filterValue);
// console.log(filterValue + " " + tw + " " + showRow);
}
// Apply the filter
applyFilterAction(elementsToFilter[i], showRow);
}
}
// This method handlecs the CSS changes if a specified element matches the filter
function applyFilterAction(element, applyFilter) {
if (element.nodeName == "DIV" || element.nodeName == "TR" ) {
if (!applyFilter) {
element.style.display = "none";
} else {
element.style.display = ""; /* reset to default display */
}
}
}
// Reset any filters applied to elements.
function resetFiltersApplied() {
for (var i = 0; i < elementsToFilter.length; i++) {
if (elementsToFilter[i].nodeName == "DIV") {
elementsToFilter[i].style.display = "inline-block";
}
}
}