2008-12-10 6 views
3

J'utilise GWT (Google Web Toolkit) 1.5.3 et GXT (ExtJS) 1.2 Je veux juste créer un formulaire simple avec quelques boutons radio générés après un appel RPC, pour obtenir des valeursGWT - GXT - Comment obtenir la valeur du bouton radio?

code:

final FormPanel simple = new FormPanel(); 
     simple.setFrame(true); 
     simple.setWidth(350); 
     simple.setHeaderVisible(false); 

     DateField date = new DateField(); 
     date.setFieldLabel("Date"); 
     simple.add(date); 

     ListFluxServiceAsync service = (ListFluxServiceAsync) 
     GWT.create(ListFluxService.class); 
     ServiceDefTarget target = (ServiceDefTarget)service; 
     String url = GWT.getModuleBaseURL() + "flux.rpc"; 
     target.setServiceEntryPoint(url); 

     final RadioGroup radioGroup = new RadioGroup("RadioGroup"); 
     radioGroup.setFieldLabel("Flux"); 
     radioGroup.setOrientation(Orientation.VERTICAL); 

     service.getAllFlux(new AsyncCallback<List<FluxModelData>>(){ 

      public void onFailure(Throwable caught) { 
       GWT.log("flux.rpx::onFailure", caught); 
       MessageBox.alert("what?", "onFailure :" + caught.getMessage(), null); 
      } 

      public void onSuccess(List<FluxModelData> result) { 

       Iterator<FluxModelData> it = result.iterator(); 
       while (it.hasNext()){ 
        FluxModelData fmd = it.next(); 
        Radio radio = new Radio(); 
        radio.setName("flux"); 
        radio.setValue(true); 
        //radio.setRawValue("my very long value"); 
        radio.setBoxLabel(fmd.getDescription()); 
        radioGroup.add(radio); 
       } 

       simple.add(radioGroup); 
       simple.layout(); //we need it to show the radio button 

      } 
     }); 



     simple.setButtonAlign(HorizontalAlignment.CENTER); 

     Button button = new Button("Récupérer"); 
     button.addSelectionListener(new SelectionListener<ButtonEvent>(){ 

      @Override 
      public void componentSelected(ButtonEvent ce) { 

       MessageBox.alert("what?", radioGroup.getValue().getRawValue() , null); 

      }}); 

     simple.addButton(button); 


     RootPanel.get().add(simple); 

Mon problème est que je ne peux pas définir/obtenir la valeur du bouton radio. Si j'essaie setRawValue ("xxxxxxx"), j'obtiendrai des erreurs nulles, alors que setValue (boolean) fonctionne mais je m'attendais à obtenir la valeur de la radio et non la valeur de l'étiquette.

Une idée?

Répondre

0

L'utilisation de radioButton.setItemId() et getItemId() le résout.

0

J'utilise la méthode radio.setAttributeValue() pour définir la valeur du bouton radio.

2

Créer la radio

Radio radio = new Radio(); 
radio.setBoxLabel("Si"); 
radio.setValue(true); 
radio.setValueAttribute("true"); 

Radio radio2 = new Radio(); 
radio2.setBoxLabel("No"); 
radio2.setValueAttribute("false"); 

RadioGroup radioGroup = new RadioGroup(); 
radioGroup.setFieldLabel("Afecto"); 
radioGroup.add(radio); 
radioGroup.add(radio2); 

obtenir la valeur sélectionnée

Boolean b = Boolean.parseBoolean(radioGroup.getValue().getValueAttribute()); 
+0

radio.setValue() était exactement ce que je cherchais à trouver! – pbojinov

1

Vous devez étendre la classe GWT RadioButton ex:

public class ExtRadioButton extends RadioButton { 
    public ExtRadioButton(String name, String label) { 
     super(name, label); 
     // TODO Auto-generated constructor stub 
    } 

    public void setValue(String value) 
    { 
     Element span = getElement(); 
     Element input = DOM.getChild(span,0); 
     DOM.setElementAttribute(input,"value",value); 
    } 

} 

défaut, il permet seulement une valeur booléenne. Après avoir initialisé le bouton radio, vous devez définir la valeur.

0

Vérifiez cette

Radio radio1 = new Radio(); 
............. 
Radio radio2 = new Radio(); 
............. 

in order to get value you can do as follow 

String value = (radio1.getValue()) ? radio1.getText() : radio2.getText(); 
0

L'autre façon de le faire est d'utiliser la méthode radio.setValueAttribute(String). Ensuite, vous pouvez utiliser le code suivant sur la RadioGroup pour obtenir l'ensemble attribut « valeur » du bouton cliqué Radio:

radioGroup.addListener(Events.Change, new Listener<BaseEvent>() { 
    @Override 
    public void handleEvent(BaseEvent be) 
    { 
    final RadioGroup radioGroup = (RadioGroup)be.getSource(); 

    final Radio clickedRadioBtn = radioGroup.getValue(); 

    final String valueAttribute = clickedRadioBtn.getValueAttribute(); // Correct !!! 

    } 
}); 
0
  Radio includeButton = new Radio(); 
     Radio excludeButton = new Radio(); 
     RadioGroup radioGroup = new RadioGroup(); 




      radioGroup.add(includeButton); 
    radioGroup.add(excludeButton); 

      includeButton.setvalue(true)//false 
Questions connexes