How to populate the flex table in GWT from the backend - gwt

I have to pull out values from backend and fill it as rows in the flex table. I am not using static data, so I cannot use:
table.setText(0, 0, "Name");
table.setText(0, 1,"Birthdate");
table.setText(0, 2, "Address");
How can I pull out data and fill it as rows in flex table. Or should I use a Grid table for the same?

What's about something like this. In another class you could load your data from backend and create new Car Objekts and pass them to the MyCarFlexTable class.
class MyCarFlexTable extends FlexTable {
private int rowNumber;
private List<Car> cars;
public MyCarFlexTable(List<Car> cars) {
this.cars = cars;
initTable();
}
private void initTable() {
for(Car c : cars) {
// define which data is printed in which column
setText(rowNumber, 0, c.getId()); // Id of Car
setWidget(rowNumber, 1, new Label(c.getName())); // Name of Car
// add more Columns
rowNumber++;
}
}
}

Related

GWT Celltable with data in map

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);

Custom GWT Tree table with TreeItem and FlexTable

GWT does not contain TreeTable but my project requirement with tree table. so I created it by using Tree and FlexTable.
Here Bean class: Item
class Item
{
ItemType itemType; //Parent,Child Enum type
long parentId;
long amount;
String itemName;
String qty;
String rate;
//Setter / Getter Methods
}
This is custom TreeItem class which has relation between parent Item & Child Item. I have Created one row By using TreeItemNode.
public class TreeItemNode extends TreeItem {
private FlexTable table; //this will use to access child
private FlexTable parentTable; //This will use to access parent of particular child.
//This constructor create parent node
public TreeItemNode(FlexTable parentTable) {
setWidget(parentTable);
this.parentTable=parentTable;
setState(true);
}
//The method used to add Child items in parent
public TreeItem addItem(Widget widget) {
this.table=(FlexTable) widget;
TreeItem ret = new TreeItem(widget);
addItem(ret);
return ret;
}
}
Using Item List, This method is creating the tree which has parent and child node relations.
class CustomTreeTable extends Tree
{
public void createTreeTable() {
//This map store parent id and tree item object for letter fetch the parent node and add child node in it
treeItemMap = new HashMap<Long, BOQTreeItem>();
for (Item item : itemList) {
if (item.getParentItemId() == 0) {
//Create new parent node
TreeItemNode itemTreeItem = new TreeItemNode(addChildItem(item,
new FlexTable(), true),
"gwt-TreeNode");
treeItemMap.put(item.getId(), treeItem);
addItem(itemTreeItem);
} else {
//Add child node
if (treeItemMap.containsKey(item.getParentItemId())
&& treeItemMap.get(item.getParentItemId()) != null) {
FlexTable dataFlextable = treeItemMap.get(
item.getParentItemId()).getTable();
if (dataFlextable == null) {
dataFlextable = new FlexTable();
treeItemMap.get(item.getParentItemId()).setTable(
dataFlextable);
}
FlexTable treeTable = addChildItem(item, dataFlextable,
false);
treeItemMap.get(item.getParentItemId()).addItem(treeTable);
}
}
}
}
}
Using this my table is as below. + is my tree node open/ close like:
ItemName qty rate amount
+ item1 11 1 11 //parent item
---------------------------
- item2 11 2 22
--------------------------------
SubItemName qty rate amount //child item
subItem1 5 2 10
subItem2 8 5 40
------------------------------------
The problem is when I want calculate qty* rate in one raw (child item) than I can do using row index but if I want to do calculate all the child item & show in parent row. then logic become so complex. so I just want to know the logic which I implemented like store the instance in map and managed. is it proper? I am going in right direction or wrong.
Maybe using a DataGrid Widget will be simpler for what you are trying to achieve.
Look at the GWT ShowCase : here
On the DataGrid, you have childs under parent row displaying when you click on ShowFriends link.
Hope it helps. :)

Add filters to LiveGrid?

