GWT CellTable

西门胜涝
2023-12-01
package com.cn.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/

public class CellTableTest implements EntryPoint {

/**
* A simple data type that represents a contact.
*/
private static class Contact {
private String address;
private Date birthday;
private String name;
private String firstName;
private String lastName;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Contact(String firstName, String lastName, Date birthday,
String address) {
this.firstName = firstName;
this.lastName = lastName;
this.name = firstName + " " + lastName;
this.birthday = birthday;
this.address = address;
}

public static final ProvidesKey<Contact> KEY_PROVIDER = new ProvidesKey<Contact>() {
public Object getKey(Contact item) {
return item == null ? null : item.getName();
}
};
}
private Label selectedLabel = new Label();
public void onModuleLoad() {
// Create a CellTable.
final CellTable<Contact> cellTable = new CellTable<Contact>();
// Display n rows in one page
cellTable.setPageSize(10);
cellTable.setWidth("1024px");

// Add a selection model so we can select cells.
final MultiSelectionModel<Contact> selectionModel = new MultiSelectionModel<Contact>(
Contact.KEY_PROVIDER);
cellTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager.<Contact> createCheckboxManager());
selectionModel.addSelectionChangeHandler(
new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
StringBuilder sb = new StringBuilder();
boolean first = true;
List<Contact> selected = new ArrayList<Contact>(selectionModel.getSelectedSet());
for (Contact value : selected) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(value.getName());
}
selectedLabel.setText("选择:"+sb.toString());
}
});

Column<Contact, Boolean> checkColumn = new Column<Contact, Boolean>(
new CheckboxCell(true, false)) {
@Override
public Boolean getValue(Contact object) {
return selectionModel.isSelected(object);
}
};
cellTable.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
cellTable.setColumnWidth(checkColumn, 10, Unit.PX);
Column<Contact, String> firstNameColumn = new Column<Contact, String>(
new EditTextCell()) {
@Override
public String getValue(Contact object) {
return object.getFirstName();
}
};

cellTable.addColumn(firstNameColumn, "名字");
firstNameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
public void update(int index, Contact object, String value) {
// Called when the user changes the value.
object.setFirstName(value);
}
});
cellTable.setColumnWidth(firstNameColumn, 10, Unit.PCT);

Column<Contact, String> lastNameColumn = new Column<Contact, String>(
new EditTextCell()) {
@Override
public String getValue(Contact object) {
return object.getLastName();
}
};
lastNameColumn.setSortable(true);
cellTable.addColumn(lastNameColumn, "姓");
lastNameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
public void update(int index, Contact object, String value) {
object.setLastName(value);
}
});
cellTable.setColumnWidth(lastNameColumn, 10, Unit.PCT);
// Add a text column to show the name.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact object) {
return object.getFirstName() + " " + object.getLastName();
}
};
cellTable.addColumn(nameColumn, "全名");
cellTable.setColumnWidth(nameColumn, 20, Unit.PCT);
// Add a date column to show the birthday.
DateCell dateCell = new DateCell();
Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
@Override
public Date getValue(Contact object) {
return object.birthday;
}
};
cellTable.addColumn(dateColumn, "生日");
cellTable.setColumnWidth(dateColumn, 40, Unit.PCT);
// Add a text column to show the address.

TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact object) {
return object.address;
}
};
cellTable.addColumn(addressColumn, "地址");
cellTable.setColumnWidth(addressColumn, 50, Unit.PCT);
// Associate an async data provider to the cellTable
// XXX: Use AsyncCallback in the method onRangeChanged
// to actaully get the data from the server side
AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>() {
@Override
protected void onRangeChanged(HasData<Contact> display) {
// int start = display.getVisibleRange().getStart();
// int end = start + display.getVisibleRange().getLength();
// end = end >= CONTACTS.size() ? CONTACTS.size() : end;
// List<Contact> sub = CONTACTS.subList(start, end);
List<Contact> subs = new ArrayList<Contact>();
Contact c1 = new Contact("王", "栋", new Date(), "张家口下花园村");
Contact c2 = new Contact("张", "强", new Date(), "张家港市张家港村");
Contact c3 = new Contact("王", "小鹏", new Date(), "河北省保定市宝莲灯村");
Contact c4 = new Contact("时", "亮", new Date(), "北京市海淀区");
Contact c5 = new Contact("王", "小贺", new Date(), "承德市朝阳区");
Contact c6 = new Contact("李", "东墙", new Date(), "北京市花园村");
Contact c7 = new Contact("赵", "大胆", new Date(), "北京市花园村");
Contact c8 = new Contact("张", "启灵", new Date(), "北京市花园村");
Contact c9 = new Contact("王", "胖子", new Date(), "北京市花园村");
Contact c10 = new Contact("谢", "子扬", new Date(), "北京市昌平区谢家屯");
Contact c11 = new Contact("齐", "殿", new Date(), "北京市海淀区西北旺村");
Contact c12 = new Contact("楚", "穆", new Date(), "北京市亚运村");
Contact c13 = new Contact("冰", "窟窿", new Date(), "北京市奥运村");
subs.add(c1);
subs.add(c2);
subs.add(c3);
subs.add(c4);
subs.add(c5);
subs.add(c6);
subs.add(c7);
subs.add(c8);
subs.add(c9);
subs.add(c10);
subs.add(c11);
subs.add(c12);
subs.add(c13);
updateRowData(0, subs);
}
};
provider.addDataDisplay(cellTable);
// provider.updateRowCount(CONTACTS.size(), true);

SimplePager pager = new SimplePager();
pager.setDisplay(cellTable);

VerticalPanel vp = new VerticalPanel();
HorizontalPanel hp = new HorizontalPanel();
Button cellTableButton = new Button("提交");
cellTableButton.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
String select = selectedLabel.getText();
Window.alert(select);
/*
String[] selectArray = select.split(",");
if(selectArray!=null && selectArray.length>0){
for(int i=0;i<selectArray.length;i++){
Window.alert(selectArray[i]);
}
}
*/
}

});
hp.add(selectedLabel);
hp.add(cellTableButton);
vp.add(hp);
vp.setCellHorizontalAlignment(hp, HorizontalPanel.ALIGN_RIGHT);
vp.add(cellTable);

vp.add(pager);
vp.setCellHorizontalAlignment(pager, HorizontalPanel.ALIGN_CENTER);
// Add it to the root panel.
RootPanel.get().add(vp);
}
}
 类似资料:

相关阅读

相关文章

相关问答