I have created a custom object to call the function “BuildReport(Document document, object dataSource)”, this object contains an object of type DataTable.
To iterate the DataTable in the .docx template I’m doing:
<<foreach [ag in data.getTable().getDataSet().getTables()]>>
<<[ag.description]>> <<[ag.value]>> <</foreach>>.
But I am getting an exception saying that I cannot iterate an object of type DataTable.
What path should I follow to iterate an object of type DataTable as I have built my custom object?
Thank you so much!
@MikeRodriguez can you please attach your template document and the object class that you are using to populate the template.
@MikeRodriguez It looks like you need to iterate throw rows in the data.getTable()
, right? But in your template you are iterating data tables in the parent data set of the table. Yoou should modify your template like this:
<<foreach [ag in data.getTable()]>>
<<[ag.description]>> <<[ag.value]>> <</foreach>>.
I have created a simple example (note, for demonstration purposes, I pass DataTable
directly as a data source):
DataTable dt = new DataTable();
dt.getColumns().add("description");
dt.getColumns().add("value");
dt.getRows().add("First description", "First value");
dt.getRows().add("Second description", "Second value");
dt.getRows().add("Third description", "Third value");
Document doc = new Document("C:\\Temp\\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, dt, "data");
doc.save("C:\\Temp\\out.docx");
Here are template and output:
in.docx (12.4 KB)
out.docx (9.9 KB)
Sorry, I made a mistake when writing how to access the Table, I did many tests and ended up writing one of the tests like crazy.
I attach the docx as I have it.
As you can see, I am embedding other data that is seen correctly, but when accessing the table, the error that I mentioned appears.
template.docx (16.0 KB)
CustomObject has inside a list, of type Foo, which in turn has inside a DataTable, that’s why I have the nested loop.
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, customObject, "f");
Thank you very much for your time
@MikeRodriguez
DataTable
and alike classes mimicking ADO.NET in Java are treated specially by LINQ Reporting Engine making it possible to use simplified syntax for them. Although, it is not mentioned in documentation, embedding of these objects into objects of custom types is not supported. There are some technical reasons behind this decision, so we are not going to change it. Put simply, behavior that you face is by design.
That said, you can use an ADO.NET mimicking object and a custom object as data sources at the same time (since the engine can accept multiple data sources for building a single report), but you need to separate them before passing.
For example, you may have a single DataTable
instance containing rows corresponding to all dato
instances. To form a relation, you then may use a field, for example, datoId
in the both. If you set the name of the DataTable
instance to table
while passing to the engine, then it is possible to use syntax like table.where(r => r.datoId == dato.datoId)
instead of dato.getTable()
to get expected results.
Ok, thank you very much for the clarification.
I understand that
Greetings
1 Like