I have list of h:selectBooleanCheckBox in rich:dataTabe. Also, there is pagination for the datatable.
The problem is when I click the next page number, the selected checkboxes at the first page of the datatable is gone. Though they are selected, clicking the next/previous page make them deselected.
Any idea about the problem?
These are the annotations for bean.
#ManagedBean(name = "bean")
#ViewScoped
To clarify it, I've attached my facelets and bean code below:
<rich:dataTable value="#{bean.ssTable}" var="data" iterationStatusVar="it" id="myDataTable">
...
<rich:column id="includeInWHMapping" >
<f:facet name="header">
<h:selectBooleanCheckbox value="#{bean.selectAll}" valueChangeListener="#{bean.selectAllCheckBox}">
<f:ajax render="myDataTable" />
</h:selectBooleanCheckbox>
</f:facet>
<h:selectBooleanCheckbox id="selectedForWHProcess" value="#{bean.checked[data]}">
<f:ajax actionListener="#{bean.selectAllRows}" />
</h:selectBooleanCheckbox>
</rich:column>
...
</rich:dataTable>
Bean code:
private Map<StandardStructure, Boolean> checked = new HashMap<StandardStructure, Boolean>();
private boolean selectAll;
/* Controller */
public MyController() {
super(new DataSetParameters());
logger.info("StandardStructureController created.");
Column rowid_col =new Column("rowid", "rowid", "No.", FilterTypes.NUMERIC, true, true, "");
Column fileid_col =new Column("fileid", "fileid", "File ID", FilterTypes.STRING, true, true, "");
Column releasetag_col =new Column("releasetag", "releasetag", "Releasetag ID", FilterTypes.STRING, true, true, "");
Column applicationid_col =new Column("applicationid", "applicationid", "Application ID", FilterTypes.STRING, true, true, "");
Column filename_col =new Column("filename", "filename", "Filename", FilterTypes.STRING, true, true, "ASC");
Column includeInWHMapping_col =new Column("includeInWHMapping", "includeInWHMapping", "Include in WH Mapping?", FilterTypes.NONE, true, true, "");
columns.put("fileid", fileid_col);
columns.put("releasetag", releasetag_col);
columns.put("applicationid", applicationid_col);
columns.put("filename", filename_col);
columns.put("includeInWHMapping", includeInWHMapping_col);
initialize();
setOrderField("importDate");
setOrder("DESC");
dataSetParameters.setColumns(columns);
loadTable();
}
/** getter/setter.. */
public boolean isSelectAll() {
return selectAll;
}
public void setSelectAll(boolean selectAll) {
this.selectAll = selectAll;
}
public Map<StandardStructure, Boolean> getChecked() {
return checked;
}
public void setChecked(Map<StandardStructure, Boolean> checked) {
this.checked = checked;
}
/** Load ssTable */
private void loadTable() {
try{
ssTable = new StandardStructureDao(dataSetParameters).getAllStandardStructure();
}catch (Exception ex){
System.out.println("Exception in loading table:"+ex);
}
}
/** Get ssTable */
public Collection<StandardStructure> getSsTable(){
return ssTable.getDto();
}
/** Pagination */
public void doPaginationChange(ActionEvent event) {
super.doPaginationChange(event);
loadTable();
/* trying to set the value of list of checkboxes after loading the table */
Iterator<StandardStructure> keys = checked.keySet().iterator();
while(keys.hasNext()){
StandardStructure ss = keys.next();
if(checked.get(ss)){ /* getting checked boxes */
/* Got stuck here. */
/* How do we just set the true (boolean) value only
for list of checkboxes though they are in Map?*/
System.out.println("Row id: " + ss.getRowid() + " Checked : " + checked.get(ss));
}
}
}
/** Select all the list of checkbox in datatable */
public void selectAllCheckBox(){
for(StandardStructure item : ssTable.getDto()){
if(!selectAll)
checked.put(item, true);
else
checked.put(item, false);
}
}
/** Select row of data in datatable */
public void selectAllRows(ValueChangeEvent e) {
boolean newSelectAll = (Boolean) e.getNewValue();
Iterator<StandardStructure> keys = checked.keySet().iterator();
logger.info("Rows selected..." + newSelectAll);
while(keys.hasNext()) {
StandardStructure ss = keys.next();
checked.put(ss, newSelectAll);
System.out.println("File::"+ss.getRowid()+":"+newSelectAll);
}
}
Many Thanks!
Since your code is unclear and confusing, I'll provide you the minimal example how pagination with select all on current page MIGHT look like. With no action listeners, just getters and setter. As simple as I could.
First XHTML:
<h:form>
<rich:dataTable value="#{bean.valuesOnPage}" var="data" id="dataTable" rows="20">
<rich:column>
<f:facet name="header">
<h:selectBooleanCheckbox value="#{bean.selectAll}">
<f:ajax render="dataTable" />
</h:selectBooleanCheckbox>
</f:facet>
<h:selectBooleanCheckbox value="#{bean.checked[data]}">
<f:ajax />
</h:selectBooleanCheckbox>
</rich:column>
<!-- other columns -->
</rich:dataTable>
</h:form>
Then the bean:
#ManagedBean(name = "bean")
#ViewScoped
public class MyBean {
// when the view is first loaded this is empty
// until someone will click one of checkboxes
private Map<Object, Boolean> checked = new HashMap<Object, Boolean>();
private boolean selectAll;
private List<Object> valuesOnPage;
private int currentPage = -1;
MyBean() {
setCurrentPage(1);
}
// no setter
public Map<Object, Boolean> getChecked() {
return checked;
}
public int getCurrentPage() {
return currentPage;
}
public boolean getSelectAll() {
return selectAll;
}
// no setter
public List<Object> getValuesOnPage() {
return valuesOnPage;
}
private void loadTable() {
try {
// gets data from data base
valuesOnPage = getData(currentPage);
} catch (Exception ex) {
System.out.println("Exception in loading table:" + ex);
}
}
public void setCurrentPage(int currentPage) {
if (this.currentPage != currentPage) {
this.currentPage = currentPage;
loadTable();
// we don't need it selected, especially if it
// was a paged we've never been on
selectAll = false;
}
}
public void setSelectAll(boolean selectAll) {
this.selectAll = selectAll;
for (Object o : valuesOnPage) {
checked.put(o, selectAll);
}
}
}
Look how and when the data is changing and when it is loaded. Check out that there's no unnessecary new action for checkbox of single row. JSF will take care of that with: value="#{bean.checked[data]}".
And once again: Your keys in map are objects. You have to make sure that equals method is good. In 95% of case the default is not, especially if they are #Entity. Check i.e. this topic.
Related
I'm trying to pass the changed value of the variable to a bean method:
<h:panelGroup rendered="#{! empty gradesBean.getAllGrades()}">
<h:dataTable value="#{gradesBean.getAllGrades()}" var="g">
<h:column>
<f:facet name="header">#{msg['outputSubject']}</f:facet>
<h:inputText value="#{g.subject}" onchange="#{g.subject}" />
</h:column>
<h:column>
<f:facet name="header">#{msg['outputGrade']}</f:facet>
<h:inputText value="#{g.mark}" onchange="#{g.mark}"/>
</h:column>
<h:column>
<h:form>
<h:commandButton value="#{msg['actionSave']}" action="#{gradesBean.edit(g)}" />
</h:form>
</h:column>
</h:dataTable>
(.........)
</h:panelGroup>
I want the changes of the user he does in the inputText on g.subject and g.mark to be passed to gradesBean.edit(g). When I try to do that somehow the same values of the original values of the both variables are passed. There are getter and setter methods in the respective object
#Named
#ViewScoped
public class GradesBean extends AbstractBean implements Serializable {
private static final long serialVersionUID = 320401008216711886L;
private static final String NO_GRADES_PRESENT = "keine Noten eingetragen";
private static final Logger loggerLogger.getLogger(GradesBean.class);
#Inject
private transient GradeDAO gradeDAO;
#Inject
private UserDAO userDAO;
private Grade grade;
private List<Grade> allGrades;
#PostConstruct
public void init() {
if (!isLoggedIn()) {
return;
}
grade = new Grade();
allGrades = getSession().getUser().getGrades();
}
public Grade getGrade() {
return grade;
}
public List<Grade> getAllGrades() {
return allGrades;
}
public String getGradeAverage() {
final List<BigDecimal> theDecimals = new ArrayList<>(allGrades.size());
for (final Grade g : allGrades) {
theDecimals.add(g.getMark());
}
final Configuration config = Configuration.getDefault();
final int scale = config.getScale();
final RoundingMode roundingMode = config.getRoundingMode();
try {
final BigDecimal average = de.unibremen.st.gradelog.businesslogic.Math
.average(theDecimals, scale, roundingMode);
return average.stripTrailingZeros().toPlainString();
} catch (final ArithmeticException e) {
logger.debug(
"Calculation of grade average has been called without any grades.", e);
return NO_GRADES_PRESENT;
}
}
public String getGradeMedian() {
final List<BigDecimal> theDecimals = new ArrayList<>(allGrades.size());
for (final Grade g : allGrades) {
theDecimals.add(g.getMark());
}
try {
final BigDecimal median = de.unibremen.st.gradelog.businesslogic.Math
.median(theDecimals);
return median.stripTrailingZeros().toPlainString();
} catch (final ArithmeticException e) {
logger.debug(
"Calculation of grades median has been called without any grades.", e);
return NO_GRADES_PRESENT;
}
}
public String save() {
if (!isLoggedIn()) {
return null;
}
final User user = getSession().getUser();
grade.setUser(user);
user.addGrade(grade);
gradeDAO.save(grade);
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
init();
return null;
}
public String edit() {
if (!isLoggedIn()) {
return null;
}
assertNotNull(grade);
final User user = getSession().getUser();
gradeDAO.update(grade);
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
init();
return null;
}
public String remove(final Grade theGrade) {
if (!isLoggedIn()) {
return null;
}
assertNotNull(theGrade);
final User user = getSession().getUser();
user.removeGrade(theGrade);
gradeDAO.remove(theGrade);
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
init();
return null;
}
}`
g.mark and g.subject will already call the corresponding setters on your backing bean class (I assume that public setters getMark() and getSubject() exist).
Since action="#{gradesBean.edit(g)}" is resolved at the server (when the page is first displayed), it will have the original values , not the changed values. Anyway , to see the changed values you can use ajax (f:ajax tag and the corresponding listener attribute should be set). But you don't have to do this. Simply change your handler to
action="#{gradesBean.edit()}" //no argument
and get the latest values from your bean class instance.
I could be more specific if you want.
I have problem with 4 ifs in my code on jsf. I have to check 3 parameteres and when it is ok show appropriate message. Why its doesn't work? In dataGrid it works in c:when
<h:outputLabel value="#{msg.deleted}" class="delete" rendered="#{((!advertisement.isStarted) and (!advertisement.isActive) and (!advertisement.isFinished))}"/>
<h:outputLabel value="#{msg.done}" class="done" rendered="#{((advertisement.isStarted) and (!advertisement.isActive) and (advertisementisFinished))}"/>
<h:outputLabel value="#{msg.realize}" class="realize" rendered="#{((advertisement.isStarted) and (advertisement.isActive) and (!advertisement.isFinished))}"/>
<h:outputLabel value="#{msg.open}" rendered="#{((!advertisement.isStarted) and (advertisement.isActive) and (!advertisement.isFinished))}"/>
All time I dont understand solution of BalusC
I have:
public boolean isIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
public boolean isIsStarted() {
return isStarted;
}
public void setIsStarted(boolean isStarted) {
this.isStarted = isStarted;
}
public boolean isIsFinished() {
return isFinished;
}
public void setIsFinished(boolean isFinished) {
this.isFinished = isFinished;
}
I wanted to remove rows from the data table when the checkbox is ticked and remove button is pressed..
This is the datatable snippet :
<p:dataTable id="cartTable" lazy="true" scrollable="true"
scrollHeight="115" selection="#{Cart_Check.selectedItems}"
value="#{Cart_Check.cart}" var="cart" rowKey="#{cart.sheetno}"
style="widht:100%;margin-top:10%;margin-left:1%;margin-right:30px ;box-shadow: 10px 10px 25px #888888;">
<f:facet name="header">
Checkbox Based Selection
</f:facet>
<p:column selectionMode="multiple" style="width:2%">
</p:column>
//Here the columns are metion
<f:facet name="footer">
<p:commandButton id="viewButton" value="Remove" />
</f:facet>
</p:dataTable>
This is the backing bean
public class checkcart {
private int items;
private ArrayList<User_Cart> cart;
private ArrayList<User_Cart> selectedItems;
public checkcart() {
getvalues();
}
//getter and setter
public void getvalues() {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(false);
System.out.println("Cart Request ::::" + session.getAttribute("regid"));
try {
Connection connection = BO_Connector.getConnection();
String sql = "Select * from cart_orderinfo where usrregno=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, (String) session.getAttribute("regid"));
ResultSet rs = ps.executeQuery();
cart = new ArrayList<>();
while (rs.next()) {
User_Cart user_cart = new User_Cart();
user_cart.setSheetno(rs.getString("sheetno"));
user_cart.setState_cd(rs.getString("state_cd"));
user_cart.setDist_cd(rs.getString("dist_cd"));
user_cart.setLicensetype(rs.getString("license_type"));
user_cart.setFormat(rs.getString("sheet_format"));
user_cart.setQuantity(rs.getInt("quantity"));
cart.add(user_cart);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
and when i run this page i get the following error
datamodel must implement org.primefaces.model.selectabledatamodel when selection is enabled.
But when i remove the checkbox then their is no error but it is without a checkbox.
What to do and how to resolve the following error ..Kindly help..
I want something like this :
http://www.primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf
You just need to define ListDataModel as shown below,
public class SD_User_Cart extends ListDataModel<User_Cart> implements SelectableDataModel<User_Cart> {
public SD_User_Cart() {
}
public SD_User_Cart(List<User_Cart> data) {
super(data);
}
#Override
public User_Cart getRowData(String rowKey) {
//In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data
List<User_Cart> rows = (List<User_Cart>) getWrappedData();
for (User_Cart row : rows) {
if (row.getCartId.toString().equals(rowKey)) {//CartId is the primary key of your User_Cart
return row;
}
}
return null;
}
#Override
public Object getRowKey(User_Cart row) {
return row.get.getCartId();
}
}
Change your "cart" object into SD_User_Cart as shown below,
private SD_User_Cart cart;
Then define selection in p:datatable, and add a column as shown below,
<p:column selectionMode="multiple" style="width:18px"/>
Hope this helps:)
You need to define a your private ArrayList<User_Cart> selectedItems; data member in back class public class checkcart like this private User_Cart[] selectedItems; and give setter and getter method for the same it will work.
I had also faced same problem.
I'm trying to use an extendedDataTable, here is some piece of the code, the xhtml part:
<h:form>
<rich:extendedDataTable id="network_table" value="#{NetworkBean.availableNetworkList()}" var="network" selection="#{NetworkBean.selection}" selectionMode="single" >
<a4j:ajax execute="#form" event="selectionchange" listener="#{NetworkBean.selectionListener}"/>
<rich:column>
<f:facet name="header">Available Networks</f:facet>
<h:outputText value="#{network}"/>
</rich:column>
</rich:extendedDataTable>
<a4j:commandButton value="1" action="#{NetworkBean.availableMssList()}">
</a4j:commandButton >
</h:form>
and some part of the backing bean:
private Collection<Object> selection;
private List<String> selectionItems = new ArrayList<String>();
public void selectionListener(AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = dataTable.getRowKey();
selectionItems.clear();
for (Object selectionKey : selection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
System.out.print((String) dataTable.getRowData());
selectionItems.add((String) dataTable.getRowData());
System.out.print(selectionItems);
}
}
dataTable.setRowKey(originalKey);
}
public Collection<Object> getSelection() {
return selection;
}
/**
* #param selection the selection to set
*/
public void setSelection(Collection<Object> selection) {
this.selection = selection;
}
/**
* #return the selectionItems
*/
public String getSelectionItem() {
if (selectionItems == null || selectionItems.isEmpty()) {
return null;
}
return selectionItems.get(0);
}
public List<String> getSelectionItems() {
return selectionItems;
}
/**
* #param selectionItems the selectionItems to set
*/
public void setSelectionItems(List<String> selectionItems) {
this.selectionItems = selectionItems;
}
public void availableMssList(){
System.out.print("hithere");
System.out.println(selectionItems);
}
SO my problem is with the selectionItems, if i click on a row it works fine and prints me the selected row to the console(two printout in the Listener). But when i try to use the method availableMssList with the a4j commandbutton the selectionItems seems to be empty. What could be the problem?
Thanks for the help!
I'm trying to create a DataTable with Multiple Row Selection but i'm getting an error here's the link of the tutorial http://www.primefaces.org/showcase/ui/datatableRowSelectionMultiple.jsf :
Here's my xhtml:
<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}"
var="userObj"
selection="#
{projectAdminisrationMB.selectedUsers}"
selectionMode="multiple" rowIndexVar="rowIndex"binding="#{table2}">
<p:column id="column3">
<f:facet name="header">
<h:outputText value=" user "></h:outputText>
</f:facet>
<h:outputText value="#{userObj.name}"/>
/
<h:outputText value="#{userObj.lastName}"></h:outputText>
<h:outputText value="#{userObj.firstName}"></h:outputText>
</p:column>
<f:facet name="footer">
<p:commandButton id="addProjectUser" value=" Add " onclick="dlg1.show()" />
<p:commandButton id="deleteProjectUser" value=" Delete " />
</f:facet>
</p:dataTable>
Managed Bean :
#ManagedBean
#SessionScoped
public class ProjectAdminisrationMB implements Serializable {
private static final long serialVersionUID = 1L;
private String projectName;
private List <User> projectUsersList;
private List<User> projectNoUsersList;
private List<User> selectedUsers;
private String projectAdmin;
public ProjectAdminisrationMB() {
super();
AdministrationProjectFinal administrationProjectFinal =new
AdministrationProjectFinal();
this.projectUsersList=administrationProjectFinal.getUserList();
this.projectNoUsersList=administrationProjectFinal.getNotUserList();
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public List<User> getProjectUsersList() {
return projectUsersList;
}
public void setProjectUsersList(List<User> projectUsersList) {
this.projectUsersList = projectUsersList;
}
public String getProjectAdmin() {
return projectAdmin;
}
public void setProjectAdmin(String projectAdmin) {
this.projectAdmin = projectAdmin;
}
public List<User> getProjectNoUsersList() {
return projectNoUsersList;
}
public void setProjectNoUsersList(List<User> projectNoUsersList) {
this.projectNoUsersList = projectNoUsersList;
}
public List<User> getSelectedUsers() {
return selectedUsers;
}
public void setSelectedUsers(List<User> selectedUsers) {
this.selectedUsers = selectedUsers;
}
}
i'm getting this error:
javax.faces.FacesException: DataModel must implement
org.primefaces.model.SelectableDataModel when selection is enabled.....
just add this attribute rowKey to the datatable tag :
<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}"
var="userObj"
rowKey="#{userObj.name}"selection="#{projectAdminisrationMB.selectedUsers}"
selectionMode="multiple" rowIndexVar="rowIndex"
binding="#{table2}">
You can get this error if you try to add a new item to the underlying list and forget to assign a value to that new item's rowKey.
Alternatively to rowKey you can wrap your data in a custom model which really implements org.primefaces.model.SelectableDataModel. This is helpful if
all of your your classes have the same kind of #Id (e.g. a long) and can implement the same interface (e.g. EjbWithId)
you want to add additional functionalities to your data which are not domain specific and don't belong e.g. User.
The interface may be something like this:
public interface EjbWithId
{
public long getId();
public void setId(long id);
}
Then a generic implementation of SelectableDataModel for all your classes can be used:
public class PrimefacesEjbIdDataModel <T extends EjbWithId>
extends ListDataModel<T> implements SelectableDataModel<T>
{
public PrimefacesEjbIdDataModel(List<T> data)
{
super(data);
}
#Override public T getRowData(String rowKey)
{
List<T> list = (List<T>) getWrappedData();
for(T ejb : list)
{
if(ejb.getId()==(new Integer(rowKey))){return ejb;}
}
return null;
}
#Override public Object getRowKey(T item) {return item.getId();}
}
In your #ManagedBean:
private PrimefacesEjbIdDataModel<User> dmUser; //+getter
dmUser = new PrimefacesEjbIdDataModel<User>(administrationProjectFinal.getUserList());
first check whether you've added
rowKey="#{userObj.id}"
then you need to have the data table List set in filteredValue attribute of your data table in xhtml, instead of value.