ZK如何停止标签的OnClick事件?

时间:2013-01-17 04:55:07

标签: events onclick tags zk

我正在创建动态tabs。我创建了1st标签然后2nd标签等。当我在3rd标签时,我希望用户无法选择其他标签,换句话说,用户将无法从一个标签移动到其他标签。

我正在使用event.propagation();来停止活动,但它无效。有没有人有其他解决方案?

1 个答案:

答案 0 :(得分:4)

试试这个:

ZUL

<zk>
    <div apply="test.TestComposer">
        <tabbox>
            <tabs id="tbs" />
            <tabpanels id="tps" />
        </tabbox>
        <button id="btn" label="add tab" />
    </div>
</zk>

TestComposer.java

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabpanel;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Tabs;

/**
 * Tested with ZK 6.0.2 and ZK Pivottable 2.0.0
 *
 */
@SuppressWarnings("rawtypes")
public class TestComposer extends SelectorComposer {
    @Wire
    Button btn;
    @Wire
    Tabs tbs;
    @Wire
    Tabpanels tps;
    List tabs = new ArrayList();
    int limit = 3;

    @Listen("onClick = #btn")
    public void onClick$btn () {
        if (tabs.size() < limit) {
            Tab tab = new Tab("Tab " + (tabs.size()+1));
            tab.setParent(tbs);
            tabs.add(tab);
            Tabpanel tp = new Tabpanel();
            new Label("Tabpanel " + tabs.size()).setParent(tp);
            tp.setParent(tps);

            if (tabs.size() == limit) {
                tab.setSelected(true);
                overrideBehavior();
            }
        }
    }
    public void overrideBehavior () {
        final Tab toSel = (Tab)tabs.get(tabs.size()-1);
        for (int i = 0; i < tabs.size() - 1; i++) {
            final Tab tab = (Tab)tabs.get(i);
            tab.addEventListener( // add an EventListener
                    "onClick", // listen to onClick event
                    new EventListener () { // create an EventListener instance to add
                        public void onEvent (Event event) { // the method will be called while onClick event triggered
                            Clients.evalJavaScript("zk.log('"+tab+"  clicked')");
                            toSel.setSelected(true); // make sure the third one is selected at server side
                        }
                    }
                );
                tab.setWidgetOverride("_sel", "function(notify, init) {return;}"); // prevent select at client side
        }
    }
}