Grails with ZK: How To Create Components in Runtime with The Builder
ZKGrails plugin has an extremely nice feature which make easier than ever to create ZK components in runtime.
First lets see how is the usual way to create components in runtime using Java. This is the zul representation of what we’re trying to achieve:
<div>
<toolbarbutton label="Clickable Item" href="#" />
<toolbarbutton image="edit.png" if="${userHasAccess}" />
<toolbarbutton image="delete.png" if="${userHasAccess}" />
</div>And the java code to do the same:
Div div = new Div();
Toolbarbutton tbb = new Toolbarbutton("Clickable Item");
tbb.setHref("#");
tbb.setParent(div);
if (userHasAccess) {
Toolbarbutton tbbEdit = new Toolbarbutton();
tbbEdit.setImage("edit.png");
tbbEdit.setParent(div);
Toolbarbutton tbbDelete = new Toolbarbutton();
tbbDelete.setImage("delete.png");
tbbDelete.setParent(div);
}As you can easily see the java code is pretty verbose. Using ZKBuilder the code will be easier to understand and smaller, take a look:
Div div = new Div();
div.append {
toolbarbutton(label: "Clickable Item", href: "#")
if (userHasAccess) {
toolbarbutton(image: "edit.png")
toolbarbutton(image: "delete.png")
}
}Which one do your prefer?
If you want more continue reading to see how to use the builder as a template to a list of items.
