WEB开发网
开发学院软件开发Java JSF 2 fu: Ajax 组件 阅读

JSF 2 fu: Ajax 组件

 2010-06-08 00:00:00 来源:WEB开发网   
核心提示: 最后,请注意 getListBoxId() 函数,JSF 2 fu: Ajax 组件(8),这个帮助器函数会从文本输入的客户机标识符中获取列表框的客户机标识符,该函数可以完成此任务,因为它将与 清单 4 中的 autoComplete 组件相结合, autoComplete 组件将 input

最后,请注意 getListBoxId() 函数。这个帮助器函数会从文本输入的客户机标识符中获取列表框的客户机标识符。该函数可以完成此任务,因为它将与 清单 4 中的 autoComplete 组件相结合。 autoComplete 组件将 input 和 listbox 分别指定为文本框和列表框的组件标识符,因此 getListBoxId() 函数会删除 input 并附加 listbox,以便获取文本输入的客户机标识符。

清单 6 显示了监听程序的最终实现:

清单 6. 监听程序

package com.corejsf; 
 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 
 
import javax.enterprise.context.SessionScoped; 
import javax.faces.component.UIInput; 
import javax.faces.component.UISelectItems; 
import javax.faces.component.UISelectOne; 
import javax.faces.context.FacesContext; 
import javax.faces.event.ValueChangeEvent; 
import javax.inject.Named; 
 
@Named 
@SessionScoped 
public class AutocompleteListener implements Serializable { 
  private static String COMPLETION_ITEMS_ATTR = "corejsf.completionItems"; 
  
  public void valueChanged(ValueChangeEvent e) { 
   UIInput input = (UIInput)e.getSource(); 
   UISelectOne listbox = (UISelectOne)input.findComponent("listbox"); 
 
   if (listbox != null) { 
     UISelectItems items = (UISelectItems)listbox.getChildren().get(0); 
     Map<String, Object> attrs = listbox.getAttributes(); 
     List<String> newItems = getNewItems((String)input.getValue(), 
      getCompletionItems(listbox, items, attrs)); 
 
     items.setValue(newItems.toArray()); 
     setListboxStyle(newItems.size(), attrs); 
   } 
  } 
  
  public void completionItemSelected(ValueChangeEvent e) { 
   UISelectOne listbox = (UISelectOne)e.getSource(); 
   UIInput input = (UIInput)listbox.findComponent("input"); 
   
   if(input != null) { 
    input.setValue(listbox.getValue()); 
   } 
   Map<String, Object> attrs = listbox.getAttributes(); 
   attrs.put("style", "display: none"); 
  } 
  
  private List<String> getNewItems(String inputValue, String[] completionItems) { 
   List<String> newItems = new ArrayList<String>(); 
   
   for (String item : completionItems) { 
     String s = item.substring(0, inputValue.length()); 
     if (s.equalsIgnoreCase(inputValue)) 
      newItems.add(item); 
   } 
   
   return newItems; 
  } 
  
  private void setListboxStyle(int rows, Map<String, Object> attrs) { 
   if (rows > 0) { 
     Map<String, String> reqParams = FacesContext.getCurrentInstance() 
      .getExternalContext().getRequestParameterMap(); 
    
     attrs.put("style", "display: inline; position: absolute; left: " 
       + reqParams.get("x") + "px;" + " top: " + reqParams.get("y") + "px"); 
 
     attrs.put("size", rows == 1 ? 2 : rows); 
   } 
   else 
     attrs.put("style", "display: none;"); 
  } 
 
  private String[] getCompletionItems(UISelectOne listbox, 
   UISelectItems items, Map<String, Object> attrs) { 
     Strings] completionItems = (String[])attrs.get(COMPLETION_ITEMS_ATTR); 
   
     if (completionItems == null) { 
      completionItems = (String[])items.getValue(); 
      attrs.put(COMPLETION_ITEMS_ATTR, completionItems); 
     } 
   return completionItems; 
  } 
} 

上一页  3 4 5 6 7 8 9 10  下一页

Tags:JSF fu Ajax

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接