2010-12-08 147 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

相關問題