访问小部件上的值并发送到服务器 [英] Access values on widgets and send to server

查看:102
本文介绍了访问小部件上的值并发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从GXT 2.2.5库创建的接口,现在我需要从TextField获取用户输入的值并将其发送到服务器。我怎样才能让这个用户从文本框输入值?这里的示例代码:

  package kz.bimash.client; 

导入com.extjs.gxt.ui.client.widget.LayoutContainer;

导入com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Slider;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;

public class Form extends LayoutContainer {

private VerticalPanel vp;

private FormData formData;

@Override
protected void onRender(Element parent,int index){
super.onRender(parent,index);
formData = new FormData( - 20);
vp = new VerticalPanel();
vp.ensureDebugId(vps);
vp.setSpacing(10);
// createForm1();
createForm2();
add(vp);



$ b private void createForm2(){
FormPanel form2 = new FormPanel();
form2.ensureDebugId(pan);
form2.setFrame(true);
form2.setHeading(带FieldSets的简单表单);
form2.setWidth(350);
form2.setLayout(new FlowLayout());

FieldSet fieldSet = new FieldSet();
fieldSet.setHeading(用户信息);
fieldSet.setCheckboxToggle(true);

FormLayout layout = new FormLayout();
layout.setLabelWidth(75);
fieldSet.setLayout(layout);

TextField< String> firstName = new TextField< String>();

firstName.setFieldLabel(First Name);
firstName.ensureDebugId(firstName);
firstName.setValue(olzheke);
firstName.setAllowBlank(false);
fieldSet.add(firstName,formData);

TextField< String> lastName = new TextField< String>();
lastName.setFieldLabel(Last Name);
lastName.setValue(firstName.getItemId());
fieldSet.add(lastName,formData);

TextField< String> company = new TextField< String>();
company.setFieldLabel(Company);
fieldSet.add(company,formData);

TextField< String> email = new TextField< String>();
email.setFieldLabel(电子邮件);
fieldSet.add(email,formData);

form2.add(fieldSet);
fieldSet = new FieldSet();
fieldSet.setHeading(电话号码);
fieldSet.setCollapsible(true);

layout = new FormLayout();
layout.setLabelWidth(75);
fieldSet.setLayout(layout);

TextField< String> field = new TextField< String>();
field.setFieldLabel(Home);
fieldSet.add(field,formData);

field = new TextField< String>();
field.setFieldLabel(Business);
fieldSet.add(field,formData);

field = new TextField< String>();
field.setFieldLabel(Mobile);
fieldSet.add(field,formData);

field = new TextField< String>();
field.setFieldLabel(Fax);
fieldSet.add(field,formData);

form2.add(fieldSet);
form2.setButtonAlign(Horizo​​ntalAlignment.CENTER);
form2.addButton(new Button(Save));
form2.addButton(new Button(Cancel));

vp.add(form2);
}

}

这里是EntryPoint实现的类,我要发送的内容:

  package kz.bimash.client; 




导入com.extjs.gxt.ui.client.widget.Document;
import com.extjs.gxt.ui.client.widget.MessageBox;

import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui。*;


public class GWTSample实现EntryPoint {
private UserServiceAsync prox;
私人HTML标签1,button1;

public void onModuleLoad(){
prox = GWT.create(UserService.class);
// Window.enableScrolling(false);
// Window.setMargin(0px);
Form form = new Form();

form.setVisible(true);
按钮btn =新建按钮(Click me!);
//最终RootPanel root = RootPanel.get();
final RootLayoutPanel root = RootLayoutPanel.get();

btn.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event){
prox.getMsq(new AsyncCallback< String>() {
@Override
public void onFailure(Throwable caught){
//使用File | Settings | File Templates。
}

@Override
public void onSuccess(String result){

// MessageBox.info(testing,this is ext gwt,null);
}
});
}
});

TextField< String> tf =(TextField< String>)form.getItemByItemId(firstName);
Label lab = new Label();
//lab.setText(tf.getValue());
// root.add(lab);
root.get()。add(form);

root.get()。add(btn);
//Document.get()。getElementByID()
}

}

单击按钮时,我需要从Texfield中检索值并将其发送到服务器,我如何访问这些值?

解决为什么不在你的Form类中创建一个方法?

  public class Form extends LayoutContainer {

TextField< String>名字;

public String getFirstName(){
return firstName.getValue();





$ b然后在onModuleLoad( )

  public void onModuleLoad(){
...
final Form form = new Form() ;



btn.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event){
String firstName = form.getFirstName();
}
...
}
}


