How to export all columns including invisible ones?

Hi team,

CUBA version: 7.1.4

Want to use Excel Action to export data from a table with some columns set visible="false". But the default behavior only exports those columns show in the UI. So customize the action should do the trick, but I can’t find a method to get all columns including invisible columns, any idea?

Already tried with table.getColumns() it returns the same result as table.getNotCollapsedColumns():

@UiController("forum_Customer.browse")
@UiDescriptor("customer-browse.xml")
@LookupComponent("customersTable")
@LoadDataBeforeShow
public class CustomerBrowse extends StandardLookup<Customer> {

    @Inject
    private GroupTable<Customer> customersTable;

    @Subscribe("customersTable.excel")
    public void onCustomersTableExcel(Action.ActionPerformedEvent event) {
        List<Table.Column> notCollapsedColumns = customersTable.getNotCollapsedColumns();
        // return the same result as above line
        List<Table.Column<Customer>> columns = customersTable.getColumns();
    }

}

Hi, @hanbing.yin

Invisible columns are not loaded from the descriptor, so Table does not contain them.

I can suggest you use a collapsed attribute on columns. Collapsed columns are not fully hidden from the user and can be restored in the Table “settings”:

image

If it suits your requirements, you can use this approach:

@Inject
private GroupTable testsTable;
@Inject
private ExportDisplay exportDisplay;

private ExcelExporter excelExporter = new ExcelExporter();

@Subscribe("testsTable.excel")
public void onTestsTableExcel(Action.ActionPerformedEvent event) {
    excelExporter.exportTable(testsTable, testsTable.getColumns(), exportDisplay);
}

The second way is more complicated. After getting columns from table, we create instance of invisible columns manually:

@Inject
private GroupTable<Test> testsTable;
@Inject
private ExportDisplay exportDisplay;
@Inject
private MessageTools messagesTools;
@Inject
private Metadata metadata;

private ExcelExporter excelExporter = new ExcelExporter();
private List<Table.Column> invisibleColumns = new ArrayList<>();

@Subscribe
public void onInit(InitEvent event) {
    // add printable for missed column to table
    testsTable.addPrintable("name", item -> item.getValue("name"));

    fillInvisibleColumns();
}

@SuppressWarnings("rawtypes")
@Subscribe("testsTable.excel")
public void onTestsTableExcel(Action.ActionPerformedEvent event) {
    List<Table.Column> columns = new ArrayList<>(testsTable.getColumns());
    columns.addAll(invisibleColumns);
    excelExporter.exportTable(testsTable, columns, exportDisplay);
}

private void fillInvisibleColumns() {
    MetaClass metaClass = metadata.getClassNN(Test.class);
    MetaPropertyPath mpp = metaClass.getPropertyPath("name");
    if (mpp != null) {
        Table.Column<Test> column = new Table.Column<>(mpp);
        column.setCaption(messagesTools.getPropertyCaption(mpp.getMetaProperty()));
        invisibleColumns.add(column);
    }
}
1 Like