在h:dataTable中动态复制行

时间:2020-12-27 20:11:42

I have a data table

我有一个数据表

<h:dataTable value="#{vendor.vh.currentVendorVO.vfms}" var="row">

In VendorVO

private VFM[] vfms;
public VFM[] getVfms() {
    return vfms;
}
public void setVfms(VFM[] vfms) {
    this.vfms = vfms;
}

In VFM

private String orderTypeId;
private String fulfillTypeId;
private int orderSeq;
private String lastUpdated;
private String lastUpdatedBy;
private boolean lastItem;
private String action = "none";

I would like to duplicate the <h:dataTable> row when a button is clicked.

我想在单击按钮时复制 行。

How can I achieve this?

我怎样才能做到这一点?

1 个解决方案

#1


Use a dynamically expansible ArrayList instead of a fixed size array [].

使用动态可扩展的ArrayList而不是固定大小的数组[]。

private List<VFM> vfms; // +getter (setter is unnecessary)

Then, it's just a matter of letting the button invoke add() method on it with new VFM instance.

然后,只需让按钮使用新的VFM实例调用add()方法即可。

<h:commandButton value="Add" action="#{bean.addVfm}" />
public void addVfm() {
    vfms.add(new VFM());
}

If you intend to have this button on every row which copies a new VFM instance, then just pass it along and add a copy constructor.

如果您打算在复制新VFM实例的每一行上都有此按钮,则只需将其传递并添加复制构造函数即可。

<h:commandButton value="Copy" action="#{bean.copyVfm(row)}" />
public void copyVfm(VFM vfm) {
    vfms.add(new VFM(vfm));
}
public VFM(VFM vfm) {
    orderTypeId = vfm.orderTypeId;
    fulfillTypeId = vfm.fulfillTypeId;
    orderSeq = vfm.orderSeq;
    lastUpdated = vfm.lastUpdated;
    lastUpdatedBy = vfm.lastUpdatedBy;
    lastItem = vfm.lastItem; 
    action = vfm.action;
}

#1


Use a dynamically expansible ArrayList instead of a fixed size array [].

使用动态可扩展的ArrayList而不是固定大小的数组[]。

private List<VFM> vfms; // +getter (setter is unnecessary)

Then, it's just a matter of letting the button invoke add() method on it with new VFM instance.

然后,只需让按钮使用新的VFM实例调用add()方法即可。

<h:commandButton value="Add" action="#{bean.addVfm}" />
public void addVfm() {
    vfms.add(new VFM());
}

If you intend to have this button on every row which copies a new VFM instance, then just pass it along and add a copy constructor.

如果您打算在复制新VFM实例的每一行上都有此按钮,则只需将其传递并添加复制构造函数即可。

<h:commandButton value="Copy" action="#{bean.copyVfm(row)}" />
public void copyVfm(VFM vfm) {
    vfms.add(new VFM(vfm));
}
public VFM(VFM vfm) {
    orderTypeId = vfm.orderTypeId;
    fulfillTypeId = vfm.fulfillTypeId;
    orderSeq = vfm.orderSeq;
    lastUpdated = vfm.lastUpdated;
    lastUpdatedBy = vfm.lastUpdatedBy;
    lastItem = vfm.lastItem; 
    action = vfm.action;
}