Default option in SelectionCell - gwt

When I pass a string list to selectionCell, the first element will be choosen as a default value, how can I change the default value? For example at runtime I want to see the item at index=i as a default value.

There is no way to set an index in that cell
You can use only String there
SelectionCell statusOptionsCell = new SelectionCell(statusType);
Column<InvitedUser, String> statusColumn = new Column<Object, String>(
statusOptionsCell) {
#Override
public String getValue(Object object) {
return object.getStatus(); //value set at runtime
}
};
And if you need array only then
String[] myaaray= {"one","two","three"};
SelectionCell statusOptionsCell = new SelectionCell(statusType);
Column<InvitedUser, String> statusColumn = new Column<Object, String>(
statusOptionsCell) {
#Override
public String getValue(Object object) {
return st[1]; //pass integer as i here at runtime
}
};

Selection Cells are used in Cell widgets. So lets take CellTable as an example. In a Cell Table each row corresponds to one record ie a model/POJO. Each column corresponds to one property in the model/POJO. So, your selection cell column should be bound to one property in the model.
Set that property's value to whatever you want as a default value before supplying that model to the Cell Table.
Once you set the model's property its all Cell table and co's responsibility to set the value in to the selection cell.

I have a real-life example of changing SelectionCell's value at runtime. I want to have a list of possible actions to be performed for each row. After selecting (and performing) an action I want the select to be set back to it's default value.
ArrayList<String> options = new ArrayList<String>();
options.add("Choose...");
options.add("Action 1");
options.add("Action 2");
final SelectionCell cell = new SelectionCell(options);
Column<MyObject, String> column = new Column<MyObject, String>(cell) {
#Override
public String getValue(MyObject object) {
return null;
}
};
column.setFieldUpdater(new FieldUpdater<MyObject, String>() {
#Override
public void update(final int index, MyObject object, String value) {
// perform selected action
// action name is stored in the `value` parameter
// ...
// back to default value
cell.clearViewData(object);
// ... or back to other value
// cell.setViewData(object, "Action 1");
table.redrawRow(index);
}
});
table.addColumn(column, "Test");
Notice that the action is hooked-up on update method so it will be fired only if selected value has changed. It will never be fired with default value ("Choose...").

Related

GWT CellTable: How to Update A TextBox Dynamically

I have the following CellTable
When the user clicks the Pay Min. CheckBox, it should copy the value from the Due Now column over to the Pay Today text field AND recalculate the total for the Pay Today column.
Here is the code for the CheckboxCell (Pay Min.) and the TextInputCell (Pay Today) columns:
private Column<AccountInvoice, Boolean> buildPayMin() {
columnPayMin = new Column<AccountInvoice, Boolean>(new CheckboxCell(true, false)) {
#Override
public Boolean getValue(AccountInvoice object) {
return object.isPayMinimum();
}
#Override
public void onBrowserEvent(Context context, Element elem, AccountInvoice object, NativeEvent event){
// Get event type
int eventType = Event.as(event).getTypeInt();
// See if this is a 'change' event
if (eventType == Event.ONCHANGE) {
String value = columnMinDue.getValue(object);
// Get the cell to copy the value from
TextInputCell cell = (TextInputCell) columnPayToday.getCell();
// Re-create the view data for the cell
TextInputCell.ViewData viewData = new TextInputCell.ViewData(value);
cell.setViewData(object, viewData);
// Refresh
cellTable.redraw();
event.preventDefault();
event.stopPropagation();
}
}
};
columnPayMin.setDataStoreName(columnPayMinHeader);
columnPayMin.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
columnPayMin.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
return columnPayMin;
}
// -----------------------------------------------------------
private Column<AccountInvoice, String> buildPayToday() {
columnPayToday = new Column<AccountInvoice, String>(new TextInputCell()) {
#Override
public String getValue(AccountInvoice object) {
return object.getPaymentAmount();
}
};
columnPayToday.setDataStoreName(columnPayTodayHeader);
columnPayToday.setFieldUpdater(new FieldUpdater<AccountInvoice, String>() {
#Override
public void update(int index, AccountInvoice object, String value) {
object.setPaymentAmount(value);
cellTable.redraw();
}
});
return columnPayToday;
}
I can get the value to copy over, but the total for the Pay Today column doesn't refresh. It seems to refresh only when a value is manually entered into the Pay Today text field. I even tried doing:
columnPayToday.getFieldUpdater().update(context.getIndex(), object, value);
which didn't help either.
Thanks for any help you might provide.
ViewData represents a temporary value in your TextInputCell. When you call cellTable.redraw(), the TextInputCell reverts to the original value (the one from getValue() method for that column).
Do not redraw the table if you want to modify only the "view" state of TextInputCell, or call accountInvoice.setPayToday# (or whatever method is there) to update the object and then call refresh() on your ListDataProvider (which is a more efficient way to update a table compared to redraw).

Create GWT Datagrid with a checkbox column

