Edit row data from richfaces datatable

喜你入骨 提交于 2019-12-13 04:45:57

问题


my application show the list of customer using richfaces datatable as show in image:

When user click the edit option an editable row must appear to edit the row data like in image:

my jsf page index.xhtml

<h:form>
    <rich:panel style="width:100%;" header="List of Customer" >

        <rich:dataTable style="width: 100%;" value="#{customerDataBaseQuery.customerDataModel}" var="c" rowClass="odd even" columnClasses=" c1,c2,c3,c4,c5,c6,c7"
          onrowmouseover="this.style.fontWeight='bold'" onrowmouseout="this.style.fontWeight='normal'">

            <rich:column>
            <f:facet name="header">First Name:</f:facet>
                    <h:inputText value="#{customerDataBaseQuery.customer.firstName}"  rendered="#{c.cusEditFlag}" />
            <h:outputText value="#{c.firstName}" rendered="#{not c.cusEditFlag}" />

        </rich:column>

        <rich:column>
            <f:facet name="header">Last Name:</f:facet>
                    <h:inputText value="#{customerDataBaseQuery.customer.firstName}"  rendered="#{c.cusEditFlag}" />
            <h:outputText value="#{c.lastName}" rendered="#{not c.cusEditFlag}" />

        </rich:column>

            <rich:column>
            <f:facet name="header">Address</f:facet>
                    <h:inputText value="#{customerDataBaseQuery.customer.address}"  rendered="#{c.cusEditFlag}" />
            <h:outputText value="#{c.address}" rendered="#{not c.cusEditFlag}" />

        </rich:column>
            <rich:column>
            <f:facet name="header">Phone No:</f:facet>
                    <h:inputText value="#{customerDataBaseQuery.customer.phone}"  rendered="#{c.cusEditFlag}" />
            <h:outputText value="#{c.phone}" rendered="#{not c.cusEditFlag}" />

        </rich:column>

        <rich:column>
            <f:facet name="header">View Record:</f:facet>
                    <h:commandLink id="view" value="View" action="#{customerDataBaseQuery.assignCustId(c.id,c.firstName,c.lastName)}" rendered="#{not c.cusEditFlag}" styleClass="lk"/>


        </rich:column>
            <rich:column>
            <f:facet name="header">Edit Record:</f:facet>
                    <h:commandLink id="Edit" value="Edit"  styleClass="lk"  action="#{customerDataBaseQuery.cusEditAction(c)}" rendered="#{not c.cusEditFlag}" />

                    <h:commandLink id="Save" value="Save" styleClass="lk" action="#{customerDataBaseQuery.cusSaveAction(c)}" rendered="#{c.cusEditFlag}"/>


        </rich:column>
            <rich:column>
            <f:facet name="header">Remove Record:</f:facet>
                    <h:commandLink id="Remove" value="Remove"  styleClass="lk" action="#{customerDataBaseQuery.deleteCustomer(c.id)}" rendered="#{not c.cusEditFlag}"/>


        </rich:column>

    </rich:dataTable>


</rich:panel>
</h:form>

My Bean class CustomerDatabaseQuery.java

@ManagedBean()
@SessionScoped

public class CustomerDataBaseQuery {
private DataModel customerDataModel;
private DataModel creditDataModel;

private Customer customer;
private Items items;
private CustomerHelper customerHelper;
private int custID;
private String fName;
private String lName;

public CustomerDataBaseQuery(){
customer=new Customer();
items=new Items();
customerHelper=new CustomerHelper();

}
public String assignCustId(int ID,String f,String l){
    custID=ID;
    fName=f;
    lName=l;
    return "View";
}
public void cusSaveAction(Customer c){
    c.setCusEditFlag(false);
}
public String cusEditAction(Customer c){
    c.setCusEditFlag(true);
    return null;
}
public void itemSaveAction(Items i){
    i.setItemEditFlag(false);
}
public void itemEditAction(Items i){
    i.setItemEditFlag(true);
}
public String getFName() {
    return fName;
}

public void setFName(String fName) {
    this.fName = fName;
}

public String getLName() {
    return lName;
}

public void setLName(String lName) {
    this.lName = lName;
}

public void setCustId(int custID){
    this.custID=custID;
}
public int getCustID(){
    return custID;
}

public Customer getCustomer() {
    return customer;
}

public void setCustomer(Customer customer) {
    this.customer = customer;
}

public DataModel getCustomerDataModel(){
    customerDataModel = new ListDataModel(customerHelper.getCustomerRecord());
    return customerDataModel;
}

public /*String*/ void insertCustomer(){
      /*String returnString =*/ customerHelper.saveCustomerRecord(customer);
      //return returnString;
}
public void deleteCustomer(int ID){
    customerHelper.deleteCustomerRecord(ID);
}
public Items getItems() {
    return items;
}

public void setItems(Items items) {
    this.items = items;
}

public DataModel getCreditDataModel(){
    creditDataModel = new ListDataModel(customerHelper.getCreditRecord(custID));
    return creditDataModel;
}

public /*String*/ void insertCreditRecord(){
    items.setCustomerId(custID);
    /*String returnString =*/ customerHelper.saveCreditRecord(items);
    //return returnString;
}
public void deleteCredit(int ID){
    customerHelper.deleteCreditRecord(ID);
}
}

At first <h:outputText> will render as the value of variable cusEditFlag is false. When edit command link is clicked then the value of variable cusEditFlag is set true. After setting its value true <h:inputText> must render but it doesn't happen. I also look the value of variable doing debug while clicking the edit command link the value of variable changes to true but also it doesn't work. I learn the similar article for here only difference is he is using jsf datatable and I am using richfaces datatable. Does the dsf datatable and richfaces datatable has differences doing such tasks? or I am doing mistake can anyone figure it out.


回答1:


Post is ancient, but for the posterity:

My guess is that your problem lies in getCustomerDataModel(). When you click your edit link it correctly sets the edit value to true on the selected object in your datamodel. After that you return null, resulting in the current page being reloaded. During this reload the table will render again, thus calling getCustomerDataModel(). This method creates a whole new DataModel based on what I assume to be the original state of the customer record. Basically, it throws away the edit flag you just set by giving you a fresh clean DataModel.

The effect is that you never see your changes, regardless of which dataTable you use (RichFaces of JSF). The example you quote simply does not have this problem because it uses static intialization of the data list - the list never changes, only the objects do.

So: Try to initialise your DataModel in some init-Method (on Bean creation or similar) and only deliver it in the getter. Then the changed edit flag on the customer object should show up in the list.



来源:https://stackoverflow.com/questions/19448044/edit-row-data-from-richfaces-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!