从Richfaces数据表中编辑行数据 [英] Edit row data from richfaces datatable

查看:62
本文介绍了从Richfaces数据表中编辑行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用richfaces数据表显示客户列表,如下图所示:

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

当用户单击 edit 选项时,必须出现可编辑行以编辑行数据,如图像中所示:

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

我的jsf页面索引.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);
}
}

起初 将呈现,因为变量cusEditFlag的值为false。单击编辑命令链接时,变量cusEditFlag的值设置为true。将其值设置为true后,必须呈现< h:inputText> ,但不会发生。在单击编辑命令链接时,我还查看了变量进行调试的值,变量的值更改为true,但它不起作用。我在此处上学习了类似的文章唯一的区别是他使用的是jsf数据表,而我使用的是richfaces数据表。 dsf数据表和richfaces数据表在执行此类任务时是否有所不同?

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.

推荐答案

帖子很古老,但后代很重要:

Post is ancient, but for the posterity:

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

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.

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

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.

所以:尝试初始化您的DataModel采用某种init-Method(关于Bean创建或类似方法),并且仅在getter中传递它。然后,客户对象上已更改的编辑标志应显示在列表中。

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.

这篇关于从Richfaces数据表中编辑行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