I have a master detail UI where I have a table column listener b which a new row is inserted when a new value is entered in the particular column. The code looks as follows:
linesDs.addItemPropertyChangeListener(e -> {
if ("quantity".equals(e.getProperty())) {
linesTable.getRowsCount();
ProductionGrLines line = metadata.create(ProductionGrLines.class);
line.setProductionGr(productionGrDs.getItem());
linesDs.addItem(line);
linesDs.refresh();
linesTable.setSelected(line);
//showNotification("none", NotificationType.HUMANIZED);
}
});
I want to improve this functionality but seems like I need to know answer to the following questions:
I want to insert the new row only at the end of the table (currently it is added randomly
I can count the total number of Rows the table has. How can I get if the current row is the last row of the table. I want to insert a new row only when the user is editing a particular column of the last row. The new row should be created just below that.
After a new row is created, the focus is lost, goes to URL or anywhere but i want the cursor moves to the new row created
If you want to insert the new row only at the end of the table please ensure that your lines property has type List, because Set is unordered. Also don’t call refresh on this datasource after adding item.
You can check if item is the last item of a datasource using method getItemIds:
import com.google.common.collect.Iterables;
...
usersDs.addItemPropertyChangeListener(e -> {
if ("login".equals(e.getProperty())
&& Iterables.getLast(usersDs.getItemIds()) == e.getItem().getId()) {
User user = metadata.create(User.class);
usersDs.addItem(user);
usersTable.setSelected(user);
usersTable.requestFocus();
}
});
For now it is not yet possible to move focus to concrete cell of a table, you can only request focus on the table itself:
usersTable.requestFocus();
We are planning to improve focus inside editable tables in the next minor release.