Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/06/2017, 10:59
Avatar de julia2021
julia2021
 
Fecha de Ingreso: diciembre-2014
Ubicación: Mexico
Mensajes: 152
Antigüedad: 9 años, 4 meses
Puntos: 11
Sonrisa JS me desordena mi arreglo :-(

Hola amiguitos, estoy armando una lista select con JS y me funciona muy bien, sin embargo me pierde el orden y me muestra la lista desordenada. Les muestro el código a ver si me pueden echar una mano

Primero construyo un arreglo llamado $_offices_and_cities que tiene la siguiente forma:
Código:
id_sucursal, id_cliente, nombre, id_ciudad, nombre_ciudad

2, 28, Los Dos Caminos, 149, Caracas
1, 28, Sede Principal, 149, Caracas
7, 9, Torre Hp, 149, Caracas
9, 28, Caripe Del Guacharo, 301, Caripe
8, 22, Casigua Del Cubo, 468, Casigua
11, 28, Ejido, 240, Ejido
5, 22, Agencia Paseo Del Lago, 487, Maracaibo
10, 28, Las Delicias, 64, Maracay
Ahora aplico mi JS
Código Javascript:
Ver original
  1. <? // INICIO: LISTAS DEPENDIENTES: CLIENTE >> SUCURSAL ///////////////////// ?>
  2. <SCRIPT LANGUAGE="JavaScript">
  3. var arrSucursales   = new Array();
  4. var arrSucursalesId = new Array();
  5.  
  6. <? // Armamos la lista desplegable de los SUCURSALES y las ciudades
  7.  
  8. foreach ($_offices_and_cities as $clave => $sucursal) {
  9.  
  10.     echo 'arrSucursales['.$sucursal['id_sucursal'].'] = "'.$sucursal['nombre_ciudad'].': '.$sucursal['nombre'].'"';echo "\n";
  11.     echo 'arrSucursalesId['.$sucursal['id_sucursal'].'] = '.$sucursal['id_cliente'];echo "\n";
  12. }
  13. ?>
  14. function selectChange(control, controlToPopulate2, ItemArray2, GroupArray2)
  15. {
  16.     // Armamos la lista de SUCURSALES
  17.     var myEle2 ;
  18.     var x2 ;
  19.     // Empty the second drop down box of any choices
  20.     for (var q2=controlToPopulate2.options.length;q2>=0;q2--) controlToPopulate2.options[q2]=null;
  21.     // ADD Default Choice - in case there are no values
  22.     myEle2 = document.createElement("option") ;
  23.     myEle2.value = 0 ;
  24.  
  25.     if (control.name == "id_cliente") {
  26.         myEle2.text = "Seleccione la oficina..." ;
  27.     }
  28.     controlToPopulate2.add(myEle2) ;
  29.     // Now loop through the array of individual items
  30.     // Any containing the same child id are added to
  31.     // the second dropdown box
  32.     for ( x2 = 0 ; x2 < ItemArray2.length  ; x2++ )
  33.     {
  34.         if ( GroupArray2[x2] == control.value )
  35.         {
  36.             myEle2 = document.createElement("option") ;
  37.             myEle2.value = x2 ;
  38.             myEle2.text = ItemArray2[x2] ;
  39.             controlToPopulate2.add(myEle2) ;
  40.         }
  41.     }
  42. }
  43. //  End -->
  44. </script>
  45.  
  46. <? // FINAL: LISTAS DEPENDIENTES: CLIENTE >> SUCURSAL ///////////////////// ?>
Sin embargo para el cliente 28 me muestra la lista con el siguiente orden:
Código:
- Caracas: Sede principal (1)
- Caracas: Los Dos Caminos (2)
- Caripe: Caripe del Guacharo (9)
- Maracay: Las Delicias (10)
- Elijo: Elijo (11)
Cómo puedo hacer para ordenarlo por nombre_ciudad y no por id_sucursal ???