I just want to update the CellTable columns.
See for eg.
Header1 | Header2 | Header3 |
-------------------------------
Column11| Column12| Column13|
Column21| Column22| Column23|
Column31| Column32| Column33|
final TextColumn<Student> nameColumn = new TextColumn<Student>() {
#Override
public String getValue(Student object) {
return object.getName();
}
};
cellTable.addColumn(nameColumn, "Name");
final TextColumn<MyDTO> classColumn = new TextColumn<MyDTO>() {
#Override
public String getValue(Student object) {
return object.getClassName();
}
};
cellTable.addColumn(classColumn, "Age");
final TextColumn<Student> subjectColumn = new TextColumn<Student>() {
#Override
public String getValue(Student object) {
return object.getMark();
}
};
cellTable.addColumn(subjectColumn, "Subject");
When performing some operation, I just want to update Column22, column23 etc... like that Column32, Column33....
Can anyone help me?
Thanks in advance,
Gnik
You can use a data provider like ListDataProvider for the CellTable. The data provider holds the data for the table and is associated with the table. So when there are any changes to the data in the data provider, the changes will be reflected in the table when you call refresh() method. Here is the complete code(I have modified a little the code available here)
public class GWTCellTable implements EntryPoint {
// A simple data type that represents a contact.
private static class Contact {
private String address;
private String name;
public Contact(String name, String address) {
this.name = name;
this.address = address;
}
}
// The list of data to display.
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"));
public void onModuleLoad() {
// Create a CellTable.
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) {
return contact.address;
}
};
// Add the columns.
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Address");
// Create a data provider.
final 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.
List<Contact> list = dataProvider.getList();
for (Contact contact : CONTACTS) {
list.add(contact);
}
// Add it to the root panel.
RootPanel.get().add(table);
Button button = new Button("Update table");
RootPanel.get().add(button);
//Clicking on the update button, updates the first row of the table
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
dataProvider.getList().get(0).name = "Ganesh";
dataProvider.getList().get(0).address = "Gachibowli";
dataProvider.refresh();
}
});
}
}
Related
I have created a table using table viewer and now i need to filter based on the text entered in the text box so how can we filter the table the code to create table is as follows
TableViewerColumn message = new TableViewerColumn(viewer, SWT.NONE);
message.getColumn().setWidth(800);
message.getColumn().setText("Message");
message.setLabelProvider(new ColumnLabelProvider()
{
#Override
public void update(ViewerCell cell)
{
Object element = cell.getElement();
if(element instanceof MyObject)
{
MyObject obj = (MyObject) element;
cell.setText(obj.getMessage());
}
}
});
}
private static class MyObject
{
private String first;
private String second;
private String message;
public MyObject(String first, String second,String message)
{
this.first = first;
this.second = second;
this.message = message;
}
public String getFirst()
{
return first;
}
public void setFirst(String first)
{
this.first = first;
}
public String getSecond()
{
return second;
}
public void setSecond(String message)
{
this.second = second;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
so now how can we filter the table. Please help me as I am new to jface table viewer
Use a class derived from ViewerFilter to add a filter:
class MyFilter extends ViewerFilter
{
#Override
public boolean select(Viewer viewer, Object parentElement, Object element)
{
MyObject obj = (MyObject)element;
// TODO return true to include the object, false to exclude
}
}
Add this to the table with:
viewer.addFilter(new MyFilter());
Call
viewer.refresh();
to get the viewer to rerun the filter when the text changes.
I am a new GWTP user and I am not sure how to create a table in GWTP. I know how to make one in GWT.
// Create a CellTable.
CellTable<Contact> table = new CellTable<Contact>();
// Create name column.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact contact) {
return contact.name;
}
};
But This doesn't seem to work in GWTP. Can someone please help me with getting the values on a button press in a GWTP program.
I'm aware you asked this question over a week ago but you may still be stuck on it so here goes. You just have to make sure you put the right bits of logic in the Presenter and View respectively.
It's no different from MVP (Model-View-Presenter) without GWTP in principle:
Your Presenter has the job of getting the data to fill the CellTable, and passing it to the View:
public class TablePresenter extends Presenter<TablePresenter.MyView, TablePresenter.MyProxy>
{
public interface MyView extends View
{
void addData(List<Contact> accounts); // pass data to view
}
// proxy and constructor omitted for brevity...
#Override
protected void onReveal()
{
super.onReveal();
// server action to get contacts
dispatchAsync.execute(new GetContacts(), new AsyncCallback<GetContactsResult>()
{
#Override
public void onSuccess(GetContactsResult result)
{
getView().addData(result.getContacts());
}
});
}
}
Your View has the job of initially setting up the CellTable and its Columns, as well as receiving the data from the Presenter. Here I show a TextColumn and a Column using a ButtonCell:
public class TableView extends View implements TablePresenter.MyView
{
#UiField
CellTable<Contact> table;
// use a dataprovider to hold the data
private ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();
// COLUMNS
TextColumn<Contact> nameColumn;
Column<Contact, String> buttonColumn;
#Inject
public AccountsView(Binder uiBinder)
{
initWidget(uiBinder.createAndBindUi(this));
initTable();
}
private void initTable()
{
nameColumn = new TextColumn<Contact>()
{
#Override
public String getValue(Contact object)
{
return object.name;
}
};
// now add the column to the table
table.addColumn(nameColumn, "Name");
buttonColumn = new Column<Contact, String>(new ButtonCell())
{
// the text of the button
#Override
public String getValue(Contact object)
{
return "Delete " + object.name;
}
};
// set the button action
deleteColumn.setFieldUpdater(new FieldUpdater<Contact, String>()
{
#Override
public void update(int index, Contact object, String value)
{
// whatever you want to do when you click the button
Window.alert("You pressed " + object.name);
}
});
fileTable.addColumn(deleteColumn);
// link dataprovider to the table
dataProvider.addDataDisplay(table);
}
#Override
public void addData(List<Contact> contacts)
{
// clear the dataProvider's list
dataProvider.getList().clear();
// pass the data into the list
dataProvider.setList(contacts);
}
}
Then in your UiBinder:
<g:HTMLPanel>
<b:CellTable ui:field="table" />
</g:HTMLPanel>
I'm having problem with a column in my cell table not being populated with anything. Following is my code:
public class CellTableExample implements EntryPoint {
private static class Contact
{
private String address;
private String name;
private String phone="";
public Contact(String name, String address, String phone) {
super();
this.address = address;
this.name = name;
this.phone=getPhoneNumber(name);
}
}
public String getPhoneNumber(String name)
{
String pNum="";
SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
AsyncCallback<String> callback = new AsyncCallback<String>()
{
#Override
public void onFailure(Throwable caught)
{
}
#Override
public void onSuccess(ArrayList<String[]> result)
{
pNum=reuslt;
}
};
service.findPhoneNum(name,callback);
return pNum;
}
// The list of data to display.
private static List<Contact> CONTACTS = Arrays.asList(
new Contact("John", "123 Fourth Road asdf asdf asdfasdf"),
new Contact("Mary", "222 Lancer Lane")
);
#Override
public void onModuleLoad() {
CellTable<Contact> table = new CellTable<Contact>();
SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
AsyncCallback<ArrayList<String[]>> callback = new AsyncCallback<ArrayList<String[]>>()
{
#Override
public void onFailure(Throwable caught)
{
}
#Override
public void onSuccess(ArrayList<String[]> result)
{
compFlexTable.setPageSize(result.size());
Contact[] contactArr = new Contact[result.size()];
for(int i=0;i<result.size();i++)
{
String [] temp = result.get(i);
String name=temp[0];//name
String address=temp[1];//address
contactArr[i]=new Component1(name,address);
}
COMPONENT=Arrays.asList(compArray);
//address column
TextColumn<Contact> addressColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact contact) {
return contact.address;
}
};
//name column
TextColumn<Contact> nameColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact contact) {
return contact.name;
}
};
//name column
TextColumn<Contact> phoneColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact contact) {
return contact.phone;
}
};
service.getContactInfo(callback);
// Add the columns.
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Address");
table.setRowCount(CONTACTS.size(), true);
table.setRowData(0, CONTACTS);
RootPanel.get().add(table);
}
}
So when I'm creating a contact object, another async method is being called to get the phone number for the contact. The table displays fine but the column for the phone number is empty. How do I resolve this? Any ideas/suggestions will be appreciated.. Thanks so much in advance.
You are misunderstanding the order of execution of your program. In getPhoneNumber, you set pNum to "", start an asynchronous request, and then return pNum. pNum is still "" at the end of this process. Later, when your asynchronous request returns, pNum is set to result, but at that point it's too late - your celltable has already been rendered.
Basically, you can't expect any asynchronous calls to complete in time to render your table accurately, once render has been called. All of the data you need must be immediately available before you render, or it won't show up. You can subclass a DataProvider to do these extra retrievals before asking the CellTable to render itself.
use asynchronous data provider as #Riley said
for more reference use the below link ..
http://www.mytechtip.com/2010/11/gwt-celltable-example-using_8168.html
enter code hereI have a celltable and the columns contains some numbers. I want to add an extra row at the end of the table which will hold the total for each column. Is there any way to do this?
Following is my code:
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);
}
}
Also I would suggest that you use the footer-Argument when you add the column:
addColumn(Column col, Header header)
When you mean totals im not quite sure what you mean but this is similiar to your code but I have added a button that will add the row you howeve can take this out and just add the row.
/**
* 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);
}
}
Hopefully this will help I am not sure what you mean by total though anything can be added to that final field in the Guise of a contact even though it isnt neccessarily one , the better approach would be to use Generics for the data provider but this will achieve same effects with minimal code
public void addNewRow(){
List<Contact> newContactLst = Arrays.asList(new Contact("TEST",
"Sample"));
int numRows = table.getRowCount();
table.setRowCount(numRows+1);
table.setRowData(numRows,newContactLst);
}
I have the following cell table. I've added a button to every row. when it is clicked the new data should be updated.
But when I click update, the old information about the Contact object is being passed to the updateContact method. How Do I get the edited text entered by the user
following is my code:
public class CellTableExample implements EntryPoint {
/**
* A simple data type that represents a contact.
*/
private static class Contact {
private final String address;
private final Date birthday;
private final String name;
public Contact(String name, Date birthday, String address) {
this.name = name;
this.birthday = birthday;
this.address = address;
}
}
/**
* The list of data to display.
*/
#SuppressWarnings("deprecation")
private static final List<Contact> CONTACTS = Arrays.asList(
new Contact("John", new Date(80, 4, 12), "123 Abc Avenue"),
new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln fasfasdfasfdasdfasfasdfasfasdfasfasfasdfasdfasdf"),
new Contact("Tom", new Date(85, 3, 22), "33 Lance Lnasdfasfdasdfffffffffffffffffff"),
new Contact("Jack", new Date(85, 4, 22), "44 Lance Lnsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"));
public void onModuleLoad() {
// Create a CellTable.
final CellTable<Contact> table = new CellTable<Contact>();
// Display 3 rows in one page
table.setPageSize(3);
// Add a text column to show the name.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact object) {
return object.name;
}
};
table.addColumn(nameColumn, "Name");
// 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;
}
};
table.addColumn(dateColumn, "Birthday");
// Add a text column to show the address.
TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact object) {
return object.address;
}
};
table.addColumn(addressColumn, "Address");
ButtonCell updateButton= new ButtonCell();
Column <Contact,String> update= new Column <Contact,String>(updateButton)
{
#Override
public String getValue(Contact c)
{
return "Update";
}
};
update.setFieldUpdater(new FieldUpdater<Contact,String>()
{
#Override
public void update(int index, Contact c,String value)
{
SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
AsyncCallback callback = new AsyncCallback()
{
#Override
public void onFailure(Throwable caught)
{}
#Override
public void onSuccess(Object result)
{
}
};
service.updateContact(c.id, c.name, callback);
}
});
provider.addDataDisplay(table);
provider.updateRowCount(CONTACTS.size(), true);
SimplePager pager = new SimplePager();
pager.setDisplay(table);
VerticalPanel vp = new VerticalPanel();
vp.add(table);
vp.add(pager);
// Add it to the root panel.
RootPanel.get().add(vp);
}
}
Found the solution, all I had to do was the following to each column:
colName.setFieldUpdater(new FieldUpdater<Contact,String>()
{
public void update(int index, Contact c, String value)
{
comp.name=value;
}
});
When you update the value it is updated in the variable but not shown in the table. To show the updated data you have to use:
cellTable.redraw();
The redraw function renders the table with the updated data.