2015-04-28 117 views
0

我正在使用ZK框架,我想用给定列表的值填充组合框,但我还想包括“选择一个...“选项,因为组合不需要提交值。如何在ZK组合框中添加“选择一个...”选项

我的看法是这样的:

<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> 

在视图模型我检索列表从数据库中创下了一个纪录,我不希望添加一条记录只为这,如果有可能,我还希望避免在列表中添加空项目。

我觉得应该可以做这样的事情:

<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> 

回答

0

您可以修改您的视图模型的getItemList方法,使其会将所需的物品添加到清单中,如下所示:

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

ED IT:另一种选择是使用带有文本“Select One ...”的placeholder以及用于清除组合框中的选定项目的附加按钮(和/或热键)。

+0

谢谢你的回答!这是我为之努力的解决方案,它解决了这个问题,但我并不十分喜欢它(我没有抱怨你的贡献),它增加了控制更新父对象之前选择的值的逻辑,它是否是这个“假”价值。 –

+0

我会尝试在编辑中建议的解决方案,这非常有趣,谢谢! –

相关问题