[英]gwt - celltable - adding extra row
在这里输入代码我有一个单元格表,列中包含一些数字。 我想在表的末尾添加一行,以容纳每一列的总数。 有没有办法做到这一点?
以下是我的代码:
import java.util.*;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ListDataProvider;
public class TestProject implements EntryPoint
{
private static int totalSalary=0;
private static class Contact
{
private final String salary;
private final String name;
public Contact(String name, String salary)
{
this.name = name;
this.salary = salary;
}
}
private static List<Contact> CONTACTS = Arrays.asList(new Contact("John","100000"),
new Contact("Mary", "200000"),
new Contact("Zander", "300000"));
/**
* This is the entry point method.
*/
public void onModuleLoad()
{
final CellTable<Contact> table = new CellTable<Contact>();
// Create name column.
TextColumn<Contact> nameColumn = new TextColumn<Contact>()
{
@Override
public String getValue(Contact contact)
{
return contact.name;
}
};
// Create address column.
TextColumn<Contact> addressColumn = new TextColumn<Contact>()
{
@Override
public String getValue(Contact contact)
{
totalSalary+=Integer.parseInt(contact.salary);
return contact.salary;
}
};
// Add the columns.
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Salary");
// Create a data provider.
ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();
// Connect the table to the data provider.
dataProvider.addDataDisplay(table);
// Add the data to the data provider, which automatically pushes it to the
// widget.
final List<Contact> list = dataProvider.getList();
for (Contact contact : CONTACTS) {
list.add(contact);
}
// We know that the data is sorted alphabetically by default.
table.getColumnSortList().push(nameColumn);
Contact total = new Contact("Total: ",totalSalary+"");
list.add(total);
// Add it to the root panel.
RootPanel.get().add(table);
//RootPanel.get().add(add);
}
}
我也建议您在添加列时使用footer-Argument:addColumn(Column col,Header header)
当您表示总计时,我不太清楚您的意思,但这与您的代码类似,但是我添加了一个按钮,该按钮将添加行,您可以删除并仅添加行。
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestGwt implements EntryPoint {
private static class Contact {
private final String address;
private final String name;
public Contact(String name, String address) {
this.name = name;
this.address = address;
}
}
private static List<Contact> CONTACTS = Arrays.asList(new Contact("John",
"123 Fourth Road"), new Contact("Mary", "222 Lancer Lane"), new Contact(
"Zander", "94 Road Street"));
/**
* This is the entry point method.
*/
public void onModuleLoad() {
// Create a CellTable.
final CellTable<Contact> table = new CellTable<Contact>();
// Create name column.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact contact) {
return contact.name;
}
};
// Make the name column sortable.
nameColumn.setSortable(true);
// Create address column.
TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact contact) {
return contact.address;
}
};
// Add the columns.
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Address");
// Create a data provider.
ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();
// Connect the table to the data provider.
dataProvider.addDataDisplay(table);
// Add the data to the data provider, which automatically pushes it to the
// widget.
final List<Contact> list = dataProvider.getList();
for (Contact contact : CONTACTS) {
list.add(contact);
}
// We know that the data is sorted alphabetically by default.
table.getColumnSortList().push(nameColumn);
Button add = new Button("Add Row");
add.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
list.add(new Contact(Integer.toString(table.getRowCount()),Integer.toString(table.getRowCount())));
}
});
// Add it to the root panel.
RootPanel.get().add(table);
RootPanel.get().add(add);
}
}
希望这将有助于我不确定您的总含义,尽管可以将任何内容添加到联系人的Guise的最后一个字段中,即使不是必需的,更好的方法是将Generics用于数据提供者,但这将用最少的代码即可达到相同的效果
public void addNewRow(){
List<Contact> newContactLst = Arrays.asList(new Contact("TEST",
"Sample"));
int numRows = table.getRowCount();
table.setRowCount(numRows+1);
table.setRowData(numRows,newContactLst);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.