Can't load data in columns added to existing table

Hello,

I have an edit screen with a a Table connected to a collectionDatasource. Y added columns from other table with a ValueCollectionDatasourceImpl using the postInit() according what i found in the forum and it worked. The problem is that the table have a create, edit and remove actions and when i use them the data in the added columns dont refresh.

I tried to use the same aproach used in the postInit() inside the setAfterCommitHandler of the create action of the table but it doesn’t work. how should i do to fix this. Thanks in advance.

@Override
protected void postInit() {
super.postInit();

// This first part works fine;
ValueCollectionDatasourceImpl crds = DsBuilder.create().buildValuesCollectionDatasource();
crds.setQuery(" select distinct p.id, p.nombre from cotpt$ColoresCotizacion e join e.componente p " +
        "join e.combo o join o.cotizacionCombo c where c.id = :custom$cotiz" );
List<String> campos = Arrays.asList("id", "nombre");
crds.addProperty(campos.get(0));
crds.addProperty(campos.get(1));
crds.refresh(ParamsMap.of("cotiz", getItem().getId()));

for (KeyValueEntity entity1 : crds.getItems()) {
    final String nomColumna = entity1.getValue(campos.get(1));
    final UUID idColumna = entity1.getValue(campos.get(0));

    comboCotizacionTable.addGeneratedColumn(nomColumna, entity -> {

        ValueCollectionDatasourceImpl crds2 = DsBuilder.create().buildValuesCollectionDatasource();
        crds2.setQuery(" select l.color from cotpt$ComboCotizacion o join o.coloresCotizacion e join e.componente p join e.color l " +
                "join o.cotizacionCombo c where c.id = :custom$cotiz and p.id = :custom$compo and o.nro = :custom$nrocombo " );
        List<String> campos2 = Arrays.asList("color");
        crds2.addProperty(campos2.get(0));
        crds2.refresh(ParamsMap.of("cotiz", getItem().getId(), "compo", idColumna, "nrocombo", entity.getNro() ));
        //comboCotizacionTable.getSingleSelected().getNro()  getItem().getId()

        for (KeyValueEntity entity2 : crds2.getItems()) {
            return new Table.PlainTextCell(entity2.getValue(campos2.get(0)));
        }
        return new Table.PlainTextCell("");
    });

}

// This second part was added to update the data adding columns 
// if needed after i create new records but doesn't work
comboCotizacionTableCreate.setAfterCommitHandler(entity -> {
    ValueCollectionDatasourceImpl crdsa = DsBuilder.create().buildValuesCollectionDatasource();
    crdsa.setQuery(" select distinct p.id, p.nombre from cotpt$ColoresCotizacion e join e.componente p " +
            "join e.combo o join o.cotizacionCombo c where c.id = :custom$cotiz" );
    List<String> camposa = Arrays.asList("id", "nombre");
    crdsa.addProperty(camposa.get(0));
    crdsa.addProperty(camposa.get(1));
    crdsa.refresh(ParamsMap.of("cotiz", getItem().getId()));

    for (KeyValueEntity entity1a : crdsa.getItems()) {
        final String nomColumna = entity1a.getValue(camposa.get(1));
        final UUID idColumna = entity1a.getValue(camposa.get(0));

        comboCotizacionTable.addGeneratedColumn(nomColumna, e -> {

            ValueCollectionDatasourceImpl crds2a = DsBuilder.create().buildValuesCollectionDatasource();
            crds2a.setQuery(" select l.color from cotpt$ComboCotizacion o join o.coloresCotizacion e join e.componente p join e.color l " +
                    "join o.cotizacionCombo c where c.id = :custom$cotiz and p.id = :custom$compo and o.nro = :custom$nrocombo " );
            List<String> campos2a = Arrays.asList("color");
            crds2a.addProperty(campos2a.get(0));
            crds2a.refresh(ParamsMap.of("cotiz", getItem().getId(), "compo", idColumna, "nrocombo", e.getNro() ));
            //comboCotizacionTable.getSingleSelected().getNro()  getItem().getId()

            for (KeyValueEntity entity2a : crds2a.getItems()) {
                return new Table.PlainTextCell(entity2a.getValue(campos2a.get(0)));
            }
            return new Table.PlainTextCell("");
        });

    }
});
}

Hi,

First of all, you don’t need to use ValueCollectionDatasource just to load data from the database. Use DataManager.loadValues() method. A datasource is required only when you need to bind visual components to the loaded data.

As for the problem you described, we will be able to help you if you provide a small demo project with minimal model and UI.

Hello Konstantin,

Thanks for your reply. I used ValueCollectionDatasource because is in the web tier. i´ll try DataManager. I’m new with it.

I’m attaching a small demo project with the entities needed. I’m attaching also an excel file to explain the demo and what i’m trying to achieve

model and problem testaddcolumn.xlsx (167.4 KB)

Demo Project

Hi Jorge,

Thank you for the test project and explanation. Unfortunately, adding columns to the table when the screen is already opened is impossible. So I would suggest having just a single column and displaying colors in it using the Label component with HTML inside.

The controller’s code becomes straightforward:

public class CotizacionEdit extends AbstractEditor<Cotizacion> {
    @Inject
    private Table<ComboCotizacion> comboCotizacionTable;

    @Inject
    private ComponentsFactory componentsFactory;

    @Override
    public void init(Map<String, Object> params) {
        comboCotizacionTable.addGeneratedColumn("colors", entity -> {
            StringBuilder sb = new StringBuilder();
            for (ColoresCotizacion coloresCotizacion : entity.getColoresCotizacion()) {
                sb.append("<span style='color: #").append(coloresCotizacion.getColor().getColor()).append("'>")
                        .append(coloresCotizacion.getColor().getColor()).append("</span>");
                sb.append(" ");
            }

            Label label = componentsFactory.createComponent(Label.class);
            label.setHtmlEnabled(true);
            label.setValue(sb.toString());
            return label;
        });
    }
}

The whole working project is attached.

testaddcolumn.zip (102.5 KB)

Hello Konstantin,

Thanks for the alternative, but with that way i will have only one color column and i need many. I will need to update those values when i add data, not only in the init.

I watched other suggestions in the forum and i think i’ll try making a custom Value Datasource and add a custom table, so that way i can add as many columns as i need and to add new data i can update the datasource property.

Do you think there could be a problem if i remove and the add a table each time i add new data?

Yes, unlike individual columns, the whole table can be added and removed at any time.

1 Like