Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-23-2011, 06:24 PM   PM User | #1
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
if then esle optimization

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...
blaze4218 is offline   Reply With Quote
Old 09-23-2011, 08:32 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Like this?

Code:
var stores = [ null,"Pasadena","Alvin","Angleton","Bay City"];

myTable += '<td><select name="store' + i + '" class="select' + i + '">';
for ( var s = 1; s < stores.length; ++s )
{
    var sel = Employees[i][3] == s ? "selected" : "";
    myTable += '<option  ' + sel + ' value="' + s + '">' + stores[s] + '</option>';
}
myTable += '</select></td>'
Presumably, you will wrap that with a loop on variable i, to pick up all employees??

I took the liberty of changing your inline style to a class. But you can of course change it back.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
blaze4218 (09-23-2011)
Old 09-23-2011, 08:39 PM   PM User | #3
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
I wish I could change to a class, but you would have to take int consideration the entire loop:
Code:
    j=1;q=0;
    for (i in Employees){
     if(fromServer){
      Employees[i] = Employees[i].split('.');		<%/* BREAK EACH [EMP,PASS,STAT] PAIRING INTO SUB ARRAY */%>
      Employees[i][0] = Employees[i][0].split(' ');
      }
     empStat = (Employees[i][2])?(Employees[i][2]=='ft')?'Full Time':'Part Time':'Not Employed';
     if(Employees[i][2]||ID('showAll').checked){
      color1=(Employees[i][2])?"black":"red";colors=[4];
      j=j*-1;
      colors[0]=colors[1]=colors[2]=colors[3]=color2=(j==1)?'white':'#AAAAAA';
      q++;
      
      if(ChangedInputs){				<%/* CHECK FOR CHANGES THAT HAVEN'T BEEN SAVED YET */%>
       for(incr in ChangedInputs){			<%/* THIS WILL LOOP THROUGH ChangedInputs AT EVERY OBJECT IN Employee, IT SEEMS KIND OF EXPENSIVE ON RESOURCES, BUT I COULDN'T FIND A BETTER WAY TO SET THE COLORS HERE TO OVERRIDE THEIR DEFAULT VALUES... */%>
        temp = ChangedInputs[incr].split('.');
        if(temp[1]==i){
         colors[temp[0]]='#ff9999';
         }
        }
       }
      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);


      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"':'';
      empTable	+='<tr style="color:' + color1 + ' ; background-color:' + color2 + '"><td>ID</td><td>First Name</td></tr><tr style="background-color:' + color2 + '"><td>'					<%/* this is kinda hard to space logically, but I gave it a try... */%>
      		+ '<input autocorrect = "off" type="tel" id="idnmbr' + i 
      			+ '" onblur="CHECK_ID_AVAILABILITY(this.value,' + i + ');" '
      			+'onchange = "ID(\'toggleTagz\').style.display = \'\'; this.style.backgroundColor = \'#ff9999\';" '
      			+'value = "' + Employees[i][1] + '" '
      			+ 'style = "color:' + color1 + ' ; background-color:' + colors[0] + ' ; font-size:30px ; width:90px" />'
      		
      		+ '</td><td>'
      		+ '<input autocorrect = "off" autocapitalize = "off" '
      			+ 'style = "color:' + color1 + ' ; background-color:' + colors[1] + ' ; font-size:30px ; width:300px" '
      			+ 'onchange = "this.value = this.value.replace(/[^A-Za-z]/gi,\'\'); this.style.backgroundColor = \'#ff9999\'; UPDATE_EMPLOYEE(' + i + ',1,this.value);" '
      			+ 'mousedown=""'
      			+ 'value = "' + Employees[i][0][0] + '">'
      		 
      		+ '</td></tr><tr style = "color:' + color1 + ' ; background-color:' + color2 + '"><td>Status</td><td>Last Name</td></tr><tr style = "background-color:' + color2 + '"><td id="status' + i + '" style = "background-color:' + colors[3] + '">'
      		+ '<button style="color:'+color1+' ; width:125px ; height:35px" onclick="CHANGE_EMP_STATUS(' + i + ');">' + empStat + '</button>'
      		
      		+ '</td><td>'
      		+ '<input autocorrect = "off" autocapitalize = "off" '
      			+ 'style = "color:' + color1 + ' ; background-color:' + colors[2] + ' ; font-size:30px ; width:300px" '
      			+ 'onchange = "this.value = this.value.replace(/[^A-Za-z]/gi,\'\'); this.style.backgroundColor = \'#ff9999\'; UPDATE_EMPLOYEE(' + i + ',2,this.value);" '
      			+ 'value = ' + Employees[i][0][1] + '>'
      		
      		+ '</td></tr><tr style = "color:' + color1 + ' ; background-color:' + color2 + '"><td>Store</td><td></td></tr><tr style = "background-color:' + color2 + '"><td style ="background-color:' + colors[3] + '">'
      		+ '<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><td>'
      		+ ''
      		
      		+ '</td></tr>';
      }
     }
    ID('PleaseWait').style.display = 'none'
    empTable += '</table>';
    ID('allEmployees').innerHTML = empTable;
    ID('all').style.height = baseLineHeight + (q * tableHeight) + tableAddOnHeight + 'px';
    }
but your example looks spot on, I'm going to try it right now

Edit:
To Clarify, there *would be* like 6 permutations of classes to choose from, if you could improve upon it you would have my gratitude ( and a "Thanks" click )

Last edited by blaze4218; 09-23-2011 at 08:55 PM..
blaze4218 is offline   Reply With Quote
Old 09-23-2011, 08:50 PM   PM User | #4
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Like this?
Presumably, you will wrap that with a loop on variable i, to pick up all employees??
.
It is... From my original post:
Quote:
The following is inside a loop:
blaze4218 is offline   Reply With Quote
Old 09-23-2011, 08:58 PM   PM User | #5
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
Yep you got it! I guess I was just being lazy
But I like how you snuck in that [null] to make the array work!
blaze4218 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:57 AM.


Advertisement
Log in to turn off these ads.