0

Button pass model to java

asked 2022-07-27 23:32:07 +0800

lion111 gravatar image lion111
1 1

updated 2022-07-28 16:02:17 +0800

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();
}
delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-07-28 11:20:36 +0800

MDuchemin gravatar image MDuchemin
2390 1 6
ZK Team

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.

link publish delete flag offensive edit

Comments

thank you for your support...I used the first approach and it works

lion111 ( 2022-07-28 15:58:43 +0800 )edit
0

answered 2022-08-24 09:58:39 +0800

cor3000 gravatar image cor3000
6280 2 7

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

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2022-07-27 23:32:07 +0800

Seen: 11 times

Last updated: Aug 24 '22

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More