I am using gxt's LiveGrid. I want to add filters. I have added below few lines but data is not filtered. Am I missing any thing here?
GridFilters filters = new GridFilters();
filters.setLocal(true);
StringFilter nameFilter = new StringFilter("column name");
filters.addFilter(nameFilter);
filters.init(liveGrid);
liveGrid.addPlugin(filters);
From the filter javadoc :
To add a filter to a Grid column, create an instance of a concrete subclass of Filter, passing to the constructor the ValueProvider for the column, then add the filter to a GridFilters
Your code sample seems too restrinct and should probably be parameterized. The StringFilter should be given a ValueProvider for the property of the model object you wish to filter. Following is a simple overview of how to create a Grid with Filters.
Let's say you have a class User
public class User implements Serializable {
private String name;
private Integer id;
// Setters and getters
}
public interface UserProperties extends PropertyAccess<User> {
#Path("id")
ModelKeyProvider<User> key();
ValueProvider<User, String> name();
}
To create a grid that will display your users, you would do as follow
private static final UserProperties props = GWT.create(UserProperties.class);
...
// Create column config
ColumnConfig<User, String> nameCol = new ColumnConfig<User, String>(props.name(), 200, "Name");
// Create column model
List<ColumnConfig<User, ?>> l = new ArrayList<ColumnConfig<User, ?>>();
l.add(nameCol);
ColumnModel<User> cm = new ColumnModel<User>(l);
// Create User store
ListStore<User> store = new ListStore<User>(props.key());
// Create your grid
final LiveGridView<User> liveGridView = new LiveGridView<User>();
liveGridView.setForceFit(true);
Grid<User> view = new Grid<User>(store, cm) {
#Override
protected void onAfterFirstAttach() {
super.onAfterFirstAttach();
// Get grid data
}
};
// Create a String filter for the column
StringFilter<User> nameFilter = new StringFilter<User>(props.name());
// Create a GridFilters
GridFilters<User> filters = new GridFilters<User>();
filters.initPlugin(grid);
filters.setLocal(true);
filters.addFilter(nameFilter);
GridFilters filters = new GridFilters();
filters.setLocal(true);
StringFilter nameFilter = new StringFilter("column name");
filters.addFilter(nameFilter);
filters.init(liveGrid);
liveGrid.addPlugin(filters);
If u want to get the data after filter applied u need to overide reload() method of AbstractGridFilters.

How to Display Wicket DataTable or List View row items for an Object which has a List of Objects as a field

