ZK:自定义组件,Listbox不绑定SelectedItem

时间:2018-03-11 00:10:53

标签: binding listbox annotations custom-controls zk

我正在创建一个新的自定义组件,并且无法将控制器的属性绑定到我的自定义组件的SelectedItem注释属性。我的想法是,任何通过我的Custom组件SelectedItem注释的人,我应该能够在我的组件中检索它并将其自己分配给ListBox的SelectedItem属性。这将为我的组件的用户提供灵活性,使他们不必担心内部组件,并且该组件可以重复使用。

问题是我无法在自定义组件中获取/设置Controller值。我得到NULL。有人可以帮我解决这个问题或指向正确的方向吗?这是代码:

<bandbox id="filterDropdownBandBox" instant="true" readonly="false">
<bandpopup id="filterDropdownBandPopup" style="max-height:250px;overflow-x:hidden">
    <listbox id="listBox" hflex="1" rows="0" >
         <template name="model">
             <listitem>
                 <listcell label="${each.label}" />
             </listitem>
         </template>
     </listbox>
</bandpopup>

public class FilterDropdown extends Div implements IdSpace {

@Wire
private Listbox listBox;
@Wire
private Bandpopup filterDropdownBandPopup;
@Wire
private Bandbox filterDropdownBandBox;

private ListModelList<GenericNameValuePair> lbModel;

public FilterDropdown() {
    Executions.createComponents("/filterDropdown.zul", this, null);
    Selectors.wireComponents(this, this, false);
    Selectors.wireEventListeners(this, this);
}
public void setSelectedItem(Listitem l) // getting NULL here
    {
        l.setParent(listBox);
        listBox.setSelectedItem(l);
    }
 public void saveSelection() {
     listBox.getSelectedItem();
 }

 public Listitem getSelectedItem() {
     return listBox.getSelectedItem();
 }
}

这是我将此组件添加到lang-addon.xml文件

的方法
<component>
    <component-name>filter-dropdown</component-name>
    <extends>div</extends>
    <component-class>com.components.FilterDropdown</component-class>
    <annotation>
        <annotation-name>DDBIND</annotation-name>
        <property-name>selectedItem</property-name>
        <attribute>
            <attribute-name>ACCESS</attribute-name>
            <attribute-value>both</attribute-value>
        </attribute>
    </annotation>
</component>

这就是我在其他ZUL文件中使用自定义组件的方式

 <filter-dropdown id="filterProjDropdown" selectedItem="@DDBIND{XYZCtrl.bean.propbean.actualProp}"/>

1 个答案:

答案 0 :(得分:2)

首先,保持正常的注释,如@load()@save()@bind()`。

现在,我的第一个建议是把你的zul扔掉 在组件中实现AfterCompose接口,并使用渲染器添加所有项目 它使任何人都可以更轻松地更改该组件,并且可以实现更多性能。

其次,在班级中使用正确的注释:

@ComponentAnnotation({"selectedItem:@ZKBIND(ACCESS=both,SAVE_EVENT=onSelect)"}) 

像这样你的lang-addon.xml应该是这样的:

<component> 
     <component-name>filter-dropdown</component-name> 
     <extends>div</extends> 
     <component-class>com.components.FilterDropdown</component-class> 
</component> 

最后一次:

您需要通知活页夹selectedItems发生了变化:

Events.postEvent("onSelect", FilterDropdown.this, selectedItems);

您应该在附加到bandbox的事件监听器中处理此问题。

如果您需要高级工作组件代码,包括如何将其导出到单独的jar,请查看mine github project