如何在jsf中以编程方式将列分配给datatable?

时间:2015-10-29 14:02:54

标签: jsf jsf-2 datatable

我正在尝试以编程方式创建datatable

我在这里为dataTable创建一个新实例:

public DataTables getDataTableTest() {
        if(dataTableTest==null){
            String var=getDatatableVar();
             dataTableTest=new DataTables(context, app, "datatable1", "width: 50%", var,
              "#{programmaticallyComp.roleLists}", "single", true, 5, "", "");
        }
        return dataTableTest;
    }

DataTables

public class DataTables extends DataTable  {
    private static final long serialVersionUID = 1L;
    public DataTables() {
        super();
        // TODO Auto-generated constructor stub
    }

    public DataTables(FacesContext context, Application app, String id,String style,String var,String expression,
            String selectionMode, boolean paginator, int row,
            String rowSelectListener, String rowUnSelectListener) {
        super();
        // TODO Auto-generated constructor stub
        app.createComponent(DataTable.COMPONENT_TYPE);
        setStyle(style + " !important;");
        setSelectionMode(selectionMode);
        setPaginator(paginator);
        setRows(row);
        setVar(var);
        setMethodsExpression(this, context, app, rowSelectListener, rowUnSelectListener);
        setComId(this, id);
        setValueExpressions(this, context, app, expression);


    }
    private void setComId(DataTable comp,String id){
        comp.setId(id);
    }
    private void setValueExpressions(DataTable comp ,FacesContext context,Application app,String expression){
        ELContext elContext=context.getELContext();
        ValueExpression value=app.getExpressionFactory().createValueExpression(elContext,expression ,Object.class);
        comp.setValueExpression("value",value);
    }
    private void setMethodsExpression(DataTable comp, FacesContext context,
            Application app, String rowSelectListener,
            String rowUnSelectListener) {
        ELContext elContext = context.getELContext();
        if (JSFUtils.isInputFilled(rowSelectListener)) {
            MethodExpression rowSelectListenerMethod=app.getExpressionFactory()
                    .createMethodExpression(elContext,
                    rowSelectListener, null,new Class[] {SelectEvent.class});
            comp.setRowSelectListener(rowSelectListenerMethod);
        }
        if (JSFUtils.isInputFilled(rowUnSelectListener)) {
            MethodExpression rowUnSelectListenerMethod=app.getExpressionFactory()
                    .createMethodExpression(elContext,
                    rowUnSelectListener, null,new Class[] {UnselectEvent.class});
            comp.setRowUnselectListener(rowUnSelectListenerMethod);
        }

    }
}

我通过这种方式分配相关列:

 getDataTableTest();
                for(RoleBean roles : roleLists){ 
                     OutputLabel    outputLabelId=new OutputLabel(context,app,roles.getRoles().getRoleNum());
                     OutputLabel    outputLabelName=new  OutputLabel(context,app,roles.getDescription().getDesc());

                        getColDataTableIdTest().getChildren().add(outputLabelId);
                        getColDataTableNameTest().getChildren().add(outputLabelName);
                          }
    getDataTableTest().getChildren().add(getColDataTableIdTest());
                getDataTableTest().getChildren().add(getColDataTableNameTest());
                getRowDataTable().getChildren().add(getDataTableTest()); 
container.getChildren().add(getRowDataTable());

OutPutLabel类

public class OutputLabel extends HtmlOutputLabel {

    public OutputLabel() {
        super();
        // TODO Auto-generated constructor stub
    }

    public OutputLabel(FacesContext context, Application app, String id,
            String value) {
        super();
        // TODO Auto-generated constructor stub

        app.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
        compSetId(this, id);

        setValue(value);
        setStyleClass(JSFUtils.BootstrapClasses.LabelsControl.toString());

    }

    public OutputLabel(FacesContext context, Application app, Object value) {
        super();
        // TODO Auto-generated constructor stub
        app.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
        setValue(value);
    }

    private void compSetId(UIOutput comp, String id) {
        comp.setId(id);
    }

getColDataTableIdTest():

public AceColumn getColDataTableIdTest() {
        if(colDataTableIdTest==null){
            colDataTableIdTest=new AceColumn(app,"ID");
        }
        return colDataTableIdTest;
    }

getColDataTableNameTest():

public AceColumn getColDataTableNameTest() {
            if(colDataTableNameTest==null){
                colDataTableNameTest=new AceColumn(app,"ID");
            }
            return colDataTableNameTest;
        }

AceColumn类:

public class AceColumn extends Column {

    private static final long serialVersionUID = 1L;
    public AceColumn() {
        super();
        // TODO Auto-generated constructor stub
    }
    public AceColumn(Application app,String headerText) {
        super();
        // TODO Auto-generated constructor stub
        app.createComponent(Column.COMPONENT_TYPE);
        setHeaderText(headerText);

    }

getRowDataTable()

public RowContainer getRowDataTable() {
        if(rowDataTable==null){
            rowDataTable= new RowContainer(context, app, false,"dataTableRowId");
        }
        return rowDataTable;
    }

RowContainer

    public class RowContainer extends HtmlPanelGroup {


        public RowContainer() {
            super();
            // TODO Auto-generated constructor stub
        }

        public RowContainer(FacesContext context,Application app,boolean required,String id) {


            app.createComponent(HtmlPanelGroup.COMPONENT_TYPE);
            if(required==true){
            setStyleClass(JSFUtils.BootstrapClasses.Rows.toString() + " " + 
                        JSFUtils.BootstrapClasses.Required.toString()
                        );
            }else{
                setStyleClass(JSFUtils.BootstrapClasses.Rows.toString());
            }
            setLayout(JSFUtils.BootstrapClasses.Layout.toString());
            setId(id);
        }
}

Container是在页面中保存组件的面板组

container=new ContainerFluid(context, app, "SuperPanel");

ContainerFluid

public class ContainerFluid extends HtmlPanelGroup  {


    public ContainerFluid() {
        super();
        // TODO Auto-generated constructor stub

    }

    public ContainerFluid(FacesContext context,Application app,String id) {

        app.createComponent(HtmlPanelGroup.COMPONENT_TYPE);
        setLayout(JSFUtils.BootstrapClasses.Layout.toString());
        setStyleClass(JSFUtils.BootstrapClasses.ContainerFluid.toString());
        //compSetId(this, id);
        setStyle("padding: 30px;");
        //setId(id);


    }

}

在xhtml页面内只有:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ace="http://www.icefaces.org/icefaces/components">

<ui:composition template="/resources/template/master.xhtml">
    <ui:define name="content">
        <h:panelGroup binding="#{programmaticallyComp.container}"/>
    </ui:define>
</ui:composition>
</html>

问题在于数据表的输出: wrong output

正确的输出是没有失效日期的: right output

1 个答案:

答案 0 :(得分:0)

我们必须分配&#39;数据表&#39; var到输出标签

setDatatableVar("role");

然后我们必须将值表达式发送到输出标签,我们必须删除for循环

OutputLabel label1= new OutputLabel(context, app,"#{role.roleNum}");

我们必须分配一个值表达式

OutPutLabel

class

private void compSetValue(UIOutput comp, FacesContext context,
            Application app, String expression) {
        ELContext elContext = context.getELContext();
        ValueExpression valueExpression = app.getExpressionFactory()
                .createValueExpression(elContext, expression, Object.class);
        comp.setValueExpression("value", valueExpression);

    }

并且代码必须像这样

setDatatableVar("role");
        OutputLabel label1= new OutputLabel(context, app,"#{role.roleNum}");
        OutputLabel label2= new OutputLabel(context, app,"#{role.roleName}");
        getColDataTableIdTest().getChildren().add(label1);
        getColDataTableNameTest().getChildren().add(label2);
        getColDataTableIdTest().getChildren().add(getColDataTableIdTest());
        getDataTableTest().getChildren().add(getColDataTableNameTest());
        getRowDataTable().getChildren().add(getDataTableTest());
        container.getChildren().add(getRowDataTable());

输出就像这样:correct output