如何在 ZK 组合框中添加 "Select one..." 选项

How to add a "Select one..." option in ZK combobox

我正在使用 ZK 框架,我想用给定列表的值填充一个组合框,但我还想包含一个 "Select One..." 选项,因为组合框不需要有提交值。

我的观点是这样的:

<row>
    <hbox>
        Items:
    </hbox>
    <combobox width="100%" readonly="true" model="@load(vm.itemList)" selectedItem="@bind(vm.object.item)" >
        <template name="model">
            <comboitem label="@load(each.description)" />
        </template>
    </combobox> 
</row>

在 ViewModel 中,我从数据库中检索了包含记录集的列表,我不想为此添加记录,如果可能的话,我还想避免在列表中添加空项。

我认为应该可以做这样的事情:

<combobox width="100%" readonly="true" model="@load(vm.itemList)" selectedItem="@bind(vm.object.item)" >
    <template name="model">
        <comboitem label="@load(each.description)" />
    </template>
    <comboitem label="Select One..."/>
</combobox> 

但这两个都不起作用:

<combobox width="100%" readonly="true" model="@load(vm.itemList)" selectedItem="@bind(vm.object.item)" >
    <template name="model">
        <comboitem label="@load(each.description)" />
        <comboitem label="Select One..."/>
    </template>
</combobox> 

您可以修改视图模型的 getItemList 方法,以便将所需的项目添加到列表中,如下所示:

public List getItemList() {
    List itemList = getItemsFromDatabase();
    YourItemClass item = new YourItemClass();
    item.setDescription("Select One...");
    itemList.add(item);
    return itemList;
}

编辑:另一种选择是使用带有文本 "Select One..." 的 placeholder 和一个附加按钮(and/or 热键)来清除组合框中的选定项目。