Hello all,
I tried to search for an answer quite a bit before posting, as i know there is nothing more frusturating than answering the same thing over and over, but I couldn’t find anything quite the same.
I have a sql container backed table which I am using to allow entering new rows into a database table, and edit existing rows. New rows I want to allow users to change any data, existing rows I want them to be able to edit only some of them.
What I have done kinda works… It’s good until you get to ordering columns, or sorting. Once you do either of those, things go back to
not
read only.
Is there a better way for me to do this??
private TableQuery tq;
private SQLContainer sq;
private Table table;
tq = new TableQuery("pricing", DatabaseFactory.getInstance().getVaadinPool());
sq = null;
try {
sq = new SQLContainer(tq);
} catch (SQLException e) {
e.printStackTrace();
}
setReadOnly(sq, "pricing_cd");
table = new Table();
table.setContainerDataSource(sq);
table.setImmediate(true);
table.setEditable(true);
table.setBuffered(true);
table.setColumnReorderingAllowed(false);
table.addHeaderClickListener(new Table.HeaderClickListener() {
@Override
public void headerClick(HeaderClickEvent event) {
// TODO Auto-generated method stub
setReadOnly(sq, "pricing_cd"); //this does not work
table.refreshRowCache();
}
});
save = new Button("Save",
new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
try {
sq.commit();
setReadOnly(sq, "pricing_cd"); //this works
table.refreshRowCache();
Notification.show("Pricing saved.");
} catch (Exception e) {
// TODO Auto-generated catch block
Notification.show("Pricing could not be saved.\n Both Pricing_Description and Pricing_CD must have values, and Pricing_CD must be unique.", Type.ERROR_MESSAGE);
for(Object item : table.getItemIds())
{
if(item.toString().equals("Temporary row id") &&
(table.getItem(item).getItemProperty("pricing_description").getValue() == null ||
table.getItem(item).getItemProperty("pricing_cd").getValue() == null)) //remove uncommitted rows from the table if they haven't entered any data
{
table.removeItem(item);
}
}
}
}
});
save.setImmediate(true);
save.setWidth("65px");
save.setHeight("25px");
private void setReadOnly(Container container, String propertyID)
{
for(Object item : container.getItemIds())
{
container.getContainerProperty(item, propertyID).setReadOnly(true);
}
}