0

button onClick update value then execute @command

asked 2020-09-10 16:51:20 +0800

tinawang gravatar image tinawang
157 2

updated 2020-09-11 08:48:54 +0800

I have a button refresh, after it clicked, the searchTextbox need to be cleared and also trigger a method in my view model.

clear the searchTextbox.

<textbox id="searchTextbox"/>
<button label="search" onClick="@command('search', keyword=searchText.value)"/>
<button label="refresh" onClick="searchTextbox.value = null"/>

refresh command in defined in view model.

<textbox id="searchTextbox"/>
<button label="search" onClick="@command('search', keyword=searchText.value)"/>
<button label="refresh" onClick="@command('refresh')"/>

If there a way that combine both behavior together only set in zul file?
onClick="searchTextbox.value = null; @command('refresh')"
I hope not to wired Textbox in view model, because I can use @BindingParam get the value.

-- Updated --
Is it possible combine both behavior together only set in zul file? I knew that view model can do it.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-09-15 10:54:46 +0800

cor3000 gravatar image cor3000
6280 2 7

you can use a condition in a load binding to load a constant (e.g. an empty string) after a command

<zk>
  <div viewModel="@id('vm') @init('pkg$.MyViewModel')">
    <!-- loads an empty string as the textbox value after the refresh command -->
    <textbox id="searchText" value="@load('', after='refresh')"/>
    <button label="search" onClick="@command('search', keyword=searchText.value)"/>
    <button label="refresh" onClick="@command('refresh')"/>
  </div>
</zk>

here a runnable zkfiddle

link publish delete flag offensive edit

Comments

Thank you very much.

tinawang ( 2020-09-15 15:35:57 +0800 )edit
0

answered 2020-09-10 23:39:31 +0800

LuigiFabbri gravatar image LuigiFabbri
46 5

updated 2020-09-15 10:43:24 +0800

cor3000 gravatar image cor3000
6280 2 7

When using the MVVM approach you do not take the values from the view objects but you have to use the binding technique where the object values are copied into the viewmodel property. In your case you need to do as follows:

ZUL:

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="new page title" border="normal" viewModel="@id('vm')     @init('myviewmodel')">
<textbox value="@bind(vm.searchValue)" />
<button label = "search" onClick ="@command('search')" />
<button label = "refresh" onClick ="@command('clear')" />
</window>
</zk>

VIEWMODEL:

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
public class myviewmodel {
    private String searchValue="";

    public String getSearchValue() {
        return searchValue;
    }
    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }
    @Command 
    public void search()
    {
        System.out.println("searchValue="+this.searchValue);
    }
    @Command 
    public void clear()
    {
        this.searchValue="";
        BindUtils.postNotifyChange(null, null, this, "searchValue");
    }
}

Regards

link publish delete flag offensive edit

Comments

Thank you for reply, but is it possible that I do not need to add any parameter in my view model for the solution?

tinawang ( 2020-09-10 23:58:14 +0800 )edit

In the viewmodel you have to put the searchValue property to be linked through the bind annotation to the textbox object of the view, also in the viewmodel you have to put the two command of search and clear, to clean up the text just run the command BindUtils.postNotifyChange .....

LuigiFabbri ( 2020-09-11 00:04:06 +0800 )edit

BindUtils.postNotifyChange (null, null, this, "searchValue"); after cleaning the searchValue property.

LuigiFabbri ( 2020-09-11 00:04:35 +0800 )edit

See how I made the viewmodel I rewrote it in a more readable way

LuigiFabbri ( 2020-09-11 00:05:47 +0800 )edit

However you can copy and run my example it works

LuigiFabbri ( 2020-09-11 00:07:07 +0800 )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
1 follower

RSS

Stats

Asked: 2020-09-10 16:51:20 +0800

Seen: 16 times

Last updated: Sep 15 '20

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