Create dynamic table with programmatic datasources

Hi.

I’m creating this table, without datasource. The datasource will be created in the controller in init() method.
Now, the problem is why it always throws Exception:
GuiDevelopmentException: Table ‘rows’ element doesn’t have ‘datasource’ attribute

1 Like

Hello,

Here is the answer to a similar question: NOOB - fill table from results of a service - CUBA.Platform

Unfortunately, the table XML loader works in such a way that a datasource for the table should be specified in XML too. This is a shortcoming and probably we will fix it in the future.

For now, you have to create the table programmatically together with the datasource. For example:


Table table = componentsFactory.createComponent(Table.class);
table.setDatasource(ds);
table.setColumnCaption("mechanic", "Mechanic");
table.setColumnCaption("orders", "Orders");

table.setWidth("100%");
table.setHeight("100%");

addComponent(table);
1 Like
  1. Create a new entity (non-persistent), with the fields you want to show

  2. in the XML:
    <data>
    <collection id="tableDc" class="the enity you created">
    <loader id="tableDl">

    <layout>
    <table id="tabTable" dataContainer="tableDc" width="100%">
    <columns>
    <column id="name of the column">

  3. in the java file:(Install delegate for DataLoader)
    @Inject
    private DataManager dataManager;

    @Install(to = “tableDl”, target = Target.DATA_LOADER)

    private List<entity that you created> tableDlLoadDelegate(LoadContext<entity that you created> loadContext){
    //Stream must be of the type of the enitty you created
    Stream<entity that you created> context=dataManager.loadList(loadContext).stream();

    //data is a list or map of entities that you create
    Stream<entity that you created> dataStream=data.stream();

    Stream<entity that you created> dl=Stream.concat(context,dataStream);
    return dl.collect(Collectors.toList());
    }

  4. in onInit(), in the same java field:
    tableDl.load();

And that work for me

2 Likes