AID BID count1 count2
AID1 1 3 4
2 4 5
3 4 2
AID2 4 6 10
5 2 4
6 4 6
AID3 7 4 5
8 9 4
THis is the table I am trying to display that is for every AID in ObjA display a list of ObjB s.
I want to display a Sortable data using Wicket DataTable or Listview .I have used the SortableData Provider and it works for simple Objects like
ObjA{
int a;
String b;
}
But I have an Object which has List of Objects within it as field as shown below.
ObjA{
Long aId;
List<ObjB> listObjB;
}
#magomi
Thank you for your response.This is the code and the corresponding Html files. I am using DataTables.
I am not sure how to render the items in the listObjBs (bID,count,count2) in the table for every aID.
What is the right way to add the columns for the listObjBs in the ObjA
***columns.add(new PropertyColumn<ObjA>(Model.of("B ID"), "ObjBCounts.bID",
"ObjBCounts.BID"));
columns.add(new PropertyColumn<ObjA>(Model.of(" Count"), "objBCounts.count"));
columns.add(new PropertyColumn<ObjA>(Model.of("Count2”),"objBCounts.count2"));***
How do I sort on the column bID in the iterator method?
Public class HomePage extends Webpage
private static List<ObjB> listObjBs = Arrays.asList(
new ObjB(15L,2,5),
new ObjB(12L,7,10),
new ObjB(13L,3,5),
new ObjB(10L,6,5));
private static List<ObjA> contacts = Arrays.asList(
new ObjA(1L,listObjBs),
new ObjA(2L,listObjBs),
new ObjA(3L,listObjBs),
new ObjA(4L,listObjBs));
public HomePage(final PageParameters parameters) {
List<IColumn<ObjA>> columns = new ArrayList<IColumn<ObjA>>();
columns.add(new PropertyColumn<ObjA>(Model.of("A ID"), "aID",
"aID"));
Public class HomePage extends Webpage
private static List<ObjB> listObjBs = Arrays.asList(
new ObjB(15L,2,5),
new ObjB(12L,7,10),
new ObjB(13L,3,5),
new ObjB(10L,6,5));
private static List<ObjA> contacts = Arrays.asList(
new ObjA(1L,listObjBs),
new ObjA(2L,listObjBs),
new ObjA(3L,listObjBs),
new ObjA(4L,listObjBs));
public HomePage(final PageParameters parameters) {
List<IColumn<ObjA>> columns = new ArrayList<IColumn<ObjA>>();
columns.add(new PropertyColumn<ObjA>(Model.of("A ID"), "aID",
"aID"));
columns.add(new PropertyColumn<ObjA>(Model.of("B ID"), "ObjBCounts.bID",
"ObjBCounts.BID"));
columns.add(new PropertyColumn<ObjA>(Model.of(" Count"), "objBCounts.count"));
columns.add(new PropertyColumn<ObjA>(Model.of("Count2”),"objBCounts.count2"));
add(new DefaultDataTable<ObjA>("contacts", columns,
new ContactsProvider(), 10));
}
rivate static class ContactsProvider extends SortableDataProvider<ObjA> {
public ContactsProvider() {
setSort("aID", SortOrder.ASCENDING);
}
public Iterator<? extends ObjA> iterator(int first, int count) {
List<ObjA> data = new ArrayList<ObjA>(contacts);
Collections.sort(data, new Comparator<ObjA>() {
public int compare(ObjA o1, ObjA o2) {
int dir = getSort().isAscending() ? 1 : -1;
if ("bID".equals(getSort().getProperty())) {
return dir * (o1.listObjB.get(0).getBID().compareTo(o2.listObjB.get(0).getBID()));
} else {
return dir * (o1.getAID().compareTo(o2.getAID()));
}
}
});
return data.subList(first, Math.min(first + count, data.size()))
.iterator();
}
public int size() {
return contacts.size();
}
public IModel<ObjA> model(ObjA object) {
return Model.of(object);
}
}
<html>
<body>
<table border ="1" cellspacing = "1" wicket:id="contacts" class="contacts"></table>
</body>
</html>
add(new DefaultDataTable<ObjA>("contacts", columns,
new ContactsProvider(), 10));
}
rivate static class ContactsProvider extends SortableDataProvider<ObjA> {
public ContactsProvider() {
setSort("aID", SortOrder.ASCENDING);
}
public Iterator<? extends ObjA> iterator(int first, int count) {
List<ObjA> data = new ArrayList<ObjA>(contacts);
Collections.sort(data, new Comparator<ObjA>() {
public int compare(ObjA o1, ObjA o2) {
int dir = getSort().isAscending() ? 1 : -1;
if ("bID".equals(getSort().getProperty())) {
return dir * (o1.listObjB.get(0).getBID().compareTo(o2.listObjB.get(0).getBID()));
} else {
return dir * (o1.getAID().compareTo(o2.getAID()));
}
}
});
return data.subList(first, Math.min(first + count, data.size()))
.iterator();
}
public int size() {
return contacts.size();
}
public IModel<ObjA> model(ObjA object) {
return Model.of(object);
}
}
<html>
<body>
<table border ="1" cellspacing = "1" wicket:id="contacts" class="contacts"></table>
</body>
</html>
ObjB{
Long bId;
int count;
int count2;
}
It want to display data from ObjA which contains a list of objBs in a Sortable table
Could some one please help me with the wicket markup and and how to populate the columns.
Thank you.
I'm not quite sure but it looks like you are mixing ObjA and ObjB into one table in a way that is not possible. Is it right, you want to show a table of ObjA (at least this is indicated by your first column)? Or do you want to show a table of ObjB (as indicated by the second and third column)?
As long as a table is backed by one list you cannot mix another list into this table.
From my sight you have the following options:
You can can have a table of ObjA and inside a table cell you can have a panel with a table of ObjB. But there is no sorting over all rows of ObjB of all ObjA.
You can define some kind of DTO that has a object mixed from ObjA and ObjB. Something like this:
class Data() {
int objAId;
int objBId;
int count1;
int count2;
...
}
You can use a standard repeating view and implement the sorting into your own data provider.

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());