以编程方式添加selectonemenu

时间:2015-02-11 08:06:42

标签: jsf primefaces selectonemenu

我在运行时添加Primefaces的selectonemenu组件时遇到问题。 我添加了selectonemenu但它没有显示对象的名称或attrbt,它显示了它们的引用。 谢谢你的时间。

<ui:composition template="/pages/admin/admin.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui">

    <ui:define name="center">
        <h:form id="openposition">
            <h:panelGrid id="openpanelposition" columns="1">
                <p:outputLabel value="#{menu['menu.admin.openposition.name']}"/>
                <p:selectOneMenu id="positiontype" value="#{openPositionController.position}" converter="#{positionConverter}" panelStyle="width:180px"
                                 effect="fade" var="u" style="width:160px" filter="true" filterMatchMode="startsWith">
                    <f:selectItems value="#{openPositionController.positionList}" var="position" itemLabel="#{position.name}" itemValue="#{position}" />
                    <p:column>
                        <h:outputText value="#{u.name}" />
                    </p:column>
                </p:selectOneMenu>
                <h:panelGrid id="unitgroup" columns="1">
                    <p:selectOneMenu id="higherunit" value="#{openPositionController.unit}" converter="#{unitConverter}" panelStyle="width:180px"
                                     effect="fade" var="unit" style="width:160px" filter="true" filterMatchMode="startsWith"
                                     valueChangeListener="#{openPositionController.selectunit}">
                        <f:selectItems value="#{openPositionController.unitList}" var="unit" itemLabel="#{unit.unitName}" itemValue="#{unit}" />
                        <p:ajax update="@this"/>
                        <p:column>
                            <h:outputText value="#{unit.unitName}" />
                        </p:column>
                    </p:selectOneMenu>
                </h:panelGrid>
            </h:panelGrid>
        </h:form>
    </ui:define>

</ui:composition>
package tr.com.innova.hrm.web.controller.menucontroller;

/**
 * Created by mcan on 22/01/2015.
 */

import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;
import tr.com.innova.hrm.model.entity.Position;
import tr.com.innova.hrm.model.entity.Unit;
import tr.com.innova.hrm.service.api.business.PositionService;
import tr.com.innova.hrm.service.api.business.UnitService;
import tr.com.innova.hrm.web.converter.UnitConverter;

import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.List;

@ManagedBean(name = "openPositionController")
@ViewScoped
public class OpenPositionViewController implements Serializable{

    private Position position;
    private Unit unit;
    private List<Position> positionList;
    private List<Unit> unitList;

    @ManagedProperty("#{positionService}")
    private PositionService service;

    @ManagedProperty("#{unitService}")
    private UnitService unitService;

    public UnitService getUnitService() {
        return unitService;
    }

    public void setUnitService(UnitService unitService) {
        this.unitService = unitService;
    }

    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }

    public List<Unit> getUnitList() {
        return unitList;
    }

    public void setUnitList(List<Unit> unitList) {
        this.unitList = unitList;
    }

    public Position getPosition() {
        return position;
    }

    public void setPosition(Position position) {
        this.position = position;
    }

    public List<Position> getPositionList() {
        return positionList;
    }

    public void setPositionList(List<Position> positionList) {
        this.positionList = positionList;
    }

    public PositionService getService() {
        return service;
    }

    public void setService(PositionService service) {
        this.service = service;
    }

    @PostConstruct
    public void init()
    {
        positionList = service.loadAll();
        unitList = unitService.getAllUnit();
        FacesContext.getCurrentInstance().getViewRoot();
    }

    public void selectunit()
    {
        HtmlPanelGrid component = (HtmlPanelGrid) FacesContext.getCurrentInstance().getViewRoot().findComponent(":openposition:unitgroup");
        SelectOneMenu oneMenu = new SelectOneMenu();
        oneMenu.setRendered(true);
        oneMenu.setConverter(new UnitConverter());
        UISelectItems items = new UISelectItems();

        ValueExpression expr = getValueExpression("#{openPositionController.unitList}");
        items.setValueExpression("value", expr);
        oneMenu.getChildren().add(items);
        component.getChildren().add(oneMenu);

        RequestContext context = RequestContext.getCurrentInstance();
        context.update("openposition");
        context.update("openposition:unitgroup");
    }

    private ValueExpression getValueExpression(String expression) {
        ExpressionFactory expressionFactory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
        ELContext expressionContext = FacesContext.getCurrentInstance().getELContext();
        return expressionFactory.createValueExpression(expressionContext, expression, Object.class);
    }
}

1 个答案:

答案 0 :(得分:0)

这是我对子孙后代的答案... 这是我以编程方式创建PrimeFaces的SelectOneMenu的方式:

// Get context and expression factory
FacesContext context = FacesContext.getCurrentInstance();
ExpressionFactory ef = context.getApplication().getExpressionFactory();

// Create panel grid
HtmlPanelGrid grid = new HtmlPanelGrid();
grid.setColumns(2);

// Create the select label
OutputLabel comboLabel = new OutputLabel();
comboLabel.setValue(obj.getNameFilter());
comboLabel.setFor(cmpName);
grid.getChildren().add(comboLabel);

// The biding expression of the select
ValueExpression comboExpr = ef.createValueExpression(context.getELContext(),
        "#{MbDinamicReports.valueCombo}", String.class);

// Create the select
SelectOneMenu comboBox = new SelectOneMenu();
comboBox.setId(cmpName);
comboBox.setValueExpression("value", comboExpr);
// Here the items are added
for (int i = 0; i < 3; i++) {
    UISelectItem item = new UISelectItem();
    item.setItemLabel("Label" + i);
    item.setItemValue("value" + i);
    comboBox.getChildren().add(item);
}
grid.getChildren().add(comboBox);