简体   繁体   English

ah:dataTable中的当前元素

[英]Current element in a h:dataTable

So I have the following code: 所以我有以下代码:

<h:dataTable value="#{findRepairsBean.repairs}" var="r"
    styleClass="repair-table"
    headerClass="repair-table-header"
    rowClasses="repair-table-odd-row,repair-table-even-row">                       
    <h:column>
        <f:facet name="header">Product</f:facet>
        #{r.item.productName}
    </h:column>
    <h:column>
        <f:facet name="header">Brand</f:facet>
        #{r.item.brand}
    </h:column>
    <h:column>
        <f:facet name="header">Category</f:facet>
        #{r.item.category}
    </h:column>
    <h:column>
    <!-- column header -->
    <f:facet name="header">Defect</f:facet>
    <!-- row record -->
    #{r.details.defect}
    </h:column>

    <h:column>
    <f:facet name="header">Description</f:facet>
    #{r.details.description}
    </h:column> 
    <h:column>
    <f:facet name="header">Status</f:facet>
    #{r.details.status}
    </h:column>
    <h:column>
         <h:outputLabel for="price" value="Price: "/>
         <h:inputText id="price" value="#{findRepairsBean.price}"/>
         <h:message for="price" styleClass="error" /> 
    </h:column>
    <h:column>
         <h:commandButton styleClass="mybutton" 
            value="Place Bid" action="#{findRepairsBean.placeBid(???)}"/>
    </h:column>
</h:dataTable>

This is my bean: 这是我的豆子:

@Component
@Scope("session")
public class FindRepairsBean implements Serializable{
    @Autowired
    private RepairService repairService;

    @Autowired
    private UserService userService;

    private String keyWord;
    private int price;
    private List<Repair> repairs;
    //private Repair repair;

    @Inject
    private UserBean userBean;

    public void findRepairs(){
        repairs = repairService.findRepairsByKeyword(keyWord);
    }

    public void placeBid(Repair repair){
        try {
            Repairer repairer = (Repairer) userService.getUser(userBean.getUsername());
            repairService.placeBid(repairer, repair, price);
        } catch(UserServiceException ex){
            Logger.getLogger(TestRepairCafe.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(ex.getLocalizedMessage())); // Add global message
        }
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public List<Repair> getRepairs() {
        return repairs;
    }

    public void setRepairs(List<Repair> repairs) {
        this.repairs = repairs;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

And this is the model of Repair: 这是修复的模型:

public class Repair implements Comparable<Repair>, Serializable
{
    private static int repairCounter = 0;
    private final int repairId;
    private Client client;    
    private Repairer repairer;    
    private RepairDetails details;
    private Item item;        
    private final List<Bid> bids;        

    private Repair()
    {
        this.repairId = repairCounter++;
        this.bids = new ArrayList<>();                
    }

    public Repair(Item item, RepairDetails repDetails)
    {
        this();
        this.item = item;
        this.details = repDetails;
    }

    public void addBid(Bid bid)
    {
        this.bids.add(bid);
    }

    public void addClient(Client client)
    {
        this.client = client;
    }

    public void setRepairer(Repairer repairer)
    {
        this.repairer = repairer;
    }

    public int getRepairId()
    {
        return repairId;
    }

    public List<Bid> getBids()
    {
        return bids;
    }

    public RepairDetails getDetails()
    {
        return details;
    }

    public Client getClient()
    {
        return client;
    }

    public Repairer getRepairer()
    {
        return repairer;
    }

    public Item getItem()
    {
        return item;
    }

    public void setDetails(RepairDetails details)
    {
        this.details = details;
    }

    public void setItem(Item item)
    {
        this.item = item;
    }

    @Override
    public int compareTo(Repair o)
    {
        return this.details.getSubmitDate().compareTo(o.getDetails().getSubmitDate());
    }

    @Override
    public String toString()
    {
        return String.format("%s -%s", this.getDetails().getDescription(), item.getProductName());
    }

}

Now, in this xhtml page I am making a datatable of all repairs which contain a keyword, which is working perfectly. 现在,在此xhtml页面中,我正在制作一个包含关键字的所有维修的数据表,该表运行良好。 However the placeBid() method in my bean is supposed to place a bid on a Repair, only I do not know how to give the current Repair in the table to the placeBid() method. 但是我的bean中的placeBid()方法应该对Repair出价,只是我不知道如何将表中的当前Repair交给placeBid()方法。 I would expect I could just do it with #{r}, however this is not compiling. 我希望我可以使用#{r}来做到这一点,但是这还没有编译。 I want to store each repair in the list somehow so I can acces it in my method for each row in the table. 我想以某种方式将每个修复存储在列表中,以便可以在我的方法中为表中的每一行访问它。

Did you try with this syntax? 您尝试使用这种语法吗?

<h:commandButton styleClass="mybutton"  
   value="Place Bid" action="#{findRepairsBean.placeBid(r)}"/>

According to this answer this should work if using the correct EL version (2.2), see here another example. 根据此答案,如果使用正确的EL版本(2.2),这应该可以工作,请参见此处的另一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM