2

I went through this question from SO How to use <h:selectBooleanCheckbox> in <h:dataTable> to select multiple rows?

Using the single checkbox as shown in above question i want to find out whether i can make h:datatable cell editable so that user can edit all the rows and columns at once and submit

Here is part of bean class

public class bean {
private List<Group> GroupList;

private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();

public void setChecked(Map<Long, Boolean> checked) {
    this.checked = checked;
}

public Map<Long, Boolean> getChecked() {
    return checked;
}


}

And here is my JSF page

<h:dataTable id="editTable" styleClass = "listtable" value="#{bean.GroupList}"  var="group" border="1" first="0" rows="8" width="75%" frame="hsides" rules="all" cellpadding="5" headerClass="tableheading" rowClasses="firstrow, secondrow">

    <f:facet name="header">
    <h:outputText value="Groups"></h:outputText>
    </f:facet>

    <h:column>
        <f:facet name="header">
        <h:outputText value="GroupId"></h:outputText>
        </f:facet>
        <h:outputText value="#{group.Id}" rendered=""></h:outputText>
        <h:inputText value="#{group.Id}" rendered=""/>
    </h:column>

    <h:column>
        <f:facet name="header">
        <h:outputText value="GroupName"></h:outputText>
        </f:facet>
        <h:outputText value="#{group.Name}" rendered=""></h:outputText>
        <h:inputText value="#{group.Name}" rendered=""/>
    </h:column>


    <h:column>
        <f:facet name="header">
        <h:outputText value="Check to Enable/Disable"></h:outputText>
        </f:facet>
        <h:selectBooleanCheckbox value="#{bean.checked[group.Id]}" />
    </h:column>

    </h:dataTable>

What should be kept in rendered attribute so that when it is checked h:inputtext is rendered and when not checked h:outputtext is rendered?

Community
  • 1
  • 1
Sreeram
  • 3,160
  • 6
  • 33
  • 44

1 Answers1

1

Just bind to the same property. It returns a Boolean anyway. You can use ! or not to negate it.

<h:outputText value="#{group.Id}" rendered="#{!bean.checked[group.Id]}" />
<h:inputText value="#{group.Id}" rendered="#{bean.checked[group.Id]}" />
...
<h:outputText value="#{group.Name}" rendered="#{!bean.checked[group.Id]}" />
<h:inputText value="#{group.Name}" rendered="#{bean.checked[group.Id]}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC-Thanks for the response.but nothing is happening when i do it.i tried with ! and not also. – Sreeram Sep 21 '11 at 14:39
  • You must submit the form after checking the checkbox. Your bean must be in session (or view) scope instead of request scope. – BalusC Sep 21 '11 at 14:44
  • No i didnt submit the form.Yes my bean is session scoped.i should make it request scoped? – Sreeram Sep 21 '11 at 14:46
  • You must submit the form to get the checkbox values to be applied and the page to be re-rendered. Session scope is fine (it's only a bit too broad, the bean is now shared between all other browser tabs/windows which may lead to unexpected results when interacting with the same page in multiple windows/tabs, I'd prefer Tomahawk's ``). – BalusC Sep 21 '11 at 14:49
  • if i make like this `` Is it correct? – Sreeram Sep 21 '11 at 14:52
  • You can do so. It will only also submit all other inputs in the same form which might lead to fighting with validation like `required="true"`. If you were using JSF 2.x, it would be much easier/better with ajax powers. – BalusC Sep 21 '11 at 14:54
  • I am using only JSF 1.2.What is the better way to handle it? – Sreeram Sep 21 '11 at 14:59
  • Upgrade to JSF 2 :) Or look for an ajax-flavored component library like RichFaces/Ajax4jsf. – BalusC Sep 21 '11 at 15:02
  • Ok.Out of all the implementations of JSF.which one is the best one? – Sreeram Sep 21 '11 at 15:07
  • There are only two major JSF implementations: Mojarra and MyFaces. Choosing should be based on its robustness. But switching between JSF implementations should be relatively transparent and easy as it's just a matter of replacing the JAR files and changing some implementation-specific context parameters, if any. I myself are using Mojarra 2.1.3 which is a JSF 2.1 implementation which requires a Servlet 3.0 container (Tomcat 7, Glassfish 3, etc). But if you're still using a Servlet 2.5 container (Tomcat 6, Glassfish 2, etc), then you should use JSF 2.0, not JSF 2.1. – BalusC Sep 21 '11 at 15:14