0

Using databinding in menupopup

asked 2012-09-06 12:47:01 +0800

szarza gravatar image szarza
72 3
http://balteus.blogspot.c...

updated 2013-01-22 09:00:29 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Hello: I have a templated tree with several treechildren depending on node type. Each node has its own context menu. I want disable some options depending on node status, but I can't use databinding (like @load()) in menupopup becouse it is outside of the template (I suppose).

This is what I have

<tree id="tree" width="100%" height="100%" hflex="1"
    zclass="z-dottree" style="border: 0px"
    onSelect="@command('select')"
    model="@load(vm.tree) @template(each.data.type)" vflex="true">
    <template name="JDialerAdmin" var="node">
        <treeitem context="jdialerMenu"
            value="@load(node.data)">
            <treerow>
                <treecell
                    label="@load(node.data.name)" image="/image/kservices.png"
                    tooltiptext="@load(node.data.description)" />
            </treerow>
        </treeitem>
    </template>
    <template name="Service" var="node">
        <treeitem context="serviceMenu"
            value="@load(node.data)">
            <treerow>
                <treecell
                    label="@load(node.data.name)"
                    image="@load(node.data.data.on ? '/image/player_play.png' : '/image/player_stop.png')"
                    tooltiptext="@load(node.data.description)" />
            </treerow>
        </treeitem>
    </template>
    <template name="Campaign" var="node">
        <treeitem context="campaignMenu"
            value="@load(node.data)">
            <treerow>
                <treecell
                    label="@load(node.data.name)" image="/image/kded.png"
                    tooltiptext="@load(node.data.description)" />
            </treerow>
        </treeitem>
    </template>
    <template name="Charge" var="node">
        <treeitem context="chargeMenu"
            value="@load(node.data)">
            <treerow>
                <treecell
                    label="@load(node.data.name)" image="/image/db.png"
                    tooltiptext="@load(node.data.description)" />
            </treerow>
        </treeitem>
    </template>
</tree>
<menupopup id="jdialerMenu">
    <menuitem label="Añadir servicio"
        image="/image/add.png" onClick="@global-command('addService')" />
    <menuseparator />
    <menuitem label="Monitorizar agentes"
        image="/image/users_info.png" />
</menupopup>
<menupopup id="serviceMenu">
    <menuitem label="Añadir campaña"
        image="/image/add.png" onClick="@global-command('addCampaign')" />
    <menuseparator />
    <menuitem label="Iniciar servicio"
        image="/image/player_play.png" />
    <menuitem label="Parar servicio"
        image="/image/player_stop.png" />
    <menuitem label="Eliminar servicio"
        image="/image/cross.png"
        onClick="@global-command('deleteServices')" />
</menupopup>
<menupopup id="campaignMenu">
    <menuitem label="Añadir carga"
        image="/image/add.png" onClick="@global-command('addCharge')" />
    <menuitem label="Eliminar campaña"
        image="/image/cross.png"
        onClick="@global-command('deleteCampaigns')" />
</menupopup>
<menupopup id="chargeMenu">
    <menuitem label="Eliminar carga"
        image="/image/cross.png"
        onClick="@global-command('deleteCharges')" />
</menupopup>

I want to add dinamical behaviour to menupopup using databinding like this: disable="@load(node.data.data.on)", but it doesn't work. i.e.:

<menupopup id="serviceMenu">
    <menuitem label="Añadir campaña"
        image="/image/add.png" onClick="@global-command('addCampaign')" />
    <menuseparator />
    <menuitem label="Iniciar servicio"
        image="/image/player_play.png" disable="@load(node.data.data.on)"/>
    <menuitem label="Parar servicio"
        image="/image/player_stop.png" disable="@load(not node.data.data.on)"/>
    <menuitem label="Eliminar servicio"
        image="/image/cross.png"
        onClick="@global-command('deleteServices')" />
</menupopup>

Anybody knows how? Thanks,

delete flag offensive retag edit

1 Reply

Sort by » oldest newest

answered 2013-02-19 09:44:41 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

in your case, the state of menu will be different in each node and there is no selected context Item concept when using context menu, so I will build menu for each tree node if the tree is not very large, by combine some id trick and $EL.

here is my example

zul

<window apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('simple.TreeMenu')" vflex="true">
    <tree id="tree" width="400px" vflex="true"
        model="@load(vm.model) @template(each.data.type)">
        <template name="type1" var="node">
            <treeitem context="menu_${node.id}" value="@load(node.data)">
                <treerow>
                    <treecell label="@load(node.data.name)">
                        <menupopup id="menu_${node.id}">
                            <menuitem label="Cmd1"
                                onClick="@global-command('cmd1',item=node.data)" />
                            <menuitem label="Cmd2"
                                onClick="@global-command('cmd2',item=node.data)"
                                disabled="@load(node.data.name eq 'node1')" />
                        </menupopup>
                    </treecell>
                </treerow>
            </treeitem>
        </template>
        <template name="type2" var="node">
            <treeitem context="menu_${node.id}" value="@load(node.data)">
                <treerow>
                    <treecell label="@load(node.data.name)">
                        <menupopup id="menu_${node.id}">
                            <menuitem label="Cmd3"
                                onClick="@global-command('cmd3',item=node.data)" />
                            <menuitem label="Cmd4"
                                onClick="@global-command('cmd4',item=node.data)"
                                disabled="@load(node.data.name eq 'node3')" />
                        </menupopup>
                    </treecell>
                </treerow>
            </treeitem>
        </template>
    </tree>
</window>

java

package simple;

import java.util.ArrayList;
import java.util.Collection;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.DefaultTreeModel;
import org.zkoss.zul.DefaultTreeNode;

public class TreeMenu {

    DefaultTreeModel<Item> model;

    Item contextItem;

    int nodeId;

    @init
    public void init() {
        Node<Item> root = new Node<Item>(new Item("root", null), new ArrayList());

        Node<Item> n1 = new Node<Item>(new Item("node1", "type1"), new ArrayList());
        Node<Item> n2 = new Node<Item>(new Item("node2", "type1"), new ArrayList());
        Node<Item> n3 = new Node<Item>(new Item("node3", "type2"));
        Node<Item> n4 = new Node<Item>(new Item("node4", "type2"));

        root.add(n1);
        root.add(n2);
        n1.add(n3);
        n2.add(n4);
        model = new DefaultTreeModel<Item>(root);
    }

    public DefaultTreeModel<Item> getModel() {
        return model;
    }


    @GlobalCommand({"cmd1","cmd2","cmd3","cmd4"})
    public void doCommand(@ContextParam(ContextType.COMMAND_NAME) String cmd, @BindingParam("item") Item item){
        Clients.showNotification("Command "+cmd+" on "+item.getName());
    }

    public Item getContextItem() {
        return contextItem;
    }

    public static class Item {
        String name;
        String type;

        public Item(String name, String type) {
            this.name = name;
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

    public class Node<T> extends DefaultTreeNode<T> {

        int id = nodeId++;

        public Node(T data) {
            super(data);
        }

        public Node(T data, Collection children) {
            super(data, children);
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

    }
}
link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2012-09-06 12:47:01 +0800

Seen: 147 times

Last updated: Feb 19 '13

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