我发布了用于解决此问题的代码。感谢Per和Eric McCormick以及Paul Withers。
<xp:scriptBlock id="scriptBlock2">
<xp:this.value><![CDATA[$(document).ready(
function() {
x$("#{javascript:return getComponent(compositeData.fieldName).getClientId(facesContext);}").select2({
placeholder : "Choose an employee",
allowClear: true,
minimumResultsForSearch : 3
})
}
);
]]>
</xp:this.value>
</xp:scriptBlock>
我previous question的回答非常有用。我正在制作一个Select2自定义控件,需要在SSJS函数中使用动态生成的ID
我通过给它fieldName来动态创建自定义控件中字段的id,如下所示:
id="${javascript:compositeData.fieldName}"
在我的CC的其他部分,我使用该计算来访问id号,例如:
<xp:message
id="message1"
for="#{javascript:compositeData.fieldName}"
styleClass="help-block">
</xp:message>
然而,在构建我的Select2 CC时,我需要在脚本中添加一些SSJS,如下所示:
<xp:scriptBlock id="scriptBlock2">
<xp:this.value><![CDATA[
$(document).ready(
function() {
x$("#{id:[compositeData.fieldName]}").select2({
placeholder: "Select An Employee",
allowClear: true
});
}
);
]]></xp:this.value>
</xp:scriptBlock>
但这不起作用。我无法弄清楚如何动态生成ID。
x$("#{id:[compositeData.fieldName]}")
<?xml version="1.0" encoding="UTF-8"?>
<xp:view
xmlns:xp="http://www.ibm.com/xsp/core"
id="view1">
<xp:scriptBlock id="scriptBlock2">
<xp:this.value><![CDATA[
$(document).ready(
function() {
x$("#{id:[compositeData.fieldName]}").select2({
placeholder: "Select An Employee",
allowClear: true
});
}
);
]]></xp:this.value>
</xp:scriptBlock>
<xp:div>
<xp:this.styleClass><![CDATA[#{javascript:"form-group" + (getComponent(compositeData.fieldName).isValid() ? "" : " has-error")}]]></xp:this.styleClass>
<xp:label
styleClass="control-label"
for="#{javascript:compositeData.fieldLabel}"
value="${compositeData.fieldLabel}" />
<div class="">
<xp:comboBox
id="${javascript:compositeData.fieldName}"
value="#{compositeData.dataSource[compositeData.fieldName]}"
required="${compositeData.required}">
<xp:selectItems
value="${javascript:'#{CacheBean.'+compositeData.cacheItem+'}'}">
</xp:selectItems>
<xp:this.validators>
<xp:validateRequired message="#{javascript:compositeData.fieldLabel + ' is required'}"></xp:validateRequired>
</xp:this.validators>
</xp:comboBox>
<xp:scriptBlock
id="scriptBlock1">
<xp:this.value>
<![CDATA[x$("#{id:comboBox5}").select2({minimumResultsForSearch:5});]]>
</xp:this.value>
</xp:scriptBlock>
<xp:text
escape="true"
id="computedField1"
styleClass="help-block"
value="${compositeData.helpText}">
<xp:this.rendered><![CDATA[#{javascript:(getComponent(compositeData.fieldName).isValid()) && compositeData.helpText != null}]]></xp:this.rendered>
</xp:text>
<xp:message
id="message1"
for="#{javascript:compositeData.fieldName}"
styleClass="help-block">
</xp:message>
</div>
</xp:div>
<xp:text escape="true" id="computedField2"
value="${javascript:'#{id.'+compositeData.fieldName+'}'}">
</xp:text>
</xp:view>
答案 0 :(得分:5)
您可以使用SSJS函数getClientId()
从客户端JS(使用SSJS)获取动态生成的id。因此,在您的情况下,它将与x $函数结合使用:
x$('#{javascript:getClientId(compositeData.fieldName)}')