简体   繁体   English

Ajax update =“:#{table.clientId}”似乎不起作用<p:dataTable>

[英]Ajax update=“:#{table.clientId}” does not seem to work on <p:dataTable>

I have some problems with PrimeFaces 5.2 and client components update mechanism PrimeFaces 5.2和客户端组件更新机制存在一些问题

I created a web page with a button that triggers a server side method. 我用触发服务器端方法的按钮创建了一个网页。 This method update a List of objects and finally a datatable will be updated with new contents of the List. 此方法更新对象列表,最后将使用列表的新内容更新数据表。

<h:form> 
        ...
        <p:commandButton value="Add parameters" action="#{adminBean.addParams}"  
                         update=":growl :#{tblSensors.clientId}"  />
</h:form>

<h:form>
    <p:dataTable id="tblSensors"  
            var="sensorElem" value="#{adminBean.sensors}"
            binding="#{tblSensors}"
        >  
            ...
        </p:dataTable>
</h:form>

Unfortunatelly when the procedure callback is over, my data table are not updated .. Sincerlly I'm not able to find the problem. 不幸的是,当过程回调结束时,我的数据表没有更新..诚然,我无法找到问题。

There are no exception in debug phase and I saw nothing in Response data from Firebug. 在调试阶段也没有例外,在Firebug的Response数据中我什么也没看到。

UPDATE: I saw that when the bean method is called and some variable has been changed; 更新:我看到当调用bean方法并且某些变量已经改变时; this changes aren't persistent and they are missed every time the method is called 这种更改不是持久性的,每次调用该方法时都会丢失它们

UPDATE 2: as requested, I add the bean code: 更新2:根据要求,我添加bean代码:

@ManagedBean(name="adminBean")
@SessionScoped
public class AdminBean implements Serializable{
    private List<Sensor> sensors;


    public List<Sensor> getSensors() {
        return sensors;
    }

    public void setSensors(List<Sensor> sensors) {
        this.sensors = sensors;
    }


    //...




 @PostConstruct
    public void init(){
       //...
        //initialize sensor list
        this.sensors = new ArrayList<Sensor>();

        //...
    }



    //method for adding new sensor
    public void addParams(){


        try{


        //add new sensor element to the list 
        this.sensors.add(new Sensor(this.param_name,Double.parseDouble(this.min_val),Double.parseDouble(this.max_val), 
                this.unit, this.selectedChartType, this.selectedFieldType, id_counter++));





        }catch(Exception e){

            System.out.println("Error "+e.getMessage());
            System.out.println("stack "+e.getStackTrace());
        }

        FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_INFO,"Successful", "New sensor added!")); 
    }

    //...

}

UPDATE 3 : fix some namespace errors. 更新3 :修复一些名称空间错误。

althouh我不熟悉带有数据绑定的dataTables,我认为update-statement不正确,触发commandButton时需要更新datatable

update=":growl, :tblSensors"

Why not just specify the form id so that updating child elements will be more straightforward? 为什么不只指定表单ID,以使更新子元素更直接? try: 尝试:

<h:form> 
        ...
        <p:commandButton value="Add parameters" action="#{amminBean.addParams}"  
                         update=":growl :datatableform:tblSensors"  />
</h:form>

<h:form id="datatableform">
    <p:dataTable id="tblSensors"  
            var="sensorElem" value="#{aminBean.sensors}"
            binding="#{tblSensors}">  
            ...
        </p:dataTable>
</h:form>

I resolve my issue changing the implementation philosophy: I divide the tab contents in 2 separate xhtml files with related beans, the first one for "parent" element management and the second one for its child elements. 我解决了改变实现原理的问题:我将选项卡内容分为2个单独的xhtml文件以及相关的Bean,第一个用于“父”元素管理,第二个用于其子元素。

so, the first xhtml: 因此,第一个xhtml:

 <h:form> 
     <p:panel id="panel" header="New Station" style="margin-bottom:10px;">
          <h:panelGrid columns="2" cellpadding="5">
          <p:inputText id="stationName" required="true" value="#{adminBean.stationName}"/>
          <p:inputText id="description" required="true" value="#{adminBean.description}" />

          <p:outputLabel value="Station type" for="stationType" />
              <p:selectOneMenu id="stationType" value="#{adminBean.selectedStationType}" >
                  <f:selectItem itemLabel="Select Type" itemValue="-1" noSelectionOption="false" />
                  <f:selectItems value="#{adminBean.stationType}" />
             </p:selectOneMenu>
         </h:panelGrid>
     </p:panel>


    <p:commandButton value="Cancel" action="#{adminBean.cancella}" icon="ui-icon-check"  />

    <p:commandButton value="Next >" id="nextId" binding="#{nextId}"  action="#{adminBean.nextStep}" 
                    icon="ui-icon-check" update=":growl" />
 </h:form>

when user selects the Next Button, a new page will be called and its bean will use the bean related with first page in order to retrieve all needed info and show its metadata correctly. 当用户选择“下一步”按钮时,将调用一个新页面,其Bean将使用与第一页相关的Bean,以便检索所有所需的信息并正确显示其元数据。

this is the second page: 这是第二页:

<h:form> 
   <p:panel id="panel2" header="Aggiungi sensori" style="margin-bottom:10px;">
      <h:panelGrid columns="2" cellpadding="5">


          <p:inputText id="param_name" required="true" value="#{adminSensorsBean.param_name}"/>


          <p:selectOneMenu id="chartType" value="#{adminSensorsBean.selectedChartType}" >
                   <f:selectItem itemLabel="Select Type" itemValue="-1" noSelectionOption="false" />
                   <f:selectItem itemLabel="Line" itemValue="LINE" noSelectionOption="false" />
                   <f:selectItem itemLabel="Bar" itemValue="BAR" noSelectionOption="false" />
                </p:selectOneMenu>
                //...

                <p:selectOneMenu id="fieldType" binding="#{fieldType}" value="#{adminSensorsBean.selectedFieldType}" >
                    <f:selectItem itemLabel="Select Field" itemValue="-1" noSelectionOption="false" />
                    <f:selectItems value="#{adminSensorsBean.fieldsType}" />
                </p:selectOneMenu>

            </h:panelGrid>

            </p:panel>
            <p:commandButton value="Add Sensor" id="addSensorId" binding="#{addSensorId}" 
                             action="#{adminSensorsBean.addSensor}" 
                             icon="ui-icon-check" update=":growl :#{tblSensors.clientId}" />
        </h:form>

        <h:form>
            <p:dataTable id="tblSensors"  
                var="sensorElem" 
                value="#{adminSensorsBean.sensors}"
                scrollable="true"
                rowKey="#{sensorElem.id}"
                binding="#{tblSensors}"
                scrollHeight="150"
            >  
               //...
               //this table will be filled with data from form above
               //...

            </p:dataTable>
        </h:form>


        //...
        <h:form> 
           <p:commandButton value="Submit" id="submitId" binding="#{submitId}" action="#{adminSensorsBean.saveStation}" 
                    icon="ui-icon-check" update=":growl" />
        </h:form>

through the Add Sensor button new information will be inserted into the tableView by the called bean method. 通过“添加传感器”按钮,新信息将通过调用的bean方法插入到tableView中。

@ManagedBean(name="adminSensorsBean")
@SessionScoped
public class AdminSensorsBean implements Serializable{

//Managed property from bean of first page
@ManagedProperty(value="#{adminBean}")
    private AdminBean adminBean;

 //...

@PostConstruct
public void init(){
    //Here all second page elements as FieldType list, will be initialized using stored information from managed property.

}

//...

} }

In this way everything works fine! 这样,一切正常! Honestly I don't known why update event didn't work when I putted all my code in only one jsf page..is it a PrimeFaces/JSF bug? 老实说,当我将所有代码放在一个jsf页面中时,我不知道为什么更新事件不起作用..它是PrimeFaces / JSF错误吗? maybe but I don't sure. 也许,但我不确定。 Thank you for the support, and sorry for my rough english 谢谢您的支持,也很抱歉我的英语水平

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM