2

primefaces 库的复选框数据表对我不起作用,我尝试制作与 primefaces 展示相同的代码,但是当我检查一些行并单击查看按钮时 selectedCars[] 包含 0 行:这是我的 xhtml页 :

    <?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>TODO supply a title</title>
    </h:head>
    <h:body>
        <h:form id="form"> 
            <p:dataTable id="multiCars" var="car" value="#{fortest.mediumCarsModel}" paginator="true" rows="10"  
                         selection="#{fortest.selectedCars}">  

                <f:facet name="header">  
                    Checkbox Based Selection  
                </f:facet>  

                <p:column selectionMode="multiple" style="width:18px" />  

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

                <p:column headerText="date envoi">  
                    #{car.dateEnvoi}  
                </p:column>  

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

                <f:facet name="footer">  
                    <p:commandButton id="multiViewButton" value="View" icon="ui-icon-search"  
                                     update=":form:displayMulti" oncomplete="multiCarDialog.show()"/>  
                </f:facet>  
            </p:dataTable>  

            <p:dialog id="multiDialog" header="Car Detail" widgetVar="multiCarDialog"  
                      height="300" showEffect="fade" hideEffect="explode">  

                <p:dataList id="displayMulti"  
                            value="#{fortest.selectedCars}" var="selectedCar">  
                    id : #{selectedCar.id}, dateEnvoi #{selectedCar.dateEnvoi}  
                </p:dataList>  

            </p:dialog>  
        </h:form>
    </h:body>
</html>

这是我的托管豆:

    @ManagedBean
@SessionScoped
public class fortest implements Serializable {

    private Commande[] selectedCars;
    private dortestDataModel mediumCarsModel; 
    utilisateursHelper uh;
    /**
     * Creates a new instance of fortest
     */
    public fortest() {
        uh = new utilisateursHelper();
        mediumCarsModel = new dortestDataModel(uh.getAllCommandes());

    }

    public void setSelectedCars(Commande[] selectedCars) {
        System.out.println("alors je suis donc la size : "+selectedCars.length);
        this.selectedCars = selectedCars;
    }

    public Commande[] getSelectedCars() {
        return selectedCars;
    }

    public dortestDataModel getMediumCarsModel() {
        return mediumCarsModel;
    }

}

这是我的数据模型:

   public class dortestDataModel extends ListDataModel<Commande> implements SelectableDataModel<Commande> {    

    utilisateursHelper uh;

    public dortestDataModel() {  
    }  

    public dortestDataModel(List<Commande> data) {  
        super(data);  
        uh = new utilisateursHelper();
    }  

    @Override  
    public Commande getRowData(String rowKey) {  
        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  


        List<Commande> cars = (List<Commande>) uh.getAllCommandes();  

        for(Commande car : cars) {  
            if(car.getId().equals(rowKey))  
                return car;  
        }  

        return null;  
    }  

    @Override  
    public Object getRowKey(Commande car) {  
        return car.getId();  
    }  
}  

你有什么想法吗,提前谢谢你

4

2 回答 2

4

您忘记像这样将 rowCheckListener 附加到您的数据表

  <p:ajax event="rowSelectCheckbox" listener="#{forTest.check}"   />  

下面的代码对我有用。我可以在对话框中看到 id 和决定。

XHTML:我实现了selectableDataModel,我只是使用了 datatable 的 rowKey 属性,你也可以使用它

<h:form id="form"> 
    <p:dataTable id="multiCars" var="car" value="#{forTest.carList}" paginator="true" rows="10"  
                  rowKey ="#{car.id}" selection="#{forTest.selectedCars}" >  
      <p:ajax event="rowSelectCheckbox" listener="#{forTest.check}"   />  
        <f:facet name="header">  
            Checkbox Based Selection  
        </f:facet>  

        <p:column selectionMode="multiple" style="width:18px" />  

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

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

        <f:facet name="footer">  
            <p:commandButton id="multiViewButton" value="View" icon="ui-icon-search"  
                             update=":form:displayMulti" oncomplete="multiCarDialog.show()"/>  
        </f:facet>  
    </p:dataTable>  

    <p:dialog id="multiDialog" header="Car Detail" widgetVar="multiCarDialog"  
              height="300" showEffect="fade" hideEffect="explode">  

        <p:dataList id="displayMulti"  
                    value="#{forTest.selectedCars}" var="selectedCar">  
            id : #{selectedCar.id}, dateEnvoi #{selectedCar.decision}  
        </p:dataList>  

    </p:dialog>  
</h:form>

托管豆

package managedbeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.jws.Oneway;

import org.primefaces.event.SelectEvent;

import beans.Car;



    @ManagedBean(name="forTest")
    @SessionScoped
    public class forTest implements Serializable {
         private List<Car> carList = new ArrayList<Car>();
         private Car selectedCars[];

        public Car[] getSelectedCars() {
            return selectedCars;
        }

        public void setSelectedCars(Car selectedCars[]) {
            this.selectedCars = selectedCars;
        }

        public List<Car> getCarList() {
            if(carList.size()==0) {
                setCarList();
            }
            return carList;
        }

        public void setCarList() {
            this.carList.add(new Car(1, "car1"));
            this.carList.add(new Car(2, "car2"));
            this.carList.add(new Car(3, "car3"));
        }
        public void check(SelectEvent event) {
            System.out.println("in check");
        }


    }
于 2012-09-29T08:34:15.570 回答
0

删除这个:

<p:dialog id="multiDialog" header="Car Detail" widgetVar="multiCarDialog"  
          height="300" showEffect="fade" hideEffect="explode">  

    <p:dataList id="displayMulti"  
                value="#{forTest.selectedCars}" var="selectedCar">  
        id : #{selectedCar.id}, dateEnvoi #{selectedCar.decision}  
    </p:dataList>  

</p:dialog>
于 2012-10-26T13:55:53.613 回答