我正在使用JSF 2.2
和PrimeFaces 6.1
。我想创建一个DataTable
,其中最后一行始终是添加新行。
例如,假设我有2个实体Purchase
和Article
,这两个实体与多对多关系相关。在Purchase的视图中,我会有一个文章列表(数据表),在启动表时为空,我希望空表包含一行添加新文章,这个"添加行"应始终在那里添加新文章。
文章表有3列:名称,金额和价格。
购买实体是:
@Entity
@Table(name="purchase")
public class Purchase implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int numberArticle;
@Temporal(TemporalType.DATE)
private Date datePurchase;
@ManyToMany
@JoinTable(name = "join_purchase_article",
joinColumns = @JoinColumn(name = "purchase_id"),
inverseJoinColumns = @JoinColumn(name = "article_id"))
private List<Article> listArticles = new ArrayList();
}
文章的实体:
@Entity
@Table(name="article")
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int price;
private int amount;
@ManyToMany(mappedBy = "listArticles")
private List<Purchase> listPurchase = new ArrayList();
}
我的XHTML页面如下:
<h:form>
<p:panel>
<p:dataTable id="idTable" value="#{addPurchaseController.listArticlePurchase}" var="article">
<p:column headerText="Name">
<h:outputText value="#{article.name}"/>
</p:column>
<p:column headerText="Amount">
<h:outputText value="#{article.amount}"/>
</p:column>
<p:column headerText="Price">
<h:outputText value="#{article.price}"/>
</p:column>
</p:dataTable>
</a:panel>
</h:form>
Managed Bean:
@ManagedBean(name = "addPurchaseController")
@ViewScoped
public class AddPurchaseController implements Serializable {
private List<Article> listArticlePurchase;
@PostConstruct
public void init() {
initialisation();
}
private void initialisation() {
listArticlePurchase= new ArrayList();
}
}