-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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.
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
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
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 )editBindUtils.postNotifyChange (null, null, this, "searchValue"); after cleaning the searchValue property.
LuigiFabbri ( 2020-09-11 00:04:35 +0800 )editSee how I made the viewmodel I rewrote it in a more readable way
LuigiFabbri ( 2020-09-11 00:05:47 +0800 )editAsked: 2020-09-10 16:51:20 +0800
Seen: 16 times
Last updated: Sep 15 '20