Dynamic Template Selection for Mix Report Based on Input Parameter

I am looking to dynamically select a template for my Jmix Report based on the value of a specific input parameter. We attempted to resolve this with the Jmix AI assistant, but the response didn’t address our need.

Additionally, I would like the template selection to happen automatically without displaying the template combobox in the selection dialog.

Is there a straightforward way to achieve this without having to override the FluentReportRunner?

Thank you in advance for your assistance.

Hi, @asantosu.ext

Unfortunately, there is no simple way to achieve this using standard InputParametersDialog. A combo box to select a template automatically appears in input parameters dialog if the report has multiple templates (see implementation here). There is an ability to override an existing view from the add-on to replace with custom implementation (more details in docs).

If you need an implementation for some specific report which runs on clicking the custom button you can use combination of InputDialog and FluentUiReportRunner.

For example:

@Autowired
private Dialogs dialogs;

@Autowired
private FluentUiReportRunner uiReportRunner;

@Subscribe("runReportButton")
public void onRunReportButtonClick(ClickEvent<Button> event) {
    dialogs.createInputDialog(this)
            .withHeader("Enter values")
            .withParameters(
                     localDateParameter("date").withLabel("Date").withDefaultValue(LocalDate.now()),
                     entityParameter("user", User.class).withLabel("User").withRequired(true)
            )
            .withActions(DialogActions.OK_CANCEL) 
            .withCloseListener(closeEvent -> {
                if (closeEvent.closedWith(DialogOutcome.OK)) { 
                        LocalDate date = closeEvent.getValue("date");
                        User user = closeEvent.getValue("user");

                        String templateCode = getTemplateCode(user);
                        uiReportRunner.byReportCode("user-report")
                                .withParams(Map.of("date", date,
                                        "entity", user))
                                .withTemplateCode(templateCode)
                                .withParametersDialogShowMode(ParametersDialogShowMode.NO)
                                .runAndShow();
                }
            })
            .open();

}

public String getTemplateCode(User user) {
    //logic to get a report template code

}

Regards,
Maria.

Hi @asantosu.ext ,
Look I have, in my case, 2 buttons and this choose the report to be run, for selected item from grid.

    @Subscribe("printBtn75x35")
    public void onPrintBtn75x35(ClickEvent<JmixButton> event) {
        ReportOutputDocument label = reportRunner.byReportCode("Labels")
                .addParam("entity", depositsDataGrid.getSelectedItems().iterator().next())
                .withTemplateCode("750x350_V1")
                .withOutputType(ReportOutputType.PDF)
                .run();
        downloader.download(label.getContent(), label.getDocumentName());

    }

    @Subscribe("printBtn76x50")
    public void onPrintBtn76x50(ClickEvent<JmixButton> event) {
        ReportOutputDocument label = reportRunner.byReportCode("Labels")
                .addParam("entity", depositsDataGrid.getSelectedItems().iterator().next())
                .withTemplateCode("762x508_V5")
                .withOutputType(ReportOutputType.PDF)
                .run();
        downloader.download(label.getContent(), label.getDocumentName());

    }

and in next I have one button for the Excel report:

    // method to get report for Svalbard is run when button excelExportSvalBtn is clicked
    @Subscribe(id = "excelExportSvalBtn", subject = "clickListener")
    public void onExcelExportSvalBtnClick(final ClickEvent<JmixButton> event) {
        // prepare the report in Excel output format by Report code and Template code
        ReportOutputDocument svalbard = reportRunner.byReportCode("RepSval")
                .addParam("duplicate", duplicatesDataGrid.getSelectedItems().iterator().next())
                .withTemplateCode("Sval_V1")
                .withOutputType(ReportOutputType.XLSX)
                .run();
        // download the report in Excel format
        downloader.download(svalbard.getContent(), svalbard.getDocumentName());
    }