Comunidad de diseño web y desarrollo en internet online

como clickear en los elementos en un <List>??

Citar            
MensajeEscrito el 09 Abr 2010 06:33 pm
Hola:
tengo a grandes rasgos el siguiente codigo:

Código :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

<mx:ArrayCollection id="foodCollection">
    <mx:Array id="foods">
        <mx:Object name="Broccoli" type="Vegetable" />
        <mx:Object name="Apple" type="Fruit" />
        <mx:Object name="Orange" type="Fruit" />
    </mx:Array>
</mx:ArrayCollection>
<mx:Panel tittle="comida">
<mx:List dataProvider="{foodCollection}" labelField="name" />
</mx:Panel>
</mx:Application>


esto me crea un panel vertical con todos los nombres. Me gustaria poder clikear en cada uno de ellos y que me llame a una funcion u otra que me dibuja un poligono, como lo hago?? (las funciones que dibujan ya las tengo creadas, es simplemente hacer la llamada)

Por yeayu

18 de clabLevel



 

firefox
Citar            
MensajeEscrito el 10 Abr 2010 12:52 pm
Mírate en la ayuda los eventos del componente List, en especial el evento change

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 12 Abr 2010 01:42 pm

Código ActionScript :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

<mx:ArrayCollection id="foodCollection">
    <mx:Array id="foods">
        <mx:Object name="Broccoli" type="Vegetable" />
        <mx:Object name="Apple" type="Fruit" />
        <mx:Object name="Orange" type="Fruit" />
    </mx:Array>
</mx:ArrayCollection>
<mx:Panel tittle="comida">
 <mx:List dataProvider="{foodCollection}" labelField="name" change="dibuja('selectedItem.name')" height="200" width="140">

</mx:Panel>
<mx:scripth>
<![CDATA[
[Bindable]
    public var selectedItem:Object;

private function dibuja(type:String):void {
//segun lo que clikes en el List, tiene que dibujar una cosa u otra        
     }
 ]]>
</mx:scripth>
</mx:Application>


este código siempre me dibuja la misma cosa, da igual donde clicke!! que hago mal?

Por yeayu

18 de clabLevel



 

firefox
Citar            
MensajeEscrito el 12 Abr 2010 02:10 pm
Normalmente (al menos en Flex 3) las funciones change tienen un argumento que es "event". Así que la equiteta sería

Código ActionScript :

 <mx:List dataProvider="{foodCollection}" labelField="name" change="dibuja(event)" height="200" width="140"> 

Tu función dibuja, recoje pues un objeto de tipo event. De ahí puedes sacar el selectedItem y de ahí los valores del atributo

Código ActionScript :

private function dibuja(eventObject:Object):void { 
  Alert.show(eventObject.target.selectedItem.type);
  Alert.show(eventObject.target.selectedItem.name);
} 

NOTA:reconozco que no ando muy ducho, pero la etiqueta <mx:scripth> creo que debería ser <mx:Script>. Igualmente falta cerrar la equiteta al mx:List

Por Eliseo2

710 de clabLevel



 

firefox
Citar            
MensajeEscrito el 12 Abr 2010 02:25 pm
llevas razon, los pequeños fallos han sido de no hacer un copy paste..

la funcion como deberia ser??

Código ActionScript :

private function dibuja(eventObject:Event):void {
        //si es broccoli por ejemplo
        //que llame a dibujabrocoli

        //si es otro a dibujaotro     
     }


eso como lo haria?

puedo hacerlo con un if?? tal que:

Código ActionScript :

if(selectedItem.name=="brocoli")
dibujabrocoli();
else
dibujaapple();

Por yeayu

18 de clabLevel



 

firefox
Citar            
MensajeEscrito el 12 Abr 2010 02:35 pm
Solucionado!
lo he hecho con

Código ActionScript :

if(eventObject.target.selectedItem.name="brocoli")
dibujabrocoli();
else
dibujaapple();


gracias por la ayuda

Por yeayu

18 de clabLevel



 

firefox
Citar            
MensajeEscrito el 12 Abr 2010 02:38 pm
No es necesario que pases como argumento el callback inline, puedes hacer

change="dibuja()"

Dale id a la lista

<mx:List id="listado" ....

Luego el callback

Código ActionScript :

private function dibuja():void { 
        if(listado.selectedItem.label=="brocoli"){
          .......
        } else  if(listado.selectedItem.label=="pepino"){
           ....
        }
   etc
 


Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 12 Abr 2010 02:44 pm
Yeau, sí, con un if o con un switch. Usa una variable intermedia

Código ActionScript :

private function dibuja(eventObject:Object):void { 
  var nombre:String=eventObject.target.selectedItem.name;
  if (nombre=="Broccoli"){
         dibujabrocoli()
  }
  if (nombre=="Apple"){
         dibujaapple()
  }
}  

Código ActionScript :

private function dibuja(eventObject:Object):void { 
  var nombre:String=eventObject.target.selectedItem.name;
  switch (nombre){
     case "Broccoli":
         dibujablocoli()
     break
     case "Apple":
         dibujaapple()
     break
  }
}  

Pero lo importante es que entiendas que:
1.-En las funciones tipo change siempre ponemos como argumento "event" (que es una palabra reservada)
[as]
<mx:List change="dibuja(event) ..../>"
2.-Nuestras funciones que definimos en el mx:script tendrán por tanto un argumento de tipo Event (también le puedes indicar Object)
[as]
private function dibuja(eventObject:Event):void {...}
[/as]
3.-Como he llamado a la variable "eventObject",
en eventObject.target tienes el mx:List (en general en event.target tendremos el Objeto que "cambia")
en eventObject.target.selectedItem tienes el item seleccionado
en eventObject.target.selectedItem.name tienes el atributo "name" del item seleccionado (siempre será un STRING)
en eventObject.target.selectedItem.type tienes el atributo "type" del item seleccionado (siempre será un STRING)

Si la hubiera llamado "e", p.e.
[as]
private function dibuja(e:Event):void {...}
[/as]
DENTRO de la función e.target sería el mx:List, e.target.selectedItem el item seleccionado......

Por Eliseo2

710 de clabLevel



 

firefox
Citar            
MensajeEscrito el 12 Abr 2010 03:07 pm
vaya explicazo me acabas de dar. Muchas gracias
:D

Por yeayu

18 de clabLevel



 

firefox
Citar            
MensajeEscrito el 12 Abr 2010 03:10 pm

solisarg escribió:

No es necesario que pases como argumento el callback inline, puedes hacer

change="dibuja()"

Dale id a la lista

<mx:List id="listado" ....

Luego el callback

Código ActionScript :

private function dibuja():void { 
        if(listado.selectedItem.label=="brocoli"){
          .......
        } else  if(listado.selectedItem.label=="pepino"){
           ....
        }
   etc
 


Jorge

Haz caso a Jorge, que sabe de esto bastante más que yo

Por Eliseo2

710 de clabLevel



 

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.