-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello everyone,
I'm trying to make a simple tree like the example in ZkDemo : "Getting Started > Tree"
Everything is going fine from here until I tried to add a button at the end of each line in the resultGrid line.
I just try to get the click event on the "add cart button" but no matter what I do, in the @Command method "addToCart", "article" is always null !...
My zul :
<window apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('nc.noumea.mairie.appock.viewmodel.ListeCatalogueServiceViewModel')"
border="none" hflex="1"
vflex="1" contentStyle="overflow:auto">
<vlayout vflex="1">
<hbox hflex="1" vflex="1">
<tree id="catalogueServiceTree" model="@bind(vm.catalogueTree)"
itemRenderer="@bind(vm.catalogueTreeitemRenderer)" width="400px" vflex="1">
<treecols>
<treecol label="Libellé"/>
<treecol label="Nombre" align="center" width="80px"/>
</treecols>
</tree>
<listbox id="resultCatalogueServiceTreeGrid" width="100%" vflex="1">
<listhead menupopup="auto" sizable="true">
<listheader label="Photo" width="100px" align="center"/>
<listheader label="Référence" sort="auto(reference)" width="150px"/>
<listheader label="Libellé" sort="auto(libelle)" hflex="1"/>
<listheader label="Colisage" sort="auto(quantiteColisage)" width="80px"/>
<listheader label="" width="55px" align="center"/>
</listhead>
<template name="model">
<listitem>
<listcell>
<image src="${each.photoSrc}"/>
</listcell>
<listcell>
<label value="${each.reference}"/>
</listcell>
<listcell>
<label value="${each.libelle}"/>
</listcell>
<listcell>
<label value="${each.libelleColisage}"/>
</listcell>
<listcell>
<button iconSclass="z-icon-cart-plus" onClick="@command('addToCart', article=each)"/>
</listcell>
</listitem>
</template>
</listbox>
</hbox>
</vlayout>
</window>
And my View :
@VariableResolver(DelegatingVariableResolver.class)
public class ListeCatalogueServiceViewModel extends AbstractListeViewModel<Catalogue> implements Serializable {
private static final long serialVersionUID = 1L;
@WireVariable
CatalogueService catalogueService;
private TreeModel<CatalogueTreeNode> catalogueTree;
private TreeitemRenderer<CatalogueTreeNode> catalogueTreeitemRenderer;
@Wire("#resultCatalogueServiceTreeGrid")
private Listbox resultGrid;
@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
}
@Init(superclass = true)
public void initTree() throws IOException {
Catalogue catalogue = catalogueService.findActif();
if (catalogue == null) {
Messagebox.show("Il n'existe pas de catalogue actif à afficher", "Erreur", Messagebox.OK, Messagebox.ERROR);
return;
}
initCatalogueTreeModel(catalogue);
}
public TreeModel<CatalogueTreeNode> getCatalogueTree() {
return catalogueTree;
}
public void setCatalogueTree(TreeModel<CatalogueTreeNode> catalogueTree) {
this.catalogueTree = catalogueTree;
}
private void initCatalogueTreeModel(Catalogue catalogue) throws IOException {
setCatalogueTree(new CatalogueTreeModel(getCatalogueTreeRoot(catalogue)));
}
private CatalogueTreeNode getCatalogueTreeRoot(Catalogue catalogue) throws IOException {
return catalogueService.getCatalogueTreeRoot(catalogue);
}
public TreeitemRenderer<CatalogueTreeNode> getCatalogueTreeitemRenderer() {
if (catalogueTreeitemRenderer == null) {
catalogueTreeitemRenderer = new TreeitemRenderer<CatalogueTreeNode>() {
@Override
public void render(Treeitem treeitem, CatalogueTreeNode catalogueTreeNode, int i) throws Exception {
Treerow dataRow = new Treerow();
dataRow.setParent(treeitem);
treeitem.setValue(catalogueTreeNode);
treeitem.setOpen(true);
Treecell treecellLabel = new Treecell(catalogueTreeNode.getLibelle());
dataRow.appendChild(treecellLabel);
dataRow.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
@Override
public void onEvent(Event event) throws Exception {
CatalogueTreeNode selectedNodeValue = ((Treeitem) event.getTarget().getParent()).getValue();
if (selectedNodeValue.getTypeCatalogueNode() == TypeCatalogueNode.FAMILLE) {
resultGrid.setModel(new ListModelList<>(((SousFamille) selectedNodeValue.getEntity()).getListeArticle()));
}
}
});
Treecell treecellNombreArticle = new Treecell(String.valueOf(catalogueTreeNode.getNombreArticle()));
dataRow.appendChild(treecellNombreArticle);
}
};
}
return catalogueTreeitemRenderer;
}
public void setCatalogueTreeitemRenderer(TreeitemRenderer<CatalogueTreeNode> catalogueTreeitemRenderer) {
this.catalogueTreeitemRenderer = catalogueTreeitemRenderer;
}
@Command
public void addToCart(@BindingParam("article") Article article) {
if (article == null) {
return;
}
}
}
I'm not new to ZK and I usually find what is wrong but here I'm stuck for hours looking on forum and so...:(
Thank you for your help !
out of my head I'd say you are mixing the mvvm and mvc patterns. When using mvc some mvvm mechanisms don't initialize and I assume that's why the each
variable resolves to null
when used in a dynamic command binding inside a listitem generated without any mvvm bindings.
Usually there should be a model="@init(vm.somemodel)"
to initialize the model binding which I am missing here (I see you are setting this manually in your code... in the MVC way, not wrong - just not doing the extra bits).
If you can't switch to pure MVVM you can store the each
variable in a custom-attribute at listitem scope and use this during the command binding.
<template name="model">
<listitem>
<custom-attribute myarticle="${each}"/>
...
<listcell>
<button iconSclass="z-icon-cart-plus"
onClick="@command('addToCart', article=myarticle)"/>
</listcell>
</listitem>
</template>
As said just out-of-my-head ... if this doesn't help, please provide a running example on zkfiddle.
Asked: 2017-03-22 04:12:23 +0800
Seen: 38 times
Last updated: Mar 27 '17
ZK8 Features for MVC - Shadow Elements
The imageCaption of css is not work when open first time on Firefox
zk chart with JFreeChartEngine
Sync grid column visible state in client/server side when show/hide column through menupopup
A scrollbar error occurs when using the Listbox List Group
Render On Demand ROD how to catch event onRender from listitem
as much as I"d like to help the problem is that your example is not runnable without the dependent classes. Can you please simplify the problem and post a runnable example on zkfiddle. I can then have a look and give advice more quickly. Robert
cor3000 ( 2017-03-27 02:29:20 +0800 )edit