【问题标题】:GWT - GXT - How to get Radio Button Value?GWT - GXT - 如何获取单选按钮值?
【发布时间】:2010-09-26 06:12:08
【问题描述】:

我正在使用 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"),我会得到一些 null 错误,而设置 setValue(boolean) 是有效的,但我期望得到的是单选值而不是标签值。

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    创建电台

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

    【讨论】:

    • radio.setValue() 正是我想要找到的!
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        看看这个

         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】:
                      Radio includeButton = new Radio();
                  Radio excludeButton = new Radio();
                  RadioGroup radioGroup = new RadioGroup();
          
          
          
          
                      radioGroup.add(includeButton);
              radioGroup.add(excludeButton);
          
                     includeButton.setvalue(true)//false
          

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

              另一种方法是使用radio.setValueAttribute(String) 方法。然后您可以在 RadioGroup 上使用以下代码来获取单击的单选按钮的 set 'value' 属性:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2014-12-05
                • 2015-06-26
                • 2013-03-28
                • 2021-08-26
                • 1970-01-01
                • 2011-03-03
                相关资源
                最近更新 更多