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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:07:54  来源:igfitidea点击:

ZK getting selected item from combobox

javacomboboxzk

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

I Love Data Binding

我喜欢数据绑定

what i want to say is that you don't need to get the selection item's value :)

我想说的是,您不需要获取选择项的值:)

Reference

参考

  1. ZK Demo
  2. ZK Essentials#Implementing Data Binding
  1. ZK演示
  2. ZK Essentials#实现数据绑定

回答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 的文本