tags:

views:

53

answers:

1

I have generating a HtmlCommandButton in backing bean. By clicking that button I want to delete a HtmlColumn from a HtmlDataTable which was also generated in the baking bean.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"&gt;
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>JSF dynamic</title>
    </head>
    <body>        
        <h:form prependId="false">
            <br/><br/>
            <h:outputText value="Enter no. of columns : "/>            
            <h:inputText value="#{MyTest.colNos}"/>&#160;&#160;
            <h:commandButton value="add" action="#{MyTest.createTable}">                
            </h:commandButton>
            <br/><br/>
            <br/><br/>
            <h:dataTable id="table" binding="#{MyTest.hdt}">
            </h:dataTable>
        </h:form>
    </body>
</html>

MyTest.java

package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlColumn;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;


@ManagedBean(name = "MyTest")
@ViewScoped
public class MyTest {

        public MyTest() {
    }
    private Integer colNos;

    public Integer getColNos() {
        return colNos;
    }

    public void setColNos(Integer colNos) {
        this.colNos = colNos;
    }
    List<Object> objects = new ArrayList<Object>();

    public List<Object> getObjects() {
        return objects;
    }

    public void setObjects(List<Object> objects) {
        this.objects = objects;
    }
    Integer countMenu1;
    Integer countMenu2;

    public Integer getCountMenu1() {
        return countMenu1;
    }

    public void setCountMenu1(Integer countMenu1) {
        this.countMenu1 = countMenu1;
    }

    public Integer getCountMenu2() {
        return countMenu2;
    }

    public void setCountMenu2(Integer countMenu2) {
        this.countMenu2 = countMenu2;
    }
    HtmlDataTable hdt;

    public HtmlDataTable getHdt() {
        return hdt;
    }

    public void setHdt(HtmlDataTable hdt) {
        this.hdt = hdt;
    }

    public void createTable() {
        hdt.getChildren().clear();
        hdt.setValueExpression("value", createValueExpression("#{MyTest.tableObjects}", List.class));
        hdt.setVar("tobject");
        for (int i = 0; i < colNos; i++) {

            HtmlColumn column = new HtmlColumn();
            HtmlCommandButton commandButton = new HtmlCommandButton();
            commandButton.setValue("Delete" + i);
            commandButton.setActionExpression(createMethodExpression("#{MyTest.deleteColumn('" + i + "')}", Void.TYPE, new Class<?>[0]));
            column.getChildren().add(commandButton);

            HtmlOutputText htmlOutputText5 = new HtmlOutputText();
            htmlOutputText5.setValue("<br/>");
            htmlOutputText5.setEscape(false);
            column.getChildren().add(htmlOutputText5);

            HtmlInputText htmlInputText = new HtmlInputText();
            htmlInputText.setValueExpression("value", createValueExpression("#{tobject.headerName}", String.class));
            column.getChildren().add(htmlInputText);

            HtmlOutputText htmlOutputText1 = new HtmlOutputText();
            htmlOutputText1.setValue("<br/>");
            htmlOutputText1.setEscape(false);
            column.getChildren().add(htmlOutputText1);

            HtmlSelectOneMenu menu1 = new HtmlSelectOneMenu();
            menu1.setValue(countMenu1);
            UISelectItems itemGroup1 = new UISelectItems();
            itemGroup1.setValueExpression("value", createValueExpression("#{tobject.dataTypes}", List.class));
            menu1.getChildren().add(itemGroup1);
            column.getChildren().add(menu1);

            HtmlOutputText htmlOutputText2 = new HtmlOutputText();
            htmlOutputText2.setValue("<br/>");
            htmlOutputText2.setEscape(false);
            column.getChildren().add(htmlOutputText2);

            HtmlSelectOneMenu menu2 = new HtmlSelectOneMenu();
            menu2.setValue(countMenu2);
            UISelectItems itemGroup2 = new UISelectItems();
            itemGroup2.setValueExpression("value", createValueExpression("#{tobject.filledBy}", List.class));
            menu2.getChildren().add(itemGroup2);
            column.getChildren().add(menu2);

            hdt.getChildren().add(column);

        }
    }
    List<TableObject> tableObjects = new ArrayList<TableObject>();

    public List<TableObject> getTableObjects() {
        if (tableObjects.isEmpty()) {
            TableObject to1 = new TableObject();
            to1.setHeaderName("");
            List<SelectItem> filledBy = new ArrayList<SelectItem>();
            filledBy.add(new SelectItem(1, "Buyer"));
            filledBy.add(new SelectItem(2, "Vendor"));
            to1.setFilledBy(filledBy);
            List<SelectItem> dataTypes = new ArrayList<SelectItem>();
            dataTypes.add(new SelectItem(1, "String"));
            dataTypes.add(new SelectItem(2, "Integer"));
            to1.setDataTypes(dataTypes);
            tableObjects.add(to1);
        }
        return tableObjects;
    }

    public void setTableObjects(List<TableObject> tableObjects) {
        this.tableObjects = tableObjects;
    }

    public void deleteColumn(String arg) {
        hdt.getChildren().remove(Integer.parseInt(arg));              
    }

    private ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createValueExpression(
                facesContext.getELContext(), valueExpression, valueType);
    }

    private MethodExpression createMethodExpression(String valueExpression, Class<?> valueType, Class<?>[] expectedParamTypes) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createMethodExpression(
                facesContext.getELContext(), valueExpression, valueType, expectedParamTypes);
    }

}
A: 

Use HtmlDataTable binding in backing bean.

<h:dataTable id="dataTable" binding="#{backingBean.tableComponent}" value="#{backingBean.someList}">

On click action

//---

SomeObject selectedObject = (SomeObject) tableComponent.getRowData();

someList.remove(selectedObject);

//--
Nayan Wadekar