I have an interface created from GXT 2.2.5 library, now I need to get user entered values from TextField and send it to server. How can i get this user entered values from textfield? Here the sample code:

package kz.bimash.client;

import com.extjs.gxt.ui.client.widget.LayoutContainer;

import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Slider;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;

public class Form extends LayoutContainer {

private VerticalPanel vp;

private FormData formData;

@Override
protected void onRender(Element parent, int index) {
    super.onRender(parent, index);
    formData = new FormData("-20");
    vp = new VerticalPanel();
    vp.ensureDebugId("vps");
    vp.setSpacing(10);
    //createForm1();
    createForm2();
    add(vp);
}



private void createForm2() {
    FormPanel form2 = new FormPanel();
    form2.ensureDebugId("pan");
    form2.setFrame(true);
    form2.setHeading("Simple Form with FieldSets");
    form2.setWidth(350);
    form2.setLayout(new FlowLayout());

    FieldSet fieldSet = new FieldSet();
    fieldSet.setHeading("User Information");
    fieldSet.setCheckboxToggle(true);

    FormLayout layout = new FormLayout();
    layout.setLabelWidth(75);
    fieldSet.setLayout(layout);

    TextField<String> firstName = new TextField<String>();

    firstName.setFieldLabel("First Name");
    firstName.ensureDebugId("firstName");
    firstName.setValue("olzheke");
    firstName.setAllowBlank(false);
    fieldSet.add(firstName, formData);

    TextField<String> lastName = new TextField<String>();
    lastName.setFieldLabel("Last Name");
    lastName.setValue(firstName.getItemId());
    fieldSet.add(lastName, formData);

    TextField<String> company = new TextField<String>();
    company.setFieldLabel("Company");
    fieldSet.add(company, formData);

    TextField<String> email = new TextField<String>();
    email.setFieldLabel("Email");
    fieldSet.add(email, formData);

    form2.add(fieldSet);
    fieldSet = new FieldSet();
    fieldSet.setHeading("Phone Numbers");
    fieldSet.setCollapsible(true);

    layout = new FormLayout();
    layout.setLabelWidth(75);
    fieldSet.setLayout(layout);

    TextField<String> field = new TextField<String>();
    field.setFieldLabel("Home");
    fieldSet.add(field, formData);

    field = new TextField<String>();
    field.setFieldLabel("Business");
    fieldSet.add(field, formData);

    field = new TextField<String>();
    field.setFieldLabel("Mobile");
    fieldSet.add(field, formData);

    field = new TextField<String>();
    field.setFieldLabel("Fax");
    fieldSet.add(field, formData);

    form2.add(fieldSet);
    form2.setButtonAlign(HorizontalAlignment.CENTER);
    form2.addButton(new Button("Save"));
    form2.addButton(new Button("Cancel"));

    vp.add(form2);
}

}

Here is the EntryPoint implemented class, where i am going to send:

package kz.bimash.client;




import com.extjs.gxt.ui.client.widget.Document;
import com.extjs.gxt.ui.client.widget.MessageBox;

import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;


public class GWTSample implements EntryPoint {
private UserServiceAsync prox;
private HTML label1, button1;

public void onModuleLoad() {
    prox = GWT.create(UserService.class);
   // Window.enableScrolling(false);
  //  Window.setMargin("0px");
    Form form = new Form();

    form.setVisible(true);
  Button btn = new Button("Click me!");
   // final RootPanel root=RootPanel.get();
    final RootLayoutPanel root = RootLayoutPanel.get();

    btn.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            prox.getMsq(new AsyncCallback<String>() {
                @Override
                public void onFailure(Throwable caught) {
                  //To change body of implemented methods use File | Settings | File Templates.
                }

                @Override
                public void onSuccess(String result) {

                   // MessageBox.info("testing","this is ext gwt",null);
                }
            });
        }
    });

    TextField<String> tf = (TextField<String>) form.getItemByItemId("firstName");
    Label lab = new Label();
    //lab.setText(tf.getValue());
   // root.add(lab);
    root.get().add(form);

    root.get().add(btn);
    //Document.get().getElementByID()
}

}

when the button is clicked i need to retreive values from Texfield and send it to server, how can i access those values?

解决方案

why don't you just make a method in your Form class?

public class Form extends LayoutContainer {

    TextField<String> firstName;

    public String getFirstName(){
      return firstName.getValue();
    }
  ...
}

Then in onModuleLoad()

public void onModuleLoad() {
    ...
    final Form form = new Form();

    ...

    btn.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) { 
            String firstName =form.getFirstName();
         }
      ...
    }
}

这篇关于访问小部件上的值并发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