Multiple fragments on same page

Hi, is it possible to dynamically add a fragment to a screen multiple times and have each fragment display a different data set? I have been able to add the fragment to the screen dynamically but can only get the fragments to display 1 address.

@Subscribe
private void onInit(InitEvent event) {
    addressesDl.load();
    for (Address add : addressDc.getItems()) {
        AddressFragment addressFragment = fragments.create(this, AddressFragment.class);
        addressFragment.init();
        addresses_split.add(addressFragment.getFragment());
    }
}

Example:
A customer can have multiple addresses and rather than displaying the addresses in a tabular format (as in the table of the screenshot) I want to display each address in a formatted block (like at the bottom of the screenshot).

image

Thanks
Keith

Hi,

I would recommend defining a method that sets an item to the fragment instance container, e.g.

@UiController("sales_OrderLineFragment")
@UiDescriptor("order-line-fragment.xml")
public class OrderLineFragment extends ScreenFragment {
    @Inject
    private InstanceContainer<OrderLine> orderLineDc;

    public void setOrderLine(OrderLine orderLine) {
        orderLineDc.setItem(orderLine);
    }
}

So, in this case, you can use a fragment as follows:

private void drawFragments() {
    for (OrderLine line : linesDc.getItems()) {
        OrderLineFragment lineFragment = fragments.create(this, OrderLineFragment.class);
        lineFragment.setOrderLine(line);    // Set a specific item
        lineFragment.init();

        linesBox.add(lineFragment.getFragment());
    }
}

Regards,
Gleb

Gleb, After implementing your suggestion I also had to change the fragment xml to have provider=“false” on the data collection once that was done everything worked as required.

Thanks for you help
Keith