从bean触发/激活RowEditor以进行主要的In-Cell编辑启用p:dataTable

jim*_*ndy 9 primefaces jsf-2

我有一个启用p:dataTableInCell编辑的表面,并且想要为新添加的行触发/激活RowEditor.

XHTML的摘录

<p:commandButton id="btnAddEntry" value="Add new row" actionListener="#{myBean.addNewCar}" ... update="carTable growl" process="@this carTable ..."/>

<p:dataTable id="carTable" var="car" value="#{myBean.cars}" ... editable="true">  
        <p:column ...>  
            <p:cellEditor>
                ...
            </p:cellEditor>  
        </p:column>
    ...
        <p:column ...>  
                <p:rowEditor />  
        </p:column>
    ...         
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)

这是我目前为bean方法所做的:

public void addNewCar() {

    Car newCar = new Car();
    cars.add(newCar);

    FacesContext facesContext = FacesContext.getCurrentInstance();
    UIComponent uiTable = ComponentUtils.findComponent(facesContext.getViewRoot(), "carTable");
    DataTable table = (DataTable) uiTable;

    final AjaxBehavior behavior = new AjaxBehavior();    
    RowEditEvent rowEditEvent = new RowEditEvent(uiTable, behavior, table.getRowData());
    rowEditEvent.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
    table.broadcast(rowEditEvent);
}
Run Code Online (Sandbox Code Playgroud)

我不知道

  1. 如果这是正确的方法
  2. 如果是,则将哪个对象RowEditEvent(UIComponent component, Behavior behavior, Object object)作为第3个参数传递给构造函数

Gna*_*raz 10

如果facelet中只有一个数据表,请尝试使用它

oncomplete="jQuery('.ui-datatable-data tr').last().find('span.ui-icon-pencil').each(function(){jQuery(this).click()});
Run Code Online (Sandbox Code Playgroud)

将其添加到命令按钮.这应该工作.


Luk*_* Z. 5

我在create方法中使用了RequestContext类的execute(..)方法.这允许我首先计算行的索引以进入编辑模式并将其动态地包含在javascript中:

RequestContext.getCurrentInstance().execute("jQuery('span.ui-icon-pencil').eq(" + rowToEditIndex + ").each(function(){jQuery(this).click()});");
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.