I have successfully created a datagrid table with two columns. A checkbox col and a string col. When I press a button I want to get the selected strings. Currently when I press the button i get an empty hash set.
Selection Model:
private MultiSelectionModel<String> selectionModel = new MultiSelectionModel<String>(KEY_PROVIDER);
Here is how I create the column
Column<String, Boolean> checkColumn =
new Column<String, Boolean>(new CheckboxCell(true, false)) {
#Override
public Boolean getValue(String object) {
// Get the value from the selection model.
return selectionModel.isSelected(object);
}
};
Here is the method that is called from the button
public Set<String> getSelectedItems(){
Set<String> s = selectionModel.getSelectedSet();
return s;
}
Two pieces are missing. You need to add a FieldUpdater to your checkColumn, and you need to link it to a checkbox manager. Replace T with your Object:
checkColumn.setFieldUpdater(new FieldUpdater<T, Boolean>() {
#Override
public void update(int index, T object, Boolean value) {
getSelectionModel().setSelected(object, value);
dataProvider.refresh();
}
});
setSelectionModel(selectionModel, DefaultSelectionEventManager.<T> createCheckboxManager(0));

How to add a checkboxcell in cellTable, which will enable/disable based on the boolean property in my object, while it is initialising

I want to create a CellTable that has two columns. My first column is a checkbox cell and my second column is a text cell, I want my first column to be populated based on the boolean property in my class.
Example: Employee is the class instance and isPresented is the boolean value. I want to list the details of the employee which will by default check the presented employees and uncheck the absented employees.
Try this:
AbstractCell<Boolean> checkBoxCell = new AbstractCell<Boolean>()
{
#Override
public void render(com.google.gwt.cell.client.Cell.Context context, Boolean value, SafeHtmlBuilder sb)
{
sb.appendHtmlConstant("<input type='check' " + (value ? "checked='1'" : "")+ "'/>");
}};
CellTable<Employee> cellTable = new CellTable<Employee>();
cellTable.addColumn(new Column<Employee, Boolean>(checkBoxCell)
{
#Override
public Boolean getValue(Employee object)
{
return object.isPresented;
}
});
cellTable.addColumn(... // TextColumn

In GWT 2.5, how do I populate multiple parts of a cell based up AbstractCell

I have a class ContactCell based on AbstractCell.
It has two Labels and one Image (defined in GWT 2.5's UiBinder).
How do I Column.addColumn() to add a this custom cell to a CellTable?
And If so, how do I use the method getValue() to populate the fields of ContactCell when getValue() only returns simple values (such as String).
Column<Contact, String> column = new Column<Contact, String>(
new ContactCell()) {
#Override
public String getValue(Contact object) {
return object... CAN ONLY RETURN ONE VALUE. HOW TO POPULATE 2 LABELS & IMAGE?
}
};
You can change the render string by overridding the onrender mathod of the cell as follows.
Assuming 2 labels and a image can be computed from the value returned by getValue mathod.
ContactCell contactCell = new ContactCell()
{
#Override
public void render( com.google.gwt.cell.client.Cell.Context context, SafeHtml value, SafeHtmlBuilder sb )
{
// do value check and compute label1 and label2 and calso compute the image path.
sb.appendHtmlConstant( "<label>LABEL1</label>" +"<label>LABEL2</label>"+"<image></image>" )
}
});
Column<Contact, String> column = new Column<Contact, String>( contactCell )
{
#Override
public String getValue(Contact object)
{
return object... CAN ONLY RETURN ONE VALUE. HOW TO POPULATE 2 LABELS & IMAGE?
}
};
You can either use an IdentityColumn instead of a normal Column (will pass through the entire Contact object) or you use a normal Column like this:
Column<Contact, String[]> column = new Column<Contact, String[]>(
new ContactCell()) {
#Override
public String[] getValue(Contact object) {
String[] retvalue = new String[2];
retvalue[0] = "SOMETHING";
retvalue[1] = "SOME OTHER THING";
return retvalue;
}
};
If the cell based on AbstractCell is defined using UiBinder then it's not currently (GWT 2.5) possible to add such cells to a CellTable.

gwt celltable: Possible to only make some cells in a column editable?

I'm using GWT 2.4. When using a CellTable, I've seen its possible to add a column in which all of the cells are editable ...
final TextInputCell nameCell = new TextInputCell();
Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
#Override
public String getValue(Contact object) {
return object.name;
}
};
table.addColumn(nameColumn, "Name");
but what if I don't want every cell in the column to be editable, only specific ones, based on properties in my "Contact" object? How would I set this up? Thanks, - Dave
The way I would do it is extend the TextInputCell and override the render method to render something else, if you don't want the value in that particular row editable.
Something like this:
public class MyTextInputCell extends TextInputCell {
#Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
YourObject object = getYourObject();
if ( object.isThisCellEditable() ) {
super.render(context,value,sb);
} else {
sb.appendEscaped(value); // our some other HTML. Whatever you want.
}
}
}
In the render method you have access to the cell's context. Context.getIndex() returns the absolute index of the object. I can't remember of the top of my wad right now, but if you do not provide a ProvidesKey implementation when creating your CellTable you will get one that will use the object itself as the key. So you can get the object using Context.getKey().