-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
i don't know how to pass a model on button click zul: <window id="win" title="Brand List" width="650px" border="normal" <br=""> apply="com.controller.BrandCotroller">
<grid model="${win$composer.brandModel}" mold="paging"
pageSize="5">
<columns>
<column label="Name" width="500px" align="center" sort="auto(nameBrand)" />
<column label="" align="center" image="/image/icona_rotella.png" />
</columns>
<template name="model" >
<row>
<label value="${each.nameBrand}" />
<button id= "${each.idBrand}" label="Edit" onClick="edit()"/>
</row>
</template>
</grid>
</window>
java controller
public class BrandCotroller extends SelectorComposer<Component> implements Serializable {
private static Logger log = Logger.getLogger(BrandCotroller.class);
private static final long serialVersionUID = 1L;
private ListModel<Brand> brandModel;
@Override
public void doAfterCompose(Component window) throws Exception {
super.doAfterCompose(window);
}
public BrandCotroller() {
try {
Object[] post = RestFulService.get(null, UtilityParameterWebApp.getAllBrand, null, null);
if(post!=null && post.length>1) {
if((Integer)post[0]==HttpURLConnection.HTTP_OK) {
List<Brand> beanEsito = JsonUtility.jsonListToObject((String)post[1], Brand.class);
if(beanEsito!=null) {
brandModel = new ListModelList<Brand>(beanEsito);
}
}
}
}catch(Exception e) {
log.error(e.getMessage(),e);
}
}
public ListModel<Brand> getBrandModel() {
return brandModel;
}
public void edit(Event e ) {
String s= e.toString();
Window window = (Window)Executions.createComponents(
"/view/modifyBrand.zul", null, null);
window.doModal();
}
}
SOLUTION 1:
<grid id="gridBrand" model="${win$composer.brandModel}" mold="paging"
pageSize="5">
<columns>
<column label="Name" width="500px" align="center" sort="auto(nameBrand)" />
<column label="" align="center" image="/image/icona_rotella.png" />
</columns>
<template name="model" >
<row>
<label value="${each.nameBrand}" />
<button id= "${each.idBrand}" label="Edit" forward="gridBrand.onEdit"/>
</row>
</template>
</grid>
controller:
@Listen("onEdit = #gridBrand")
public void onEdit(ForwardEvent e) {
String s= e.toString();
Button b = (Button) e.getOrigin().getTarget();
System.out.println(b.getId());
Window window = (Window)Executions.createComponents(
"/view/modifyBrand.zul", null, null);
window.doModal();
}
Hello there!
For general composer information, you can refer to the documentation here.
Now, in your specific case, I assume that your issue is that you are trying to listen to the click event on "All buttons created by the Grid template", is that correct?
<button id= "${each.idBrand}" label="Edit" onClick="edit()"/>
You have 2 options to approach this:
1 - using forward events:
You can forward the event from the buttons to the containing grid.
See here for the smallest possible sample :D.
In that case, I listen to the grid onRowButtonClick event (forwarded by button) directly in zul, but you can listen to the event in your composer using @Listen(onClick=#myGridId)
2 - Using a row renderer instead of a template You can create the content of the row with a RowRenderer in Java code directly. In that situation, the Grid will call the renderer with the relevant data for each row, and you can build the row by creating components from Java directly. Since you are creating the components in Java directly, you can use comp.addEventListener() to bind a listener to the onClick event directly.
since the question was about passing a model [object] into an event listener I'd also suggest passing the object, or its ID directly into the forward event. This avoids having to reconstruct it from the component's ID (which may e.g. have character restrictions).
<row>
<label value="${each.nameBrand}" />
<button label="Edit" forward="gridBrand.onEdit(${each})"/>
</row>
Then the event listener becomes simpler, and you can just retrieve the model object of interest:
Brand brand = (Brand) e.getData();
or just the id:
<button label="Edit" forward="gridBrand.onEdit(${each.idBrand})"/>
in combination with:
String idBrand = (String) e.getData();
see: section https://www.zkoss.org/wiki/ZK_Developer's_Reference/Event_Handling/Event_Forwarding#Forward_with_Parameters">Forward with Parameters
Asked: 2022-07-27 23:32:07 +0800
Seen: 11 times
Last updated: Aug 24 '22