This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JSF and the fly likes data table,selectbooleancheckbox and  row deleting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "data table,selectbooleancheckbox and  row deleting" Watch "data table,selectbooleancheckbox and  row deleting" New topic
Author

data table,selectbooleancheckbox and row deleting

tun zaw
Greenhorn

Joined: Jan 23, 2008
Posts: 22
i have some problem with row deleting from datable.
i use selectbooleancheckbox in the datable.

i don't understand the concept.
Although i read source code,i don't understand.
Please guide me and explain about getwrappeddata().

datatable is created the data from database.i used Result and retrieve table
from page directly.i used bean to retrieve table.

please guide to source.
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Hello Tun,

It is difficult to understand your question. Please could you perhaps post some code snippets, to better explain the problem your current situation, and the problem.
tun zaw
Greenhorn

Joined: Jan 23, 2008
Posts: 22
hi, here is my code snippet.i get it from javabeat.com.
i want to assign Name[] name by database.
And i want to delete any from datable by selectbooleancheckbox.

<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<head>
<link href="styles.css" rel="stylesheet" type="text/css"/>
<title>
<hutputText value="#{msgs.windowTitle}"/>
</title>
</head>
<body>
<h:form>
<h:dataTable value="#{tableData.names}" var="name"
styleClass="names" headerClass="namesHeader"
columnClasses="last,first">
<h:column rendered="#{tableData.editable}">
<f:facet name="header">
<hutputText value="#{msgs.deleteColumnHeader}"/>
</f:facet>
<h:selectBooleanCheckbox value="#{name.markedForDeletion}"
onchange="submit()"/>
</h:column>
<h:column>
<f:facet name="header">
<hutputText value="#{msgs.lastColumnHeader}"/>
</f:facet>
<hutputText value="#{name.last},"/>
</h:column>
<h:column>
<f:facet name="header">
<hutputText value="#{msgs.firstColumnHeader}"/>
</f:facet>
<hutputText value="#{name.first}"/>
</h:column>
</h:dataTable>
<hutputText value="#{msgs.editPrompt}"/>
<h:selectBooleanCheckbox onchange="submit()"
value="#{tableData.editable}"/>
<h:commandButton value="#{msgs.deleteButtonText}"
rendered="#{tableData.editable}"
action="#{tableData.deleteNames}"
disabled="#{not tableData.anyNamesMarkedForDeletion}"/>
</h:form>
</body>
</f:view>
</html>


package com.corejsf;

public class Name {
private String first;
private String last;
private boolean markedForDeletion = false;

public Name(String first, String last) {
this.first = first;
this.last = last;
}

public void setFirst(String newValue) { first = newValue; }
public String getFirst() { return first; }

public void setLast(String newValue) { last = newValue; }
public String getLast() { return last; }

public boolean isMarkedForDeletion() { return markedForDeletion; }
public void setMarkedForDeletion(boolean newValue) {
markedForDeletion = newValue;
}
}

package com.corejsf;

import javax.faces.model.DataModel;
import javax.faces.model.ArrayDataModel;

public class TableData {
private boolean editable = false;
private ArrayDataModel model = null;

private static final Name[] names = {
new Name("Anna", "Keeney"),
new Name("John", "Wilson"),
new Name("Mariko", "Randor"),
new Name("William", "Dupont"),
};

public TableData() { model = new ArrayDataModel(names); }

public DataModel getNames() { return model; }

public boolean isEditable() { return editable; }
public void setEditable(boolean newValue) { editable = newValue; }

public String deleteNames() {
if (!getAnyNamesMarkedForDeletion())
return null;

Name[] currentNames = (Name[]) model.getWrappedData();
Name[] newNames = new Name[currentNames.length
- getNumberOfNamesMarkedForDeletion()];

for(int i = 0, j = 0; i < currentNames.length; ++i) {
Name name = (Name) currentNames[i];
if (!name.isMarkedForDeletion()) {
newNames[j++] = name;
}
}
model.setWrappedData(newNames);
return null;
}

public int getNumberOfNamesMarkedForDeletion() {
Name[] currentNames = (Name[]) model.getWrappedData();
int cnt = 0;

for(int i = 0; i < currentNames.length; ++i) {
Name name = (Name) currentNames[i];
if (name.isMarkedForDeletion())
++cnt;
}
return cnt;
}

public boolean getAnyNamesMarkedForDeletion() {
Name[] currentNames = (Name[]) model.getWrappedData();
for(int i = 0; i < currentNames.length; ++i) {
Name name = (Name) currentNames[i];
if (name.isMarkedForDeletion())
return true;
}
return false;
}
}
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Howzit Tun,

Can you also post the faces config xml file.

So far, I think you need to add a binding to your datatable defintion in the jsp. I'll be able to confirm this when you send me the xml config file.

cheers
Darryl
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Hi again Tun,

Firstly, there is no database. The names are hard coded into an Array, and that array is manipulated depending on user selection.

Let's look at ArrayDataModel. I suggest you first look at DataModel, get the hang of that. Then look at ArrayDataModel. ArrayDataModel, is a DataModel that wraps Arrays. The getWrappedData method according to the API documentation "Return the object representing the data wrapped by this DataModel, if any." So that returns the original Array that you used to initialise that ArrayDataModel.

Person[] x = ...; //an array of Person objects...
ArrayDatamodel adm = new ArrayDataModel(x); //passing the array of Person objects into ArrayDataModel..
adm.getWrappedData() is equals to x. So you can cast adm.getWrappedData to a Person array. like this.
Person[] people = (Person[]) adm.getWrappedData();
Does that make sense?

The rest is pretty self explanatory. Do you have the API documentation?
tun zaw
Greenhorn

Joined: Jan 23, 2008
Posts: 22
Thank you very much for your reply.
i am beginner in jsf UI frame library.i use tomahawk.
if you have some other concepts for my problem,please guide me.
i need many help for my problem.Please help me.

if i will use with mysql database,may i need other some concept?
my email address is tunzaw2008@gmail.com.

i remember for your help forever.
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Hi Again Tun,

I have read your post a couple of times now, and don't understand what your problem is. What is your problem. You asked about the getWrappedData and I explained. If you want to change the hard coding of the data, ie where you get the names from, then get the names from a a database, and in the TableData class populate the Name array with the new values.

I'm sorry, but your problem is not clear so the solution will also not be clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: data table,selectbooleancheckbox and row deleting