0

Combobox with "not selected" value

asked 2018-04-13 23:20:55 +0800

pavelEW gravatar image pavelEW
1 1

Hi guys!

I have a model, pseudocode

public ListModel<City> getCityModel() { 
    return cityModel; 
}

I need an ability to add "not selected" label to view which will have a null value. On zul page I have something like this

<combobox readonly="true" model="@load(vm.cityModel)" selectedItem="@bind(fx.city)">
    <template name="model" var="c">
        <comboitem value="@load(c.id)" label="@load(c.name)"/>
    </template>
</combobox>

I used the same approach like here forum.zkoss.org/question/97747/how-to-add-an-empty-value-to-a-combobox/ (sorry, can't post a link) but I need not an empty label, but "not selected" or smth. I added my own comboitem with this code:

@Override
public void setLabel(String label) {
    if (label == null) {
        ComboboxWithEmptyValue cbx = (ComboboxWithEmptyValue) getParent();
        if (cbx.getAddEmpty())
            label = "not selected";
    }
    super.setLabel(label);
}

The problem that I get not selected label at the bottom of dropdown menu, and I need this on top. I tried to add null value to model to the first and to last position, but that changed nothing. Any ideas?

Thanks in advance for help!

delete flag offensive retag edit

Comments

Sort your listmodel so that empty value is first

chillworld ( 2018-04-15 12:39:54 +0800 )edit

Hi chillworld! Thanks for your reply, but as I already wrote I tried sorting and nothing changes if null item is at first or at last position in ListModelList - the label not selected always at the bottom. If I don't use the label and leave just an empty combiotem, then it works.

pavelEW ( 2018-04-16 18:23:00 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-04-16 20:12:41 +0800

chillworld gravatar image chillworld flag of Belgium
5257 3 9
https://github.com/chillw...

Maybe there is something wrong with your code, try this :

@Override
public void setLabel(String label) {
    if (label == null) {
        label = "not selected";
    }
    super.setLabel(label);
}
link publish delete flag offensive edit
0

answered 2018-04-16 22:41:07 +0800

pavelEW gravatar image pavelEW
1 1

updated 2018-04-16 22:41:57 +0800

@Override public void setLabel(String label) { if (label == null) { label = "not selected"; } super.setLabel(label); }

Tried the same code, doesn't work as I need

link publish delete flag offensive edit

Comments

Make a fiddle, I'll make it work

chillworld ( 2018-04-17 03:14:35 +0800 )edit

Okay, man! I made a fiddle here http://zkfiddle.org/sample/3t9eh17/6-Combobox-with-not-selected-label and it works... But still this thing doesn't work with my project. Anyway big thanks for your help, I'll try to make it work myself.

pavelEW ( 2018-04-19 17:53:06 +0800 )edit

Maybe the sorting not correct, log the order after sort

chillworld ( 2018-04-21 14:31:47 +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
1 follower

RSS

Stats

Asked: 2018-04-13 23:20:55 +0800

Seen: 11 times

Last updated: Apr 16

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