开发运行环境:JDeveloper 11.1.2.4 + Oracle Database XE11gR2
自动提示功能是页面上非常常见的一个功能。
本文以InputText组件为例,来说明如何实现自动提示功能。与网上的其它实现不同,本文介绍的实现方式具有一定的普遍性。
需求很简单:实现Employee的JobId字段的自动提示功能。
1. 选择Departments、Employees、Jobs表生成EO和VO。
2. 在JobsView上增加一个View Criteria:queryJobsByJobIdViewCriteria,根据JobId查询Jobs。
3. 定制JobsViewImpl.java,增加一个方法:queryJobsByJobId,并暴露到Client。
publicvoid queryJobsByJobId(String partOfJobIdStr) {
this.setApplyViewCriteriaNames(null);
ViewCriteria criteria =this.getViewCriteria("QueryJobsByJobIdViewCriteria");
this.setRangeSize(10);
this.applyViewCriteria(criteria);
this.setbv_JobId(partOfJobIdStr);
this.executeQuery();
}
4. 拖放Employees Data Control生成Form
(1)在JobId上增加AutoSuggest Behavior
(2)设置AutoSubmit=true
完成后的代码如下:
<af:inputTextvalue="#{bindings.JobId.inputValue}"label="#{bindings.JobId.hints.label}"
required="#{bindings.JobId.hints.mandatory}"
columns="#{bindings.JobId.hints.displayWidth}"
maximumLength="#{bindings.JobId.hints.precision}"
shortDesc="#{bindings.JobId.hints.tooltip}"autoSubmit="true" id="it6">
<f:validator binding="#{bindings.JobId.validator}"/>
<af:autoSuggestBehavior maxSuggestedItems="10"
suggestItems="#{viewScope.myBackingBean.onJobIdSuggest}"/>
</af:inputText>
(3)对应的Managed Bean中的方法:onJobIdSuggest,代码如下:
publicList onJobIdSuggest(FacesContext facesContext, AutoSuggestUIHintsautoSuggestUIHints) {
String param = autoSuggestUIHints.getSubmittedValue();
OperationBinding binding =ADFUtils.findOperation("queryJobsByJobId");
binding.getParamsMap().put("partOfJobIdStr", param);
binding.execute();
DCIteratorBinding it = ADFUtils.findIterator("JobsView1Iterator");
it.setRangeSize(10);
Row[] allRowsInRange = it.getAllRowsInRange();
ArrayList selectItems = new ArrayList();
for (Row o : allRowsInRange) {
String var = (String)o.getAttribute("JobId");
String desc =(String)o.getAttribute("JobTitle");
selectItems.add(new SelectItem(var, var + " " +desc));
}
return selectItems;
}
5. 修改页面的Bindings
(1)手工在Executables中增加Iterator:JobsView1Iterator