Reputation: 11669
I am trying to use Render Snake HTML library to programatically generate HTML for me. I am trying to make HTML table by using Render Snake as shown below - This is just an example of how do we make table using Render Snake library.
html
.table(class_("city-table"))
.tr()
.th().content("City")
.th().content("Country")
._tr()
.tr()
.td().content("Amsterdam")
.td().content("The Netherlands")
._tr()
._table();
I need to do the same thing while iterating my object and make the proper .tr
and close it as well. So this is what confusing me a lot.
Below is the way I would like to have my table in an HTML using RenderSnake library - Here PoolName
, TotalSyncCount
, TotalAsyncCount
, SyncNinetyFivePercentile
and AsyncNinetyFivePercentile
are my column names so I am using th
for them
PoolName TotalSyncCount TotalAsyncCount SyncNinetyFivePercentile AsyncNinetyFivePercentile
Hello 100 100 4 0
World 300 300 2 0
And below is my object which has all these details and which I need to iterate to get the table in the above format
public class PoolMetrics {
private String poolName;
private String totalSyncCount;
private String totalAsyncCount;
private String syncNinetyFivePercentile;
private String asyncNinetyFivePercentile;
// getters and setters
}
And so far I am able to create only the column names using RenderSnake
. I am not sure how to add values inside those column names by iterating PoolMetrics
object -
public static void main(String[] args) throws IOException {
List<PoolMetrics> poolMetricsList = new ArrayList<PoolMetrics>();
// here poolMetricsList has all the information as shown above in the table
HtmlCanvas html = new HtmlCanvas();
html.html().body().table().tr().th().content("PoolName").th().content("TotalSyncCount").th()
.content("TotalAsyncCount").th().content("SyncNinetyFivePercentile").th()
.content("AsyncNinetyFivePercentile")._tr()._table()._body()._html();
// now how do I iterate poolMetricsList to add values inside the column names
// as shown in the above table
}
Problem Statement:-
How do I iterate poolMetricsList
object and add the proper values for those column names using RenderSnake? I would like to generate my HTML as shown above.
Upvotes: 2
Views: 1966
Reputation: 16605
Remember that each method call returns the same instance of HtmlCanvas
(not immutable), so each call you make tr()
, td()
, _html()
etc. is handled internally and you are returned the current instance.
This makes chaining awesome, but also does not require you to chain your calls.
Having said that, here is a complete example:
public class RendersnakeTest {
public static void main(String[] args) throws IOException {
List<PoolMetrics> poolMetricsList = new ArrayList<>();
poolMetricsList.add(new PoolMetrics("A", "0", "0", "0", "0"));
poolMetricsList.add(new PoolMetrics("A", "1", "1", "1", "1"));
poolMetricsList.add(new PoolMetrics("A", "2", "2", "2", "2"));
poolMetricsList.add(new PoolMetrics("A", "3", "3", "3", "3"));
poolMetricsList.add(new PoolMetrics("A", "4", "4", "4", "4"));
HtmlCanvas html = new HtmlCanvas();
html.html().body().table().tr().th().content("PoolName").th().content("TotalSyncCount").th()
.content("TotalAsyncCount").th().content("SyncNinetyFivePercentile").th()
.content("AsyncNinetyFivePercentile")._tr();
// add the rows
for (PoolMetrics pool : poolMetricsList) {
html.tr()
.td(class_("city-table")).content(pool.getPoolName())
.td().content(pool.getTotalAsyncCount())
.td().content(pool.getTotalSyncCount())
.td().content(pool.getSyncNinetyFivePercentile())
.td().content(pool.getAsyncNinetyFivePercentile())
._tr();
}
// close the table
html._table()._body()._html();
// write the file
final String rendered = html.toHtml();
final File output = new File("c:/output.html");
Files.write(output.toPath(), rendered.getBytes("UTF-8"), StandardOpenOption.TRUNCATE_EXISTING);
}
}
class PoolMetrics {
private String poolName;
private String totalSyncCount;
private String totalAsyncCount;
private String syncNinetyFivePercentile;
private String asyncNinetyFivePercentile;
public PoolMetrics(String poolName, String totalSyncCount, String totalAsyncCount, String syncNinetyFivePercentile, String asyncNinetyFivePercentile) {
this.poolName = poolName;
this.totalSyncCount = totalSyncCount;
this.totalAsyncCount = totalAsyncCount;
this.syncNinetyFivePercentile = syncNinetyFivePercentile;
this.asyncNinetyFivePercentile = asyncNinetyFivePercentile;
}
public String getPoolName() {
return poolName;
}
public String getTotalSyncCount() {
return totalSyncCount;
}
public String getTotalAsyncCount() {
return totalAsyncCount;
}
public String getSyncNinetyFivePercentile() {
return syncNinetyFivePercentile;
}
public String getAsyncNinetyFivePercentile() {
return asyncNinetyFivePercentile;
}
}
UPDATE
the class_
method is provided by a static import, it is actually contained within the abstract class HtmlAttributesFactory
, to import these, add the following to your imports list:
import static org.rendersnake.HtmlAttributesFactory.*;
I have updated the example to include this as well.
Upvotes: 2