2011-07-12 46 views
2

我無法通過包含的.zul頁面中的id訪問組件。我有一個main.zul與控制器,我需要通過java控制器類在包含的zul頁面中獲取組件,但它返回null。ZK如何通過ID到達包含.zul頁面組件?

我知道包含的方法創建新的ID空間,但有什麼辦法得到這個組件?

UPDATE

這裏是我的代碼:

主ZUL頁面

<?page title="DealerVizard.zul"?> 

<?page id="main" ?> 

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?> 
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./Dealer" ?> 
<zk> 
    <style src="/resources/css/default.css" /> 
    <window id="Dealer" class="index" 
     apply="com.i2i.prm.controller.IndexController"> 


     <div class="content" width="100%"> 

      <tabbox id="tb" forward="onSelect=onSelect"> 
       <tabs id="tabs"> 
        <tab id="info" label="INFO" /> 
        <tab id="create" label="CREATE" /> 
        <tab id="edit" label="EDIT" /> 
        <tab id="test" label="TEST PANEL(LIST BOX)" /> 

       </tabs> 
       <tabpanels> 
        <tabpanel id="DealerInfo"> 
         <include id="DealerInfoContent" 
          src="View/Dealer/DealerInfo.zul" /> 
        </tabpanel> 
        <tabpanel id="DealerCreate"> 
         <include id="DealerCreateContent" 
          src="View/Dealer/DealerCreate.zul" /> 
        </tabpanel> 
        <tabpanel id="DealerEdit"> 
         <include id="DealerEditContent" 
          src="View/Dealer/DealerEdit.zul" /> 
        </tabpanel> 

        <tabpanel id="PagingListBox"> 
         <include id="PagingListBoxContent" // Included here 
          src="View/TEST/PagingListBox.zul" /> 
        </tabpanel> 
       </tabpanels> 
      </tabbox> 
     </div> 
    </window> 

</zk> 

PagingListBox.zul(包括頁)

<?page id="list" ?> 

<zk> 

    <grid width="100%"> 

     <columns> 
      <column label="" /> 

     </columns> 
     <rows> 
      <row> 
       <listbox id="listModel" width="100%" height="100%" 
        visible="true" span="true" pagingPosition="top" rows="20" 
        selectedItem="@{DealerController.selected}" 
        model="@{DealerController.userList}" 
        forward="onSelect=//main/Dealer.onSelect"> 
        <auxhead> 
         <auxheader colspan="1"> 
          <textbox 
           value="@{DealerController.searchUser.name}" maxlength="9" 
           id="searchCO_ID" forward="onChanging=//main/Dealer.onSearch" 
           width="100%"> 
          </textbox> 
         </auxheader> 
         <auxheader colspan="1"> 
          <textbox 
           value="@{DealerController.searchUser.surname}" maxlength="21" 
           id="searchMSISDN" forward="onChanging=//main/Dealer.onSearch" 
           width="100%"> 
          </textbox> 
         </auxheader> 
         <auxheader colspan="1"> 

         </auxheader> 

        </auxhead> 

        <listhead> 
         <listheader label="Name" 
          sort="auto(UPPER(name))" /> 

         <listheader label="Surname" 
          sort="auto(UPPER(surname))" /> 


         <listheader label="Delete ?" /> 
        </listhead> 


        <listitem self="@{each=USERLIST}"> 

         <listcell> 
          <label value="@{USERLIST.user.name}" /> 
          <textbox 
           value="@{DealerController.tmpUser.name}" visible="false" /> 
         </listcell> 
         <listcell> 
          <label value="@{USERLIST.user.surname}" /> 
          <textbox 
           value="@{DealerController.tmpUser.surname}" visible="false" /> 
         </listcell> 

         <listcell> 
          <button label="Update" 
           forward="onClick=//main/Dealer.onUpdate" visible="false" /> 
          <button image="icons/edit-delete.png" 
           label="Delete" forward="onClick=//main/Dealer.onDelete" 
           width="100%" disabled="true" /> 
          <button label="Save" 
           forward="onClick=//main/Dealer.onSave" visible="false" /> 
          <button label="Cancel" 
           forward="onClick=//main/Dealer.onCancel" visible="false" /> 
         </listcell> 
        </listitem> 
       </listbox> 
       <paging id="pagingData" pageSize="20" /> 
      </row> 

     </rows> 
    </grid> 
</zk> 

IndexCOntroller.java

public class IndexController extends GenericForwardComposer { 

    private List<User> userList = new ArrayList<User>() ; 
    AnnotateDataBinder binder; 
    Tabbox tb; 
    Window Dealer; 
    private int pageCount=0; 

    @Override 
    public void doAfterCompose(Component comp) throws Exception { 
     // TODO Auto-generated method stub 
     super.doAfterCompose(comp); 
     comp.setVariable(comp.getId() + "Controller", this, true); 
     binder = (AnnotateDataBinder) Dealer.getVariable("binder", true); 


    System.out.println(Path.getComponent("//list/listModel")); 
    } 


    public IndexController() { 
     // TODO Auto-generated constructor stub 
    } 
} 

回答

6

通常我不會建議使用Path.getComponent()的方式來訪問其他組件的應用程序代碼變得與您的組件結構視圖頁面緊密耦合。 在你的情況下,你最簡單的方法是使用AbstractComponent#getFellow(String compId)方法,例如。

Include inc = (Include) Dealer.getFellow("PagingListBoxContent"); 
Listbox listModel = (Listbox) inc.getFellow("listModel"); 
System.out.println(listModel); 

所以在未來,即使您在您的列表框之前插入任何其他組件,您的代碼仍然可以工作。

UPDATE:BTW有上ZK博客這個題目有趣blogpost最近

0

您可以使用zscript或java訪問任何其他id空間中的任何組件。如果是在同一頁上,但不同的窗口,然後(在窗口A組分B):

Path.getComponent("/A/B"); 

,如果它是一個不同的頁上,然後(窗口A組分B上頁P):

Path.getComponent("//P/A/B"); 

您可以找到的文檔在這裏:http://books.zkoss.org/wiki/ZK%20Developer%27s%20Reference/UI%20Composing/ID%20Space

+0

我trind了它,但它只適用於belogns控制器類的視圖頁面。使用\t \t \t System.out.println(Path.getComponent(「// includedPageID/listboxID」));返回null。包含的頁面沒有窗口。只有div標籤和列表框。 – Ercan

+0

@Meko:你的代碼對我來說很合適,我做了一個小模型來測試它(用ZK 3.6.4)。 // list/listModel應該解析包含的ZUL中的列表框。是否有可能在多個ZUL文件中使用了頁面標識「list」,因此它不是唯一的? –

1

如果您包括有ID,您可以用美元符號來獲得內部組件

<zk> 
    <include id="inc" src="test.zul /> 

    </zk> 

測試.zul

<zk> 
    <label id="lab1" value="test1" /> 
    </zk> 

您可以使用test.zul

「INC $ lab1中的」 獲取標籤
0

您可以添加您的IndexController.java:

... 
    private Include DealerInfoContent; 
... 

這種方式可以在父作曲家中訪問包含的組件。

(雖然我建議使用camelCase ID)。

相關問題