编辑richfaces数据表中的行数据

时间:2013-10-18 11:00:39

标签: java jsf datatable richfaces

我的应用程序显示使用richfaces datatable的客户列表,如图所示: enter image description here

当用户点击修改选项时,必须出现一个可编辑的行,以便像在图片中一样编辑行数据: enter image description here

我的jsf页 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>

我的Bean类 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);
}
}

首先<h:outputText>将呈现变量cusEditFlag的值为false。单击编辑命令链接时,变量cusEditFlag的值设置为true。设置其值后,<h:inputText>必须呈现,但不会发生。我也查看变量的值,在单击编辑命令链接时执行调试,变量值变为true,但它也不起作用。我学习here的类似文章,唯一不同的是他使用的是jsf数据表,我使用的是richfaces数据表。 dsf datatable和richfaces数据表是否在执行此类任务时存在差异?或者我犯了错误,任何人都可以搞清楚。

1 个答案:

答案 0 :(得分:0)

邮政是古老的,但对于后代:

我的猜测是你的问题在于getCustomerDataModel()。单击编辑链接时,它会在数据模型中的所选对象上将编辑值正确设置为true。之后,您返回null,导致重新加载当前页面。在重新加载期间,表将再次呈现,从而调用getCustomerDataModel()。此方法基于我假设的客户记录的原始状态创建一个全新的DataModel。基本上,它会通过为您提供一个全新的DataModel来抛弃您刚设置的编辑标记。

无论您使用哪种dataTable(JSF的RichFaces),效果都是您永远不会看到您的更改。您引用的示例根本没有此问题,因为它使用数据列表的静态初始化 - 列表永远不会更改,只有对象才会更改。

所以:尝试在一些init-Method(在Bean创建或类似)上初始化你的DataModel,并且只在getter中传递它。然后,客户对象上更改的编辑标志应显示在列表中。