2010-12-08 108 views
1

我想在组合框中获取选定的值,但它作为ComboItem返回。如何获取值作为字符串?ZK从组合框中获取选定的项目

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

在这里,我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(); 

    } 

回答

3

我解决它像

searchCombo.getSelectedItem().getValue().toString(); 
2

ZK与组合框数据绑定是非常强大的,

我创建了一个样本同步选择数据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

我想说的是,你并不需要得到选择项的值:)

参考

  1. ZK Demo
  2. ZK Essentials#Implementing Data Binding
1

检索。hCombo.getSelectedItem()的getValue() - >获得选定的ComboItem

searchCombo.getSelectedItem()getLabel()的值 - >的选择得到文本的ComboItem