primefaces:如何在删除一行后正确刷新dataTable

时间:2015-03-21 20:11:17

标签: primefaces datatable

在我的dataTable中删除一行后,我仍然需要手动刷新浏览器以查看更改。你可以告诉我如何在删除数据后正确地更新我的dataTable吗?

<h:body>

<h:form id="form">
    <p:growl id="msgs" showDetail="true" />

    <p:dataTable id="items" var="items" value="#{itemView.items}"
        editable="true" editMode="cell" widgetVar="cellItems">
        <f:facet name="header">
       Cell Editing with Click and RightClick
   </f:facet>

        <p:ajax event="cellEdit" listener="#{itemView.onCellEdit}"
            update=":form:msgs" />

        <p:column headerText="Name">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{items.name}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{items.name}" style="width:96%" label="Name" />
                </f:facet>
            </p:cellEditor>
        </p:column>

         ...next columns

        <p:column headerText="Options" style="width:32px">

            <p:commandLink id="deleteLink" styleClass="ui-icon ui-icon-trash" action="#{itemView.deleteItem(items.itemId)}" update=":form:items" ajax="true">
                <p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
            </p:commandLink>

            <p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
                <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
                <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
             </p:confirmDialog>

        </p:column>                 

    </p:dataTable>

    <p:growl id="growl" life="2000" />
    <p:commandButton value="Add Item" id="ajax" update="msgs items" actionListener="#{itemView.buttonAction}" styleClass="ui-priority-primary" />

</h:form>

这是我的控制器

public class ItemView implements Serializable {

private List<Item> items;

@ManagedProperty("#{itemService}")
private ItemService serviceItem;

@PostConstruct
public void init() {
    items = serviceItem.getAllItems();      
}    

public List<Item> getItems(){
    return items;
}

public List<String> getUseOrNotCriticalCount(){
    return serviceItem.getUseOrNotCriticalCount();
}

public void setServiceItem(ItemService serviceItem){
    this.serviceItem = serviceItem;
}

public void onRowEdit(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Item Edited");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onRowCancel(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Edit Cancelled");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onCellEdit(CellEditEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();

    if(newValue != null && !newValue.equals(oldValue)) {

        DataTable dataTable = (DataTable) event.getSource();
        Item editedItem = (Item) dataTable.getRowData();
        serviceItem.edit(editedItem);
        System.out.println(editedItem.getName().toString());

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

public void buttonAction(ActionEvent actionEvent) {
    Item item = new Item("Nazov", 0,0,0,false,0,0,0);
    serviceItem.add(item);
    addMessage("New item has been added!");
}

public void addMessage(String summary) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary,  null);
    FacesContext.getCurrentInstance().addMessage(null, message);
}

public void deleteItem(int itemId){
    serviceItem.delete(itemId);

    addMessage("Selected item has been deleted!");
}

}

1 个答案:

答案 0 :(得分:0)

看起来您正在页面上正确更新,但您也需要更新bean。因此,在deleteItem(int itemId)中,可以手动从items列表中删除已删除的项目,也可以调用init()进行刷新。