简体   繁体   English

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

[英]How to assign columns to datatable programmatically in jsf?

I am trying to create a datatable programmatically 我想创建一个datatable编程

Here where i am making a new instance for the 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);
        }

    }
}

I am assign the related columns by this way : 我通过这种方式分配相关的列:

 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 Class 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() : getColDataTableIdTest():

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

getColDataTableNameTest() : getColDataTableNameTest():

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

AceColumn class : 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() 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 is the panel group that holds the component in the page 容器是在页面中保存组件的面板组

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);


    }

}

Inside the xhtml Page there is only : 在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>

the problem is in the output of the datatable : wrong output 问题出在数据表的输出中: 错误的输出

and the right output is like this without the expiry date: right output 正确的输出是这样的,没有到期日期: 正确的输出

we must assign the 'datatable' var to the output label 我们必须将'datatable'变量分配给输出标签

setDatatableVar("role");

then we must send a value expression to the output label and we must delete the for loop 那么我们必须将值表达式发送到输出标签,并且必须删除for循环

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

and we must assign a value expression the 并且我们必须分配一个值表达式

OutPutLabel 输出标签

class

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());

and the code must be like this 并且代码必须像这样

 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()); 

the out put well be like this : 放完就是这样: 正确的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM