Search code examples
javabuttongridzk

Execute class method from ZK button in grid


Need help with this.

Post.java is an object that has an User who posted it, a String of info and an Int with the number of likes. PostData.java saves a list of all the posts in an ArrayList. PostViewModel.java communicates with the zul.

The zul:

<zk>
<window apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('fb.PostViewModel')">
<grid height="550px" model="@load(vm.postData.posts)" emptyMessage="Nothing in Posts.">
<template name="model">
<row>
<window border="normal">
<caption label="@load(each.user.getNombre())"/> 
<label value="@load(each.info)" />
<separator bar="true"/>
<button label="Like" onClick="addLike()"/>.
<textbox id="likeTB" disabled="true" value="@load(each.likes)" width="30px;"/>
<zscript>
public void addLike(){
//Somehow add to the object Post's likes one.
//Temporary fix that just modifies the textbox.
//String test = likeTB.getValue();
//Integer num = Integer.parseInt(test);
//num++;
//likeTB.setValue(num+"");
}
</zscript></window></row></template></grid></window>
</zk>

My question is, how can I, using the Like button, go to the object Post where I gave Like and use the method addOneLike()?


Solution

  • I'm going to assume your post has an id or some other way of being unique. I'm also going to assume that your zscript code will be moved to a view model. You can do the following in your zul code:

    <button label="Like" onClick="@command('addLike', id=each.id)"/>
    

    And then in your view model:

    @Command
    public void addLike(@BindingParam("id") long id) {
        // post with id like++
    }