Vaadin table row change best practice - vaadin

What is the best way to replace a table row in Vaadin (6 and 7)? I use BeanItemContainer. The bean is an entity and has changed (not the ID).
I think this cause unnecessary method invocation and/or object creation:
table.removeItem( item );
table.addItem( item );

As I know, the best pratice is:
BeanItemContainer<DataModel> tableDataSource = new BeanItemContainer<>(DataModel.class);
table.setContainerDataSource(tableDataSource);
When you want to replace a row, just replace the data of this row in tableDataSource:
tableDataSource.removeItem(item);
tableDataSource.addItem(item);
The difference between your code and mine is:
In your code, you replace the row (it means the row is removed from the table and then a new row will be added to table).
In my example, I just replace the data of row.
Hope it helps

Related

UITableView reloadRows only works every other time

I have a custom UITableView ('table') fed by a data array (e.g. 'rowVals[]') which does not display correct values after making a data change.
Error Case
Init table to 5 rows with values fed by 'rowVals' = ["R1" - "R5"]
post - displays correctly
Change rowVals[1] from "R2" to "S2"
post - shows init values (e.g. w/"R2")
Call table.reloadRows() on the table
post - shows new values (e.g. w/"S2")
(BUG!) Call table.reloadRows() on the table
post - shows init values again! (e.g. w/"R2")
Steps #3/#4 now cycle repeatedly!
Question
Why is this occurring, where is the table finding "R2" to display? I have spent over two hours debugging this to no avail, yikes! Thoughts?
Thank you for the suggestion Paulw11, with debugging and persistence I was able to resolve and correct this.
Problem Source
Custom cells, initialized on viewDidLoad() in vc
Problem Solution
Have tableView(cellForRowAtIndexPath) return the cell directly, not by dequeueReusableCell()
I am not sure entirely why this corrected to problem but it does somewhat make sense, dequeueReusableCell() returns a new cell when the 'identifier' is not found!

Eclipse Scout Neon Delete empty row dot trigger save

In one form I have system OK button :
#Order(910.0)
public class SaveButton extends AbstractOkButton {
#Override
protected String getConfiguredLabel() {
return TEXTS.get("Save");
}
}
and I have tableField ...
If I add new empty row in this table execStore is called when pressing save button, but if I delete this empty row and press save button nothing is called, button only close form.
How to solve this? I would like to be able to delete empty row.
EDIT :
I figure it out that all rows where only smart fields are filled in, on delete it doesn't detect change. (not only empty rows...)
Problem is that checkSaveNeeded of the form return that nothing was changed in doOk method.
How do you add your row?
You can mark the row as inserted or not. See: addRow(newRow, markAsInserted); on the Table.
You second question is "How does a Table Field computes if the form needs to be stored or not"?
You can implement you custom logic on with execIsSaveNeeded(). See this question on the Scout forum:
https://www.eclipse.org/forums/index.php?t=msg&th=477037&goto=1042295#msg_1042295
Your last point is: when a row has the status ITableHolder.STATUS_NON_CHANGED in an editable table. When the value in a SmartColumn is changed with a SmartField, the status of the row is not changed to ITableHolder.STATUS_UPDATED.
I just tested it and it works as expected.
For row deletion, it depends on how your table field is configured. What is the value of the property isAutoDiscardOnDelete().
The doc here is still valid with Neon: Table > Delete a row from the table
With isAutoDiscardOnDelete() returning true (this is the case if getConfiguredAutoDiscardOnDelete() returns true), when you remove a row in your table field, it is discarded. There is nothing to save because there is no deleted row in the table field (Everything is explained in the wiki section I have mentioned)
If you want to persit a deletion on the server, you should not use discard the row but just delete it. This way you get the information in the FormData and from there you can work with the row status as described here: TableData (again old doc from our wiki, because it works like this since years. This it not new with Neon).

list to NSIndexPath monotouch

I am trying to add records dynamically to an already populated UITable. In my UITableView class I have a custom List object
List<CustomObject> _data=new List<CustomObject>();
where I add records to from my webservice call like so:
_data.Add(Json(Object));// Json returns a list of CustomObject
//table is a UITableView
//this.table.InsertRows(_data);
I know this above line won't work because its expecting an NSIndexPath[] but I am not sure how to go about adding the new items an load only the new items added. I don't really follow objective C very well so examples I found don't help so much
Generally, you will add data to your List, and then call ReloadData() on your TableView. You usually only call InsertRows() if you want to insert data at a particular spot in the table based on some sort of user action.
You are trying to add a List of CusomObject as CusomObject to a List<CusomObject> ,that is not quite right.you should be adding a CusomObject to List<CusomObject>.

