Table displays no data went sent as a parameter

This table displays perfectly on the screen that it is being sent from, but I’m not getting any data to display when I send the table as a parameter to another window.

like this


Map<String, Object> params = new HashMap();
       
        params.put("equipmentAddonsTable", equipmentAddonsTable);
              Window window = openWindow("edge$EquipmentAddons.browse", WindowManager.OpenType.DIALOG, params);

Here’s the code for the browse screen

package com.tab.edge.web.collateral.equipmentaddons;

import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.web.gui.components.WebTable;
import com.tab.edge.entity.collateral.EquipmentAddons;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
import java.util.Map;

/**
 * @author narmstrong
 */
public class EquipmentAddonsBrowse extends AbstractLookup {

    @Inject
    private CollectionDatasource equipmentAddonsDs;
    @Named("equipmentAddonsTable")
    private Table equipmentAddonsTable;


    @Inject
    private Metadata metadata;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        equipmentAddonsTable = (Table<EquipmentAddons>) params.get("equipmentAddonsTable");
        equipmentAddonsDs = equipmentAddonsTable.getDatasource();
     
		equipmentAddonsTable = new WebTable<>();
        equipmentAddonsTable.setDatasource(equipmentAddonsDs);
        equipmentAddonsDs.refresh();
	 
    }


    public void saveEquipmentAddon() {
        if (validateAll()) {
            getDsContext().commit();
            showNotification(getMessage("changesSaved"), NotificationType.HUMANIZED);
            equipmentAddonsDs.refresh();
        }
    }

    @Override
    public boolean validateAll() {
        return super.validateAll();
    }
}

and

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://browseCaption"
        class="com.tab.edge.web.collateral.equipmentaddons.EquipmentAddonsBrowse"
        focusComponent="equipmentAddonsTable"
        lookupComponent="equipmentAddonsTable"
        messagesPack="com.tab.edge.web.collateral.equipmentaddons">
    <dsContext>
        <datasource id="equipmentDs"
                    class="com.tab.edge.entity.collateral.Equipment"
                    view="equipment-view">
            <collectionDatasource id="equipmentAddonsDs"
                                  property="equipmentAddons"/>
        </datasource>
    </dsContext>
    <dialogMode height="605"
                width="800"/>
    <layout spacing="true">
        <table id="equipmentAddonsTable"
               editable="true"
               height="504px"
               width="100%">
            <actions>
                <action id="save"
                        caption="mainMsg://actions.Save"
                        invoke="saveEquipmentAddon"/>
            </actions>
            <columns>
                <column id="active"
                        align="CENTER"
                        editable="true"
                        width="70px"/>
                <column id="retailAmount"
                        width="80px"/>
                <column id="name"/>
            </columns>
            <rows datasource="equipmentAddonsDs"/>
        </table>
        <buttonsPanel id="buttonsPanel"
                      align="BOTTOM_LEFT"
                      alwaysVisible="true">
            <button id="saveBtn"
                    action="equipmentAddonsTable.save"/>
        </buttonsPanel>
    </layout>
</window>

What am I missing?

empty table

Hi,

According to your code, I see that the reason you pass the table as a parameter to another window is obtaining the datasource from this table, in other words, to get the data. If so, please clarify your goals and I will guide you to the right approach with minimum effort.

For now, I can say that assigning a component object to the injected variable has no effect. If you want to replace a component, you need to remove it from a container and add the new one (with the same id).

Also, you call equipmentAddonsDs.refresh(). The refresh() method reloads data from the database. Since equipmentAddonsDs is a nested datasource it has nothing to load from database, and it obtains data from the parent entity. In your case there is no data in the parent entity.

Regards,
Gleb

Ok. Now I’m passing in the Datasource instead. How do I change the table’s dataSource?

I assume that you need data from the data source. In this case, you can iterate throw all the items in the passed data source and add them the to target data source, for example:

for (OrderLine line : dsPassedAsAWindowParameter.getItems()) {
   linesDs.addItem(line);
}

I don’t recommend to replace the whole data source with the new one because according to your code the target data source is a nested data source. That means it has additional settings to be related to the master data source.

