简体   繁体   English

PrimeFaces DataTable在pojo中使用自动完成功能进行编辑

[英]PrimeFaces DataTable editing with autocomplete in pojo

I have a page with a datatable which have to display various informations about Mailbox objects. 我有一个带有数据表的页面,它必须显示有关邮箱对象的各种信息。 One of these informations is the owner of the Mailbox which is stored by its id in Mailbox object. 其中一个信息是邮箱的所有者,它的ID存储在Mailbox对象中。 In output I solved this with a method in backing bean that retrieve the username by the mailbox object. 在输出中,我通过支持bean的方法解决了这个问题,该方法通过邮箱对象检索用户名。 In input I thought to use autocomplete with pojo but I can't exactly realize how do this. 在输入中我认为使用自动完成与pojo但我不能完全意识到这是怎么做的。

My jsf page: 我的jsf页面:

<p:dataTable id="dataTable" value="#{bean.mailboxes}" var="m" editable="true">
  <!-- other table -->
  <p:column headerText="Owner">
    <p:cellEditor>
      <f:facet name="output">  
        <h:outputText value="#{bean.userByMailbox(m)}" />  
      </f:facet>
      <f:facet name="input">
        <!-- here comes autocomplete -->
      </f:facet>  
    </p:cellEditor>
  </p:column>
</p:dataTable>

And my bean: 我的豆子:

public class Bean {

// Other properties and methods

List<Mailbox> mailboxes;

public List<Mailbox> getMailboxes() {
    if (mailboxes == null) {
        Query q = em.createNamedQuery("Mailbox.findAll");
        mailboxes = q.getResultList();
    }

    return mailboxes;
}

public User getUserByMailbox(Mailbox m) {
    Query q = em.createNamedQuery("User.findByUsrId");
    q.setParameter("usrId", m.getUsrId());
    return (User)q.getSingleResult();
}
}

Thank you all! 谢谢你们!

Your model is wrong. 你的模型错了。

In Mailbox , replace Mailbox ,替换

@Column
private Long usrId;

by 通过

@ManyToOne
@JoinColumn(name="usrId")
private User user;

This way you can just use #{m.user} instead of #{bean.userByMailbox(m)} . 这样您就可以使用#{m.user}而不是#{bean.userByMailbox(m)} This way the property is also writable (perhaps you actually got a PropertyNotWritableException while attempting to use this EL expression in <p:autoComplete value> ; in the future questions tell that so instead of asking an overly generic question). 这样,该属性也是可写的(也许你在尝试在<p:autoComplete value>使用这个EL表达式时实际上得到了一个PropertyNotWritableException ;在将来的问题中告诉那样,而不是问一个过于通用的问题)。

Note that this concrete problem has essentially nothing to do with JSF nor <p:autoComplete> . 请注意,这个具体问题基本上与JSF和<p:autoComplete>

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

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