Vaadin - Table column order

Anybody know how to/or it is possible - create a Table with column specific order; configuration order which was before save - example in DB,
and uploaded at specific view on? also I wonder how to take generate this columns headers and content from POJOS class - beans.
Any good ideas?
setVisibleColumns
The Table::setVisibleColumns does double-duty:
Controls which columns are visible, and
Sets the order in which the columns appear.
Call Table::getVisibleColumns to see current ordering.
Doc
This is well described in:
Book of Vaadin > Table
Sampler > User Interface > Data Presentation > Table
Table API JavaDoc
Example Code
Basically, you need the code like this to control columns order and also set list of bean instances as datasource.
Code is not tested, just a demonstration. Valid for Vaadin 6, but I guess no significant changes comparing to Vaadin 7.
table = new Table();
// Wrap your beans collection into vaadin data container. There are many
// types of them , check Book of Vaadin.
BeanItemContainer<Bean> container = new BeanItemContainer<Bean>(Bean.class)
container.addBean(new Bean());
// Set collection of your beans as data source.
// Columns will be created for each property, vaadin uses reflection.
table.setContainerDataSource( container );
// You can select to display only properties you want, not all.
// Order matters. You can get columns list from any source - for example
// store in your DB.
table.setVisibleColumns( new Object[] {"prop1", "prop2"} );
// You can set column headers (by default vaadin will set them same as bean
// properties names). Order matters, should match above order.
table.setColumnHeaders( new String[] {"Property 1", "Property2"} );
The answer by Sergey Makarov is correct. This answer provides further information.
User’s Reordering
You may allow the user to drag-and-drop columns in a table to re-order them at runtime. To enable this feature, call isColumnReorderingAllowed.
You can use a listener to be informed when such a reorder event occurs.
The user’s re-ordering lasts only for this work session. If you want to maintain the user’s order in future work sessions, you must somehow persist their desired order and apply the order when instantiating the table again.
Losing The Order
If you replace the data source of the table, your column order will be reset. You can get the current order before the change, then restore. Example code follows.
// Visible status & ordering.
java.lang.Object[] visibleColumns = this.exampleTable.getVisibleColumns();
// ------| Fresh Data |--------------
// Put fresh data into the Table.
this.exampleTable.setContainerDataSource( freshBeanItemContainer );
// ------| Restore Config |--------------
// Visible status & ordering.
this.exampleTable.setVisibleColumns( visibleColumns ); // Assumes the table has the same properties.
By the way, as with visibility and order, being collapsed will also be reset with fresh data. You may want to track and restore column collapsing as well as ordering.

How to select all row in vaadin table?

HI,
I am having one check box and one table and table has 10 rows .If user selects the check box then all the 10 rows in the vaadin table should need to select but i don't know how to achieve this functionality.Can anyone tell me how to achieve this? If possible provide me some code snippet.
Table.getValue() takes either the ID of a single item or a collection of several item ID's, and Table.getItemIds() returns the ID's of all items in the table. This means that you can select all items in the table simply by:
yourTable.setValue(yourTable.getItemIds());
Note that this will cause performance troubles if there are a lot of items in the table's container. Should work in a simple case like yours, though.
Make sure the table has yourTable.setMultiSelect(true) and then just iterate the ID's got from yourTable.getItemIds() and call yourTable.select(id) for all id's.
This is one way.
In Vaadin 7 when you have table with container data source you can do this:
table.setValue(container.getItemIds());
In Vaadin 6 works this for me:
public void selectAll() {
int size = table.getItemIds().size();
for(int i = 0; i < size; i++) {
table.select(i);
}
table.requestRepaint();
}
And of course in both Vaadin versions don't forget to these lines:
table.setSelectable(true);
table.setMultiSelect(true);
You can simply do it by
Table table = new Table();
table.setValue(table.getItemIds());
It should not cause performance troubles instead you have few hundreds rows. In case you have - bad architecture.
Also you can just iterate through the list (Vaadin does the same)
Here you can find how to reverse selected list using simple iteration.
In two words:
Collection<Object> toSelect = new ArrayList<Object>();
for (Iterator<?> it = simpleTable.getItemIds().iterator(); it.hasNext(); ) {
Object next = it.next();
if (!((Collection<?>) simpleTable.getValue()).contains(next))
toSelect.add(next);
}
simpleTable.setValue(toSelect);

Resources