It’s not a nested datasource any more. Using the code above, I can add to the datasource, but can’t remove things in it first. The table ends up displaying 1000 items plus the new items.

public class EquipmentAddonsBrowse extends AbstractLookup { 
    /*@Inject 
    private CollectionDatasource<EquipmentAddons, UUID> equipmentAddonsDs;*/ 
    @Inject 
    private CollectionDatasource<EquipmentAddons, UUID> equipmentAddonsDs; 
    @Inject 
    private Metadata metadata; 
    @Named("equipmentAddonsTable") 
    private Table equipmentAddonsTable; 
    @Override 
    public void init(Map<String, Object> params) { 
        super.init(params); 
        //equipmentAddonsDs.clear(); 
        CollectionDatasource<EquipmentAddons, UUID> collectionDatasourceFromParams = (CollectionDatasource<EquipmentAddons, UUID>) params.get("equipmentAddonDS"); 
        equipmentAddonsTable = new WebTable<>(); 
        //equipmentAddonsDs.clear(); 
        //equipmentAddonsDs = collectionDatasourceFromParams; 
        //collectionDatasourceFromParams.refresh(); 
        for (EquipmentAddons equipmentAddons : equipmentAddonsDs.getItems()) { 
            equipmentAddonsDs.removeItem(equipmentAddons); 
        } 
        for (EquipmentAddons equipmentAddons : collectionDatasourceFromParams.getItems()) { 
            equipmentAddonsDs.addItem(equipmentAddons); 
        } 
        equipmentAddonsTable.setDatasource(equipmentAddonsDs); 
        System.out.println(); 
    } 
    public void saveEquipmentAddon() { 
        if (validateAll()) { 
            getDsContext().commit(); 
            showNotification(getMessage("changesSaved"), NotificationType.HUMANIZED); 
            equipmentAddonsDs.refresh(); 
        } 
    } 
    @Override 
    public boolean validateAll() { 
        return super.validateAll(); 
    } 
} 

now the xml looks like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<window xmlns="[url=http://schemas.haulmont.com/cuba/window.xsd"]http://schemas.haulmont.com/cuba/window.xsd"[/url]; 
        caption="msg://browseCaption" 
        class="com.tab.edge.web.collateral.equipmentaddons.EquipmentAddonsBrowse" 
        focusComponent="equipmentAddonsTable" 
        lookupComponent="equipmentAddonsTable" 
        messagesPack="com.tab.edge.web.collateral.equipmentaddons"> 
    <dsContext> 
        <!--<datasource id="equipmentDs" 
                    class="com.tab.edge.entity.collateral.Equipment" 
                    view="equipment-view"> 
            <collectionDatasource id="equipmentAddonsDs" 
                                  property="equipmentAddons"/> 
        </datasource>--> 
        <collectionDatasource id="equipmentAddonsDs" 
                    class="com.tab.edge.entity.collateral.EquipmentAddons" 
                    view="equipmentAddons-view"/> 
    </dsContext> 
    <dialogMode height="605" 
                width="800"/> 
    <layout spacing="true"> 
        <table id="equipmentAddonsTable" 
               editable="true" 
               height="504px" 
               width="100%"> 
            <actions> 
                <action id="save" 
                        caption="mainMsg://actions.Save" 
                        invoke="saveEquipmentAddon"/> 
            </actions> 
            <columns> 
                <column id="active" 
                        align="CENTER" 
                        editable="true" 
                        width="70px"/> 
                <column id="retailAmount" 
                        width="80px"/> 
                <column id="name"/> 
            </columns> 
            <rows datasource="equipmentAddonsDs"/> 
            <rowsCount/> 
        </table> 
        <buttonsPanel id="buttonsPanel" 
                      align="BOTTOM_LEFT" 
                      alwaysVisible="true"> 
            <button id="saveBtn" 
                    action="equipmentAddonsTable.save"/> 
        </buttonsPanel> 
    </layout> 
</window>

You can remove all items by invoking com.haulmont.cuba.gui.data.CollectionDatasource#clear.