Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
iCity
EnergyDashboard
Commits
939afbc5
Commit
939afbc5
authored
Jul 17, 2021
by
Pithon Kabiro
Browse files
Add working linked drop down lists
parent
45920289
Changes
1
Hide whitespace changes
Inline
Side-by-side
public/js/dropDownList.js
View file @
939afbc5
"
use strict
"
;
"
use strict
"
;
const
dropDownBuilding
=
document
.
querySelector
(
"
#drop-down--bldg
"
);
const
buildingsAvailableSensorsArr
=
[
const
dropDownSensorParent
=
document
.
querySelector
(
[
"
--Select--
"
,
""
,
""
],
"
#drop-down--sensor-parent
"
);
[
"
Bau 101
"
,
"
Vorlauftemperatur
"
,
"
15 min
"
],
const
dropDownSensor
=
document
.
querySelector
(
"
#drop-down--sensor
"
);
[
"
Bau 101
"
,
"
Vorlauftemperatur
"
,
"
60 min
"
],
[
"
Bau 101
"
,
"
Rücklauftemperatur
"
,
"
15 min
"
],
const
showSensorDropDownListFromBuildingList
=
function
()
{
[
"
Bau 101
"
,
"
Rücklauftemperatur
"
,
"
60 min
"
],
if
(
dropDownSensorParent
.
classList
.
contains
(
"
hidden
"
))
{
dropDownSensorParent
.
classList
.
toggle
(
"
hidden
"
);
[
"
Bau 102
"
,
"
Vorlauftemperatur
"
,
"
15 min
"
],
}
[
"
Bau 102
"
,
"
Vorlauftemperatur
"
,
"
60 min
"
],
[
"
Bau 102
"
,
"
Rücklauftemperatur
"
,
"
15 min
"
],
[
"
Bau 102
"
,
"
Rücklauftemperatur
"
,
"
60 min
"
],
[
"
Bau 107
"
,
"
Vorlauftemperatur
"
,
"
15 min
"
],
[
"
Bau 107
"
,
"
Vorlauftemperatur
"
,
"
60 min
"
],
[
"
Bau 107
"
,
"
Rücklauftemperatur
"
,
"
15 min
"
],
[
"
Bau 107
"
,
"
Rücklauftemperatur
"
,
"
60 min
"
],
[
"
Bau 112
"
,
"
Vorlauftemperatur
"
,
"
15 min
"
],
[
"
Bau 112
"
,
"
Vorlauftemperatur
"
,
"
60 min
"
],
[
"
Bau 112
"
,
"
Rücklauftemperatur
"
,
"
15 min
"
],
[
"
Bau 112
"
,
"
Rücklauftemperatur
"
,
"
60 min
"
],
[
"
Bau 125
"
,
"
Vorlauftemperatur
"
,
"
15 min
"
],
[
"
Bau 125
"
,
"
Vorlauftemperatur
"
,
"
60 min
"
],
[
"
Bau 125
"
,
"
Rücklauftemperatur
"
,
"
15 min
"
],
[
"
Bau 125
"
,
"
Rücklauftemperatur
"
,
"
60 min
"
],
[
"
Bau 225
"
,
"
Vorlauftemperatur
"
,
"
15 min
"
],
[
"
Bau 225
"
,
"
Vorlauftemperatur
"
,
"
60 min
"
],
[
"
Bau 225
"
,
"
Rücklauftemperatur
"
,
"
15 min
"
],
[
"
Bau 225
"
,
"
Rücklauftemperatur
"
,
"
60 min
"
],
[
"
Bau 225
"
,
"
Durchfluss
"
,
"
15 min
"
],
[
"
Bau 225
"
,
"
Durchfluss
"
,
"
60 min
"
],
[
"
Bau 225
"
,
"
Leistung
"
,
"
15 min
"
],
[
"
Bau 225
"
,
"
Leistung
"
,
"
60 min
"
],
[
"
Bau 225
"
,
"
Energie
"
,
"
15 min
"
],
[
"
Bau 225
"
,
"
Energie
"
,
"
60 min
"
],
[
"
Bau 225
"
,
"
Energie_VERBR
"
,
"
15 min
"
],
[
"
Bau 225
"
,
"
Energie_VERBR
"
,
"
60 min
"
],
];
/**
* Get the unique values from an array
* @param {Array} dataArr Input array
* @param {Number} index Integer representing a zero-based index of the columns in the array
* @returns {Array} An array of unique options
*/
const
getUniqueValues
=
function
(
dataArr
,
index
)
{
const
uniqueOptions
=
new
Set
();
dataArr
.
forEach
((
row
)
=>
uniqueOptions
.
add
(
row
[
index
]));
return
[...
uniqueOptions
];
};
};
const
hideSensorDropDownListFromBuildingList
=
function
()
{
/**
if
(
!
dropDownSensorParent
.
classList
.
contains
(
"
hidden
"
))
{
* Populate the HTML elements that make up a drop down list
dropDownSensorParent
.
classList
.
toggle
(
"
hidden
"
);
* @param {String} element String corresponding to the ID of a drop down HTML element
}
* @param {Array} uniqueValuesArr An array of unique values
* @returns {undefined}
*/
const
populateDropDown
=
function
(
element
,
uniqueValuesArr
)
{
element
.
innerHTML
=
""
;
uniqueValuesArr
.
forEach
((
item
)
=>
{
const
option
=
document
.
createElement
(
"
option
"
);
option
.
textContent
=
item
;
element
.
appendChild
(
option
);
});
};
};
// SET THE INITIAL STATE OF THE DROPDOWN
/**
hideSensorDropDownListFromBuildingList
();
* Filter an array using filter strings of variable length
* @param {*} dataArr Input array
const
selectBuildingFromDropDownList
=
function
()
{
* @param {*} filtersAsArray An array of filter strings
const
selectedBuilding
=
* @returns {Array} An array that contains the filter result
dropDownBuilding
.
options
[
dropDownBuilding
.
selectedIndex
].
value
;
*/
const
filterArray
=
function
(
dataArr
,
filtersAsArray
)
{
switch
(
selectedBuilding
)
{
return
dataArr
.
filter
((
row
)
=>
case
"
none
"
:
filtersAsArray
.
every
((
filterItem
,
i
)
=>
filterItem
===
row
[
i
])
hideSensorDropDownListFromBuildingList
();
);
break
;
case
"
101
"
:
console
.
log
(
"
Building 101
"
);
showSensorDropDownListFromBuildingList
();
break
;
case
"
102
"
:
console
.
log
(
"
Building 102
"
);
showSensorDropDownListFromBuildingList
();
break
;
case
"
107
"
:
console
.
log
(
"
Building 107
"
);
showSensorDropDownListFromBuildingList
();
break
;
case
"
112
"
:
console
.
log
(
"
Building 112
"
);
showSensorDropDownListFromBuildingList
();
break
;
case
"
125
"
:
console
.
log
(
"
Building 125
"
);
showSensorDropDownListFromBuildingList
();
break
;
case
"
225
"
:
console
.
log
(
"
Building 225
"
);
showSensorDropDownListFromBuildingList
();
break
;
default
:
console
.
log
(
"
Default case
"
);
}
};
};
const
selectSensorFromDropDownList
=
function
()
{
/**
const
selectedSensor
=
* Create a drop down list in the HTML document
dropDownSensor
.
options
[
dropDownSensor
.
selectedIndex
].
value
;
* @param {*} dataArr Input array
* @param {*} filtersAsArray An array of strings tp be used as filters
switch
(
selectedSensor
)
{
* @param {*} targetElement String corresponding to the ID of a drop down HTML element
case
"
none
"
:
*/
break
;
const
makeDropDown
=
function
(
dataArr
,
filtersAsArray
,
targetElement
)
{
case
"
vorlauf
"
:
const
filteredArr
=
filterArray
(
dataArr
,
filtersAsArray
);
console
.
log
(
"
VORLAUF
"
);
break
;
const
uniqueList
=
getUniqueValues
(
filteredArr
,
filtersAsArray
.
length
);
case
"
ruecklauf
"
:
console
.
log
(
"
RUECKLAUF
"
);
getUniqueValues
(
dataArr
);
break
;
case
"
durchfluss
"
:
populateDropDown
(
targetElement
,
uniqueList
);
console
.
log
(
"
DURCHFLUSS
"
);
};
break
;
case
"
leistung
"
:
/**
console
.
log
(
"
LEISTUNG
"
);
* Use the `makeDropDown` function to create the first two levels of the linked drop down lists
break
;
*/
case
"
energie
"
:
const
applyDropDown
=
function
()
{
console
.
log
(
"
ENERGIE
"
);
const
selectLevel1Value
=
document
.
querySelector
(
"
#drop-down--bldg
"
).
value
;
break
;
const
selectLevel2
=
document
.
querySelector
(
"
#drop-down--sensor
"
);
case
"
energieVerbr
"
:
makeDropDown
(
buildingsAvailableSensorsArr
,
[
selectLevel1Value
],
selectLevel2
);
console
.
log
(
"
ENERGIE_VERBR
"
);
break
;
applyDropDown2
();
default
:
console
.
log
(
"
Default case
"
);
}
};
};
dropDownBuilding
.
addEventListener
(
"
change
"
,
selectBuildingFromDropDownList
);
/**
dropDownSensor
.
addEventListener
(
"
change
"
,
selectSensorFromDropDownList
);
* Use the `makeDropDown` function to create the third level of the linked drop down lists
*/
const
applyDropDown2
=
function
()
{
const
selectLevel1Value
=
document
.
querySelector
(
"
#drop-down--bldg
"
).
value
;
const
selectLevel2Value
=
document
.
querySelector
(
"
#drop-down--sensor
"
).
value
;
const
selectLevel3
=
document
.
querySelector
(
"
#drop-down--sampling-rate
"
);
makeDropDown
(
buildingsAvailableSensorsArr
,
[
selectLevel1Value
,
selectLevel2Value
],
selectLevel3
);
};
/**
* Use the `populateDropDown` function to populate the first level drop down
*/
const
populateFirstLevelDropDown
=
function
()
{
const
el
=
document
.
querySelector
(
"
#drop-down--bldg
"
);
const
uniqueList
=
getUniqueValues
(
buildingsAvailableSensorsArr
,
0
);
populateDropDown
(
el
,
uniqueList
);
};
document
.
querySelector
(
"
#drop-down--bldg
"
)
.
addEventListener
(
"
change
"
,
applyDropDown
);
document
.
querySelector
(
"
#drop-down--sensor
"
)
.
addEventListener
(
"
change
"
,
applyDropDown2
);
// These functions run after "DOMContentLoaded" event
populateFirstLevelDropDown
();
applyDropDown
();
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment