【问题标题】:Unable to update p:datatable cell after ajax call on another cell's cellEdit event在对另一个单元格的 cellEdit 事件进行 ajax 调用后无法更新 p:datatable 单元格
【发布时间】:2015-12-10 10:02:41
【问题描述】:

在我的支持 bean 中的单元格编辑事件中,如果单元格值发生更改,我想更新另一列的值以及数据表的值。该值已在我的列表对象中正确设置,但未反映在我的数据表中。我也尝试使用 RequestContext 使用数据表进行更新,但列表仍然没有刷新。我究竟做错了什么?我应该为此更改调用任何行事件吗?

代码 sn-p:

豆子

@Named("editView")
@ViewScoped

public class EditTableView implements Serializable {


    private List<Cars> carList;
    private List<String> brands;

    @PostConstruct
    public void init() {

        brands = new ArrayList<String>();
        brands.add("BMW");
        brands.add( "Mercedes");
        brands.add("Volvo");
        brands.add("Audi");
        brands.add("Renault");
        brands.add("Fiat");
        brands.add("Volkswagen");
        brands.add("Honda");
        brands.add("Jaguar");
        brands.add( "Ford");
        brands.add("Maruti Suzuki");

        carList = new ArrayList<Cars>();
        modifiedList = new ArrayList<Cars>();
        Cars car =new Cars();
        car.setCarId(1);
        car.setBrand("BMW");
        car.setColor("Grey");
        car.setModelNumber("2323");
        car.setSelfDriven("Y");
        car.setYear("1980");
        car.setStatus("");

        carList.add(car);               
    }




    public void onCellEdit(CellEditEvent event) {

        int rowIndex = event.getRowIndex();
        Cars car = null;
        LOGGER.info("Car Edited"+rowIndex);

        Object oldValue = event.getOldValue();
        Object newValue = event.getNewValue();

        DataTable carTable = (DataTable)   event.getSource();
        String index = carTable.getClientId(FacesContext.getCurrentInstance());
        LOGGER.info(index);

        if(newValue != null && !newValue.equals(oldValue)) {
            LOGGER.info("Cell Changed Old: " + oldValue + ", New:" + newValue);
            car=carList.get(rowIndex);    
            carList.get(event.getRowIndex()).setStatus("Changed");      
            modifiedList.add(car);
        }
    }    
}

xhtml

<h:form id="cellEditForm">

    <p:dataTable id="cars" var="car" value="#{editView.carList}"
        editable="true" editMode="cell" widgetVar="cellCars">
        <f:facet name="header">Cell Editing</f:facet>

        <p:ajax event="cellEdit" listener="#{editView.onCellEdit}"
            update=":cellEditForm:cars" />

        <p:column headerText="Id">
            #{car.carId}
        </p:column>

        <p:column headerText="Model Name" style="background-color:yellow">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{car.modelNumber}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText id="modelInput" value="#{car.modelNumber}"
                        style="width:96%" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Year" style="background-color:yellow">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{car.year}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{car.year}" style="width:96%" label="Year" />
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Brand" style="background-color:yellow">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{car.brand}" />
                </f:facet>
                <f:facet name="input">
                    <h:selectOneMenu value="#{car.brand}" style="width:100%">
                        <f:selectItems value="#{editView.brands}" var="man"
                            itemLabel="#{man}" itemValue="#{man}" />
                    </h:selectOneMenu>
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Self Driven" style="background-color:yellow">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{car.selfDriven}" />
                </f:facet>
                <f:facet name="input">
                    <p:inplace>
                        <p:inputText value="#{car.selfDriven}" />
                    </p:inplace>
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Color" style="background-color:yellow">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{car.color}" />
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{car.color}" style="width:96%" label="Color" />
                </f:facet>
            </p:cellEditor>
        </p:column>


        <p:column>
            <h:outputText value="#{car.status}" id="statusLabel"></h:outputText>

        </p:column>
    </p:dataTable>

</h:form>

【问题讨论】:

  • 下次请格式化代码更好。
  • 尝试 update="cars" 而不是 update=":cellEditForm:cars"
  • 这不一样,为什么?
  • @Kukeltje 我实际上需要 primefaces 或核心 jsf 库中的解决方案。我无法将 Omnifaces 合并到我的项目中。不过感谢您的解决方案:)

标签: ajax jsf primefaces datatable


【解决方案1】:

我最终使用了 jquery。获取更新行的索引并从我的 bean 中调用 jquery 函数。

RequestContext requestContext = RequestContext.getCurrentInstance();
String call = "updateCell(" + event.getRowIndex() + ")";
requestContext.execute(call);

jQuery 代码

function updateCell(i) {
   var index =  "cellEditForm:cars:"+i+":statusLabel";  
   document.getElementById(index).innerHTML="Changed";
}

【讨论】:

  • 这不是以 JSF 方式“更新”它。它只是用静态值替换页面的内容。如果你想这样做,至少也要返回相关bean中属性的值。
  • 顺便说一句,这是更新一个单元格,而不是一行,所以请更正问题
【解决方案2】:

OmniFaces 有一个很好的工具,叫做'Ajax'

关于这个的 StackOverflow 帖子展示了如何使用它:How to use OmniFaces Ajax.updateColumn() or Ajax.updateRow() via p:ajax

【讨论】:

  • 如果合适的话,我接受反对票,但反对票的人(或其他人)能否说明为什么这不是目前问题的一个好答案(除了 OP 的评论外,OmniFaces 不能使用)
【解决方案3】:

更新表格而不是表格或将表格放入面板并更新面板。

为了安全

<h:form id="cellEditForm">
<p:panel id="something" styleclass="pero">
   <p:dataTable id="cars

update="@(.pero)"

请务必将 id 放在面板上,因为类更新会搜索该 id 并使用它。这个解决方案是因为我不知道其余的代码,它适用于任何代码。

【讨论】:

  • 我尝试了上面建议的所有更改,但没有给出预期的结果。在使用 update=cars 时,清除表单并仅在表单上显示修改后的值。我现在已经粘贴了我的整个代码以供参考。
  • String updatedIndex = carTable.getClientId(FacesContext.getCurrentInstance()) + ":" + event.getRowIndex() +":statusLabel"; RequestContext.getCurrentInstance().update(updatedIndex); 没有更新单元格
  • 所以把 stylclass 放到你不想更新的每个元素上。原理是一样的。
猜你喜欢
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多