rich:选择下拉跳转到列表中当前选定的项目

时间:2013-07-18 08:50:20

标签: html jsf richfaces

我们正在使用rich:select作为下拉输入。

<rich:select id="select" value="#{controller.attrs.value}" >
       <f:selectItems value="#{controller.attrs.items}" var="foo"
            itemValue="#{foo}" itemLabel="#{foo.bar}" />
       <f:ajax event="#{ajaxActionController.action}" render="input" />            
</rich:select>

我们的问题是,当触发下拉菜单时,它始终以列表中的第一个元素开头。但是我们希望它跳转到当前所选项目的位置。

2 个答案:

答案 0 :(得分:1)

不可能通过传统方式,但这应该有效:

<rich:select id="select" onlistshow="L.scrollList()" onlistclick="L.saveId()">

...

L = window.L || {};

(function() {        
    id = 0;    

    L.saveId = function() {
        id = #{ rich:component('select') }.list.index;
    };

    L.scrollList = function() {
        var list = #{ rich:component('select') }.list,
            listDiv = $( #{ rich:component('select') }.popupList.popup ).find(".rf-sel-lst-scrl"),
            selectedDivPos = $(list.items[id]).position();  

            listDiv.scrollTop(selectedDivPos.top - 7);
    };  

})(L); 

答案 1 :(得分:0)

<h:inputHidden id="selectedId" value="#{controller.attrs.value}"/>
<rich:select id="select" value="#{controller.attrs.value}" >
   <f:selectItems value="#{controller.attrs.items}" var="foo"
        itemValue="#{foo}" itemLabel="#{foo.bar}" />
   <f:ajax event="#{ajaxActionController.action}" render="selectedId input" />            

制作<h:inputHidden/>并将其ID(此示例中为selectedId)添加到ajax的"render=",然后<h:inputHidden/>值将在新选择时更新。

现在,您可以在javascript中获取<h:inputHidden/></rich:select>的所选值)的值:

var selectedValue = document.getElementById('selectedId').value;