0

Can't catch onSearch event when using SimpleListModel with chosenbox

asked 2015-11-24 07:27:29 +0800

bkstorm gravatar image bkstorm
38 3

Hi everyone. I have a problem. My data is too big, so I can't load all of it to chosebox's model. This is my design, when users type on chosenbox, chosenbox will query database to get data, then set it to model. It works perfectly. There is only one problem, I can't catch onSearch event to add new item to chosenbox. source code is here: http://zkfiddle.org/sample/uao8dq/6-simplelistmodel-with-chosenbox#source-2 I don't know why it does not run on zkfiddle, but it work on my computer.

It seem like SimpleListModel can't be changed, therefore we can't catch onSearch event to add new item. Do you have any idea for this problem? Thanks :D

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-11-24 10:35:10 +0800

Darksu gravatar image Darksu
1991 1 4

Hello bkstorm,

First of all please find below a modified version of your code that runs on zk fiddle:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zk.ui.event.KeyEvent;
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.zkmax.zul.Chosenbox;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.SimpleListModel;

public class TestComposer extends SelectorComposer<Component> {

    /**
     * 
     */
    private static final long serialVersionUID = -5582437516748319706L;

    @Wire
    private Chosenbox cbx;
    @Wire
    private Chosenbox cbx2;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);

        cbx.setModel(new SimpleListModel(Collections.emptyList()) {
            @Override
            public ListModel getSubModel(Object value, int nRows) {
                return createModel(value.toString());
            }
        });

    }

    private ListModel<?> createModel(String value) {
        List<String> data = new ArrayList<String>();

              if (value.equals("aa")) {
                    data.add("aab");
                    data.add("aa-bb");
              } else if (value.equals("bb")) {
                    data.add("bbcd");
                    data.add("bb-bb");
              } else if (value.equals("cc")) {
                    data.add("ccc");
                    data.add("cc-bb");
              } else if (value.equals("ddd")) {
                    data.add("dddd");
                    data.add("ddee");
              }

        return new SimpleListModel(data);
        // return new ListModelList<>(data);
    }


    @Listen("onSearch = #cbx")
    public void onSearchCbx(InputEvent event) {
        System.out.println(event.getValue());
    }

    @Listen("onOK = #cbx")
    public void onOKCbx(KeyEvent event) {

    }
}

Furthermore you should use onSearching instead of on Search in order to accomplish your task.

http://books.zkoss.org/wiki/ZKComponentReference/Input/Chosenbox#Supported_Events

Best Regards,

Darksu

link publish delete flag offensive edit

Comments

Sorry, I didn't explain clearly, if data returned from server is empty, I need to add a new item when users press ENTER or separator, so I think onSearch is what I looking for, but I can't handle this event, this is my problem.

bkstorm ( 2015-11-24 14:22:27 +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
2 followers

RSS

Stats

Asked: 2015-11-24 07:27:29 +0800

Seen: 17 times

Last updated: Nov 24 '15

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