GWT - GXT - 如何获得单选按钮值?

时间:2008-12-10 14:43:32

标签: gwt extjs radio-button gwt-rpc gxt

我正在使用GWT(Google Web Toolkit)1.5.3和GXT(ExtJS)1.2 我只想创建一个简单的表单,在RPC调用后生成一些单选按钮,以获取一些值

代码:

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);

我的问题是我无法设置/获取单选按钮值。 如果我尝试setRawValue(“xxxxxxx”),我会得到一些空错误,而设置setValue(boolean)正在工作,但我期望得到无线电值而不是标签值。

任何想法?

7 个答案:

答案 0 :(得分:2)

制作广播

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);

获取选定的值

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

答案 1 :(得分:1)

您需要扩展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);
     }

}

默认它只允许布尔值。初始化单选按钮后,您需要设置值。

答案 2 :(得分:0)

使用radioButton.setItemId()和getItemId()解决它。

答案 3 :(得分:0)

我使用radio.setAttributeValue()方法为单选按钮设置值。

答案 4 :(得分:0)

看看这个

 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(); 

答案 5 :(得分:0)

另一种方法是使用radio.setValueAttribute(String)方法。然后,您可以在RadioGroup上使用以下代码来获取设置值&#39;单击的单选按钮的属性:

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 !!!

  }
});

答案 6 :(得分:0)

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




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

           includeButton.setvalue(true)//false