The following is inside a loop:
Code:
store1 = (Employees[i][3]==1)?'selected="true"':'';
store2 = (Employees[i][3]==2)?'selected="true"':'';
store3 = (Employees[i][3]==3)?'selected="true"':'';
store4 = (Employees[i][3]==4)?'selected="true"':'';
myTable+= '<td><select id="store' + i + '" onchange="" style="color:'+color1+' ; width:125px ; height:35px" >'
+'<option ' + store1 + ' value="1">Pasadena</option>'
+'<option ' + store2 + ' value="2">Alvin</option>'
+'<option ' + store3 + ' value="3">Angleton</option>'
+'<option ' + store4 + ' value="4">Bay City</option>'
+'</select></td>'
I originally came up with the idea in a similar part of the app that allowed choosing between employees and would ideally prefer something similar:
Code:
function BUILD_EMP_SELECTOR(){
empSelector = '<select id="empSelect" onchange="UPDATE_EMP();">';
for (i=1 ; i<AllEmployees.length ; i++){
Booleanselect = (i==me)?'selected="true"':'';
empSelector += '<option ' + Booleanselect + ' value="' + i + '">' + AllEmployees[i] + '</option>';
}
empSelector += '</select>';
ID('employee').innerHTML = empSelector;
}
The problem I'm having is that I would like to optimize the part of the first code that chooses which store a particular employee is at.
I've also come up with this, but I'm not sure if its the best solution, or if it's even any better than my first:
Code:
store1=store2=store3=store4='';sel='selected="true"';
Employees[i][3]==1&&(store1=sel);
Employees[i][3]==2&&(store2=sel);
Employees[i][3]==3&&(store3=sel);
Employees[i][3]==4&&(store4=sel);
Also, while I would have to hard code additional stores
+'<option ' + store5 + ' value="5">Elsewhere</option>' I would rather not have to add an additional "else if store5=" if possible.
I know this is mostly code, and not much question, but what I'm looking for is mostly your opinions/recommendations...