An Angular Service, responsible to build URLs from values of select and multi-select elements drives me crazy.
A multi-select
uses an array to store its selected values, even if there is only one element.
{
"select006": [
"m30x1.5",
"m18x1"
]
}
{
"select006": [
"m30x1.5"
]
}
A single select
uses simply an string to store its selected value.
{
"select006": "m30x1.5"
}
How do i deal with that issue? How do i check if the select-element is multiple or not and set the selected option to the specific value(s)?
Here is the relevant code:
/**
* Expand multiple values
*
* Convert param values back to an object with array
* of values.
*
* @example ?param=m30x1.5,m18x1 becomes {"select006": ["m30x1.5","m18x1"]}
*
* @param filters
* @returns {object}
* @private
*/
var _expandMultipleValues = function (filters) {
var param = {};
for (var filter in filters) {
if (filters.hasOwnProperty(filter)) {
if (filters[filter].indexOf(',') > -1) {
param[filter] = filters[filter].split(',');
} else {
// multiple needs to be an array to set selected option
// param[filter] = [filters[filter]];
// singe needs to be an string to set selected option
// param[filter] = filters[filter];
}
}
}
return param;
};
It seems i need to be more specific. The Problem here is that i need an array to set a selected option for multiple selects (even if only one is selected) but a string for a single select.
I've a variable in $scope wich holds the selected elements:
$scope.filter = {
"SA152": ["lorem", "ipsum"],
"SA044": ["30mm"],
"SA034": "m8x3"
}
If i try to set the selected option for SA044
with "SA044": '30mm'
the multiple select won't be selected as in angular a multiple selection needs to be an array.
This sets the selected option for a multi select:
$scope.filter = {
"SA044": ['30mm']
}
This does not work for a multi select:
$scope.filter = {
"SA044": "30mm"
}
Vice versa for single select elements.