复选框数据表格不工作 [英] checkbox datatable primefaces doesn't working

查看:152
本文介绍了复选框数据表格不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

primetable库的checkbox数据库不适用于我,我试图使同样的代码作为展示的原始表,但当我检查一些行,我点击视图按钮selectedCars []包含0行:
这里是我的xhtml页面:

the checkbox datatable of primefaces library doesn't work for me, I tried to make the same code as the showcase of primefaces but when I check some rows and I click on the view button the selectedCars[] contains 0 rows : here is my xhtml page :

    <?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>

这里是我的托管Bean:

and here is my managed-Bean :

    @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;
    }

}

这里是我的dataModel: / p>

and here is my dataModel :

   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();  
    }  
}  

你有什么想法, / p>

do you have any idea, thank you in advance

推荐答案

您忘记将rowCheckListener附加到您的datatable中

you forgot to attach rowCheckListener to your datatable like this

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

以下代码适用于我。我可以在对话框中看到ID和决定。

below code is working for me. i am able to see the id and decision on the dialog.

XHTML
i dint实现 selectableDataModel ,我只是使用了 datKate的rowKey属性,你也可以使用

XHTML: i dint implement selectableDataModel, i just used rowKey attribute of datatable, you could use that as well

<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>

ManagedBean

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");
        }


    }

这篇关于复选框数据表格不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