GWT Celltable with data in map - gwt

I have my datas as a Map<Key,ArrayList<Value>> So i want my col1 to represent my key and the other columns to represent data in my values. And this Value can be or multiple rows. So i was thinking of putting a celltable inside a celltable but then it will affect my headers(since the number of columns for the outer celltable will be just 2) and also the sorting.
And also if i pass the map directly to ListDataProvider it assumes the number of rows is the number of rows of the map but it is not the case.
So what is the best way to do this ?

I'd suggest using a class RowObject like this:
class RowObject {
private final Key key;
private final Value value;
RowObject(Key key, Value value) {
this.key = key;
this.value = value;
}
/* Getters omitted */
}
Then convert the map to a list of RowObjects and feed the list to the data provider:
List<RowObject> rowObjects = new ArrayList<RowObject>();
for (Entry<Key, ArrayList<Value>> entry : map.entrySet()) {
for (Value value : entry.getValue()) {
rowObjects.add(new RowObject(entry.getKey(), value));
}
}
ListDataProvider<RowObject> dataProvider = new ListDataProvider<RowObject>();
dataProvider.addDataDisplay(cellTable);
dataProvider.setList(rowObjects);
Add columns like this:
Column column = new Column<RowObject, String>(new TextCell()) {
#Override
public String getValue(RowObject object) {
return getStringValueFor(object);
}
};
cellTable.addColumn(column);

Related

Hide Column in CellTable

How to hide the columns in Cell table? I need column for grouping purpose but don't want to show in table. Is there any way to hide? Any clue? See the below Code..
TextColumn<ContactInfor> ageTxt = new TextColumn<ContactInfor>() {
#Override
public String getValue(ContactInfor object) {
return object.age;
}
};
cellTable.addColumn(ageTxt, "AGE");
TextColumn<ContactInfor> empIdTxt = new TextColumn<ContactInfor>() {
#Override
public String getValue(ContactInfor object) {
return object.empId;
}
};
cellTable.addColumn(empIdTxt, "EMPLOYEE ID");
TextColumn<ContactInfor> addressTxt = new TextColumn<ContactInfor>() {
#Override
public String getValue(ContactInfor object) {
return object.address;
}
};
cellTable.addColumn(addressTxt, "ADDRESS");
Want to hide addressTxt column from cell table?
Assuming you can't actually remove it from the table, perhaps just set the width to 0:
cellTable.setColumnWidth(addressTxt, "0px");
Javadocs: http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/cellview/client/CellTable.html#setColumnWidth(com.google.gwt.user.cellview.client.Column,java.lang.String)

How to style specific cells in CellTable depending the value of that cell (GWT)?

Ok, i have a CellTable that has 3 columns and 2 rows. I want the text in SOME specific cells (not all cell) in the table to be BOLD.
Please look at this code:
ListDataProvider<List<String>> dataProvider = new ListDataProvider<List<String>>();
dataProvider.addDataDisplay(table);
List<List<String>> list = dataProvider.getList();
List<String> sublist1= Arrays.asList("223","546","698");
List<String> sublist2= Arrays.asList("123","876","898");
List<String> sublist2= Arrays.asList("123","896","438");
IndexedColumn column1=new IndexedColumn(0);
table.addColumn(column1, "Col1");
IndexedColumn column2=new IndexedColumn(1);
table.addColumn(column2, "Col2");
IndexedColumn column3=new IndexedColumn(2);
table.addColumn(column3, "Col3");
Now, I want the Cell that is the intersect of row2 & col3 (ie "898") to be BOLD, so if i do like this
column3.setCellStyleNames(getView().getRes().css().myBoldColor());
Then it will make the whole column BOLD.
So, i think properly we need to loop over each cell in column3 & set the style accordingly, so that we can have the result like this:
Col1 - Col2 - Col3
223 - 546 - 698
123 - 876 - 898
123 - 896 - 438
You can try it be extending AbstractCell also.
Read here about Implementing the render() Method.
Sample code:
static class BoldCell extends AbstractCell<String> {
/**
* The HTML templates used to render the cell.
*/
interface Templates extends SafeHtmlTemplates {
#SafeHtmlTemplates.Template("<div style=\"{0}\">{1}</div>")
SafeHtml cell(SafeStyles styles, SafeHtml value);
}
/**
* Create a singleton instance of the templates used to render the cell.
*/
private static Templates templates = GWT.create(Templates.class);
#Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
/*
* Always do a null check on the value. Cell widgets can pass null to cells if the
* underlying data contains a null, or if the data arrives out of order.
*/
if (value == null) {
return;
}
// If the value comes from the user, we escape it to avoid XSS attacks.
SafeHtml safeValue = SafeHtmlUtils.fromString(value);
// Use the template to create the Cell's html.
FontWeight weight = FontWeight.NORMAL;
if (safeValue.asString().equals("898")) {
weight = FontWeight.BOLD;
}
SafeStyles styles = SafeStylesUtils.forFontWeight(weight);
SafeHtml rendered = templates.cell(styles, safeValue);
sb.append(rendered);
}
}
In above code you can try it with row no also (Bold value for 0th column of 3rd row)
...
FontWeight weight = FontWeight.NORMAL;
if (context.getIndex()==2) {
weight = FontWeight.BOLD;
}
...
Cell<String> cell = new BoldCell();
Column<Contact, String> nameColumn = new Column<Contact, String>(cell) {
#Override
public String getValue(Contact object) {
return object.name;
}
};
table.addColumn(nameColumn, "Name");
Override getCellStyleNames() for a column:
Column<Document, Date> dueColumn = new Column<Document, Date>(new DateCell(DateTimeFormat.getFormat(PredefinedFormat.MONTH_ABBR_DAY))) {
#Override
public Date getValue(Document document) {
return document.getDueDate();
}
#Override
public String getCellStyleNames(Context context, Document document) {
if (document.getDueDate().getTime() < new Date().getTime()) {
return "boldStyle";
}
};

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.

Default option in SelectionCell

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...").

Adding a row-number column to GWT CellTable

I need to insert a new first-column into a CellTable, and display the RowNumber of the current row in it. What is the best way to do this in GWT?
Get the index of the element from the list wrapped by your ListDataProvider. Like this:
final CellTable<Row> table = new CellTable<Row>();
final ListDataProvider<Row> dataProvider = new ListDataProvider<Starter.Row>(getList());
dataProvider.addDataDisplay(table);
TextColumn<Row> numColumn = new TextColumn<Starter.Row>() {
#Override
public String getValue(Row object) {
return Integer.toString(dataProvider.getList().indexOf(object) + 1);
}
};
See here for the rest of the example.
Solution from z00bs is wrong, because row number calculating from object's index in data List. For example, for List of Strings with elements: ["Str1", "Str2", "Str2"], the row numbers will be [1, 2, 2]. It is wrong.
This solution uses the index of row in celltable for row number.
public class RowNumberColumn extends Column {
public RowNumberColumn() {
super(new AbstractCell() {
#Override
public void render(Context context, Object o, SafeHtmlBuilder safeHtmlBuilder) {
safeHtmlBuilder.append(context.getIndex() + 1);
}
});
}
#Override
public String getValue(Object s) {
return null;
}
}
and
cellTable.addColumn(new RowNumberColumn());