File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Multiple Data Tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Multiple Data Tables" Watch "Multiple Data Tables" New topic
Author

Multiple Data Tables

Khalid Basapur
Greenhorn

Joined: Mar 23, 2009
Posts: 1
Hi,
I am trying to create new Datatable dynamically when user clicks a createTable command button. Now i have a list of datatables which are mapped to only one list in the Backing bean. Now i want to add a new row to selected datatable. I am creating new empty object of list to create a row. The problem is it is updating all the datatable instead of selected datatable.

The JSP Code is As Follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib prefix="a4j" uri="http://richfaces.org/a4j"%>
<%@ taglib prefix="rich" uri="http://richfaces.org/rich"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link href="<%= request.getContextPath() %>/css/main.css"
rel="stylesheet" media="screen" />
</head>
<body>
<f:view>
<rich:panel>
<h:form>
<h:commandButton value="Create Table"
actionListener="#{multiTableBean.createDynamicTable}" id="AddTable"></h:commandButton>
<rich:panel binding="#{multiTableBean.richPanel}">
<rich:dataTable binding="#{multiTableBean.dataTable}">
</rich:dataTable>
</rich:panel>
</h:form>
</rich:panel>
</f:view>
</body>
</html>

And the Backing bean is AS Follows:
private void createNewBlock() {
HtmlSpacer spacer = new HtmlSpacer();
spacer.setHeight("5px");
richPanel.getChildren().add(spacer);

dataTable = new HtmlDataTable();
dataTable.setValue(bodyList);
dataTable.setVar("model");
dataTable.setHeaderClass("headerClass");
dataTable.setColumnClasses("column-text");
dataTable.setValueExpression("value", createValueExpression(
"#{multiTableBean.bodyList}", List.class));
int columns = getBodyList().size();
System.err.println("body List Size---->" + columns);
System.err.println("Count=-=>" + count);
for (int i = 0; i < columns; i++) {
// Create Element Name Text Box.
HtmlInputText elementName = new HtmlInputText();
elementName.setStyleClass("textBoxClass");
elementName.setValueExpression("value", createValueExpression(
"#{model.elementName}", String.class));

// Create Data type ComboBox.
HtmlComboBox dataTypeComboBox = new HtmlComboBox();
dataTypeComboBox.setValueExpression("value", createValueExpression(
"value", String.class));
dataTypeComboBox.setWidth("110");
dataTypeComboBox.setStyleClass("comboBoxClass");
dataTypeComboBox.setDefaultLabel("Select Datatype");
dataTypeComboBox.setDirectInputSuggestions(true);
// Add SelectItems To Data type ComboBox.
UISelectItems selectItems = new UISelectItems();
selectItems.setValueExpression("value", createValueExpression(
"#{multiTableBean.dataTypeList}", List.class));
dataTypeComboBox.getChildren().add(selectItems);

// Create is Mandatory Check Box.
HtmlSelectBooleanCheckbox isMandatoryCheckBox = new HtmlSelectBooleanCheckbox();
isMandatoryCheckBox.setValueExpression("value",
createValueExpression("#{model.mandatory}", Boolean.class));

// Create is Repeatable Check Box.
HtmlSelectBooleanCheckbox isRepeatableCheckBox = new HtmlSelectBooleanCheckbox();
isRepeatableCheckBox
.setValueExpression("value", createValueExpression(
"#{model.repeatable}", Boolean.class));

// Create hasChild check box.
HtmlSelectBooleanCheckbox hasChildCheckBox = new HtmlSelectBooleanCheckbox();
hasChildCheckBox.setValueExpression("value", createValueExpression(
"#{model.hasChild}", Boolean.class));

// Create Parent Element ComboBox.
HtmlComboBox parentElementComboBox = new HtmlComboBox();
parentElementComboBox.setValueExpression("value",
createValueExpression("#{model.parentElementName}",
String.class));
parentElementComboBox.setWidth("110");
parentElementComboBox.setStyleClass("comboBoxClass");
parentElementComboBox.setDefaultLabel("Select Parent");

// Add SelectItems To Parent Element ComboBox.
UISelectItems parentElementSelectItems = new UISelectItems();
parentElementSelectItems.setValueExpression("value",
createValueExpression(
"#{multiTableBean.parentElementList}", List.class));
parentElementComboBox.getChildren().add(parentElementSelectItems);

// Create Length text Box
HtmlInputText lengthTextBox = new HtmlInputText();
lengthTextBox.setSize(5);
lengthTextBox.setStyleClass("smallTextBoxClass");
lengthTextBox.setValueExpression("value", createValueExpression(
"#{model.maxSize}", Integer.class));

// Create Image Command Button.
HtmlCommandButton actionButton = new HtmlCommandButton();
actionButton.setImage("/images/delete16.gif");
actionButton.setTitle("Delete");
actionButton.setId("button" + i);
actionButton
.setActionListener(createActionListener("#{multiTableBean.deleteRow}"));

// MethodExpression methodExpression =
// createMethodExpression("#{multiTableBean.deleteRow}",
// String.class);
// actionButton.setActionExpression(methodExpression);

HtmlSpacer gridSpacer = new HtmlSpacer();
gridSpacer.setWidth("620px");

HtmlCommandButton addButton = new HtmlCommandButton();
addButton.setValue("Add Row");
addButton.setId("addBut_" + count);
addButton.setTitle("Add Row");
addButton.setStyleClass("generalButtonClass");
addButton
.setActionListener(createActionListener("#{multiTableBean.addNewRow}"));

HtmlSpacer gridSpacer1 = new HtmlSpacer();
gridSpacer1.setWidth("2px");

HtmlCommandButton saveButton = new HtmlCommandButton();
saveButton.setValue("Save");
saveButton.setId("saveBut_" + count);
saveButton.setTitle("Save");
saveButton.setStyleClass("generalButtonClass");
saveButton
.setActionListener(createActionListener("#{multiTableBean.saveRowData}"));

HtmlPanelGrid panelGrid = new HtmlPanelGrid();
panelGrid.setColumns(4);
panelGrid.getChildren().add(gridSpacer);
panelGrid.getChildren().add(addButton);
panelGrid.getChildren().add(gridSpacer1);
panelGrid.getChildren().add(saveButton);

// Create and Set header Label For DataTable Columns.
HtmlOutputText elementNameLabel = new HtmlOutputText();
elementNameLabel.setValue("Element Name");

HtmlOutputText dataTypeLabel = new HtmlOutputText();
dataTypeLabel.setValue("Data Type");

HtmlOutputText isMandatoryLabel = new HtmlOutputText();
isMandatoryLabel.setValue("Is Mandatory");

HtmlOutputText isRepeatableLabel = new HtmlOutputText();
isRepeatableLabel.setValue("Is Repeatable");

HtmlOutputText hasChildLabel = new HtmlOutputText();
hasChildLabel.setValue("Has Child");

HtmlOutputText parentElementLabel = new HtmlOutputText();
parentElementLabel.setValue("Parent Element");

HtmlOutputText lengthLabel = new HtmlOutputText();
lengthLabel.setValue("Length");

HtmlOutputText actionsLabel = new HtmlOutputText();
actionsLabel.setValue("Actions");

// Create and Set columns.
HtmlColumn column = new HtmlColumn();
column.setHeader(elementNameLabel);
column.getChildren().add(elementName);

HtmlColumn datatypeColumn = new HtmlColumn();
datatypeColumn.setHeader(dataTypeLabel);
datatypeColumn.getChildren().add(dataTypeComboBox);

HtmlColumn isMandatoryColumn = new HtmlColumn();
isMandatoryColumn.setHeader(isMandatoryLabel);
isMandatoryColumn.setStyle("text-align:center");
isMandatoryColumn.getChildren().add(isMandatoryCheckBox);

HtmlColumn isRepeatableColumn = new HtmlColumn();
isRepeatableColumn.setHeader(isRepeatableLabel);
isRepeatableColumn.setStyle("text-align:center");
isRepeatableColumn.getChildren().add(isRepeatableCheckBox);

HtmlColumn hasChildColumn = new HtmlColumn();
hasChildColumn.setHeader(hasChildLabel);
hasChildColumn.setStyle("text-align:center");
hasChildColumn.getChildren().add(hasChildCheckBox);

HtmlColumn parentElementColumn = new HtmlColumn();
parentElementColumn.setHeader(parentElementLabel);
parentElementColumn.getChildren().add(parentElementComboBox);

HtmlColumn lengthColumn = new HtmlColumn();
lengthColumn.setHeader(lengthLabel);
lengthColumn.getChildren().add(lengthTextBox);

HtmlColumn actionColumn = new HtmlColumn();
actionColumn.setHeader(actionsLabel);
actionColumn.getChildren().add(actionButton);

// Add columns to Data Table.
dataTable.getChildren().add(column);
dataTable.getChildren().add(datatypeColumn);
dataTable.getChildren().add(isMandatoryColumn);
dataTable.getChildren().add(isRepeatableColumn);
dataTable.getChildren().add(hasChildColumn);
dataTable.getChildren().add(parentElementColumn);
dataTable.getChildren().add(lengthColumn);
dataTable.getChildren().add(actionColumn);

// Add Data table To Panel.
richPanel.getChildren().add(dataTable);
richPanel.getChildren().add(panelGrid);
}
// Add HtmlDataTable to List<HtmlDataTable>.
dataTableList.add(dataTable);
tableMap.put("body_" + dataTableList.size(), bodyList);
System.err.println("List<HtmlDataTable> size--->"
+ dataTableList.size());
count++;

}

Now when user clicks add Row button it should add new row.

public void addNewRow(ActionEvent event) {
ReturnElement element = new ReturnName();
bodyList.add(elememt);
}

But the thing is it is not updating the present selected table. When i click createtable command button i am getting new table with row added.

My requirement is to add a new row for a datable dynamically.
Please help me solve this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Data Tables
 
Similar Threads
Programmatically create HTMLextendeddatatable
setting action in commnad link not working in a dynamic dataTable
Need Help With Dynamic JSF Datatable
Dynamic JSF, programatically adding components
Problem with dynamic dataTable generation