java ZK 从组合框中获取所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4385605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ZK getting selected item from combobox
提问by Ercan
I am trying to get selected value in combobox but it returns as a ComboItem.How can I get value as string?
我试图在组合框中获取选定的值,但它以 ComboItem 形式返回。如何以字符串形式获取值?
<zscript>
<![CDATA[
String[] months = { "Ada", "Basic", "C", "C++", "Cobol", "Forth",
"Fortran", "Go", "Groovy", "Haskell", "Java", "JavaScript", "Lisp",
"Python", "Ruby", "Scala", "Scheme" };
ListModel lmonths = new SimpleListModel(months);
]]></zscript>
<combobox id="searchCombo"
forward="onChange=onSearch" model="@{months}" >
<!--
<comboitem self="@{each='months'}"
label="@{months}" value="@{months}">
</comboitem>
-->
</combobox>
And here my onSearch method
这里是我的 onSearch 方法
public void onSearch(ForwardEvent event) {
System.out.println(searchCombo.getSelectedItem());
prodevt.search(searchCombo.getSelectedItem().toString());
filterCbox.setChecked(true);
AnnotateDataBinder binder = (AnnotateDataBinder) win.getVariable(
"binder", true);
binder.loadAll();
}
回答by Ercan
I solved it like
我解决了它
searchCombo.getSelectedItem().getValue().toString();
回答by Ryan Wu
ZK's databinding with combobox is very powerful,
ZK与combobox的数据绑定非常强大,
i created a sample to sync select data from comboboxes and listbox
我创建了一个示例来同步组合框和列表框中的选择数据
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<zk>
<zscript>
<![CDATA[
String[] langs = { "ZK" ,"Ada", "Basic", "C", "C++", "Cobol", "Forth",
"Fortran", "Go", "Groovy", "Haskell", "Java",
"JavaScript","Lisp", "Python", "Ruby", "Scala",
"Scheme"
};
//(Optional) Default Select ZK
String things_i_have_selected = langs[0];
]]></zscript>
<hlayout>
<combobox model="@{langs}" selectedItem="@{things_i_have_selected}" />
<combobox model="@{langs}" selectedItem="@{things_i_have_selected}" />
<listbox model="@{langs}" selectedItem="@{things_i_have_selected}"
rows="5" width="400px">
<listitem self="@{each=String}">
<listcell label="@{String}"></listcell>
</listitem>
</listbox>
</hlayout>
</zk>
what i want to say is that you don't need to get the selection item's value :)
我想说的是,您不需要获取选择项的值:)
Reference
参考
回答by John Eduard Lopez Lopez
searchCombo.getSelectedItem().getValue() --> get value of selected ComboItem
searchCombo.getSelectedItem().getValue() --> 获取所选组合项的值
searchCombo.getSelectedItem().getLabel() --> get text of selected ComboItem
searchCombo.getSelectedItem().getLabel() --> 获取所选 ComboItem 的文本