使用p:dataTable替换单个和多个选择

时间:2015-04-08 09:26:20

标签: jsf-2 primefaces datatable selection

需要一些帮助才能知道我的解决方案是否有效。

我有一个带有单行选择的primefaces数据表,我需要添加一个多选复选框列...我的想法是切换选择模式点击一个按钮,将选择模式从单个切换到多个,反之亦然....

<pf:dataTable 
    value="${bean.notifications}" 
    var="notif" 
    selection="#{bean.isMultiple() ? consulterCorbeilleBean.selectedNotifs : consulterCorbeilleBean.selectedNotif}" 
    selectionMode="#{not bean.isMultiple() ? 'single' : ''}"
    rowKey="${notification.cle.idNotification}">

    <pf:ajax event="rowSelect" disabled="${bean.isMultiple()}"
            listener="${bean.function()}" update=":table:notificationTable" 
                    oncomplete="stopPropagationClick()" />

    <pf:column selectionMode="multiple" rendered="#{bean.isMultiple()}"/>

</pf:dataTable>

我的选择绑定有问题。我有这个错误:

  

集合操作的非法语法:   javax.el.PropertyNotWritableException:/index.xhtml @ 114,50   selection =“#{bean.isMultiple()?bean.selectedNotifs:   bean.selectedNotif}“

有什么想法可以解决吗?我使用的是Primefaces 3.2。

最诚挚的问候并感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

您无法在选择中使用动态设定值。

如果我是你,我会重新考虑设计,但如果你真的需要同一个表上的单个和多个选择选项,你可以使用2个数据表,一次只渲染其中一个,并在它们之间切换按钮单击。

答案 1 :(得分:0)

如果你真的需要它,那么你可以尝试这个工作示例

查看

<h:form id="form">
    <p:growl id="msgs" showDetail="true" for="basicDT" />

    <p:inputSwitch value="#{dtSelectionView.multiple}">
        <p:ajax listener="#{dtSelectionView.addMessage}" update="basicDT, msgs" />
    </p:inputSwitch>

    <p:dataTable 
        id="basicDT" 
        var="car" 
        value="#{dtSelectionView.cars1}"
        rowStyleClass="#{dtSelectionView.checkSelection(car)? 'ui-state-highlight':''}"
        >

        <f:facet name="header">
            DoubleSelect
        </f:facet>

        <p:column style="width:32px">
            <p:commandButton update="basicDT,:form:msgs,:form:carDetail,:form:multiCarDetail" icon="ui-icon-check"  actionListener="#{dtSelectionView.addSelection(car)}" />
        </p:column>   

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

        <p:column headerText="Year">
            <h:outputText value="#{car.year}" />
        </p:column>

        <p:column headerText="Brand">
            <h:outputText value="#{car.brand}" />
        </p:column>

        <p:column headerText="Color">
            <h:outputText value="#{car.color}" />
        </p:column>

    </p:dataTable>


    <p:dialog header="Car Info" widgetVar="carDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" closeOnEscape="true">
        <p:outputPanel id="carDetail" style="text-align:center;">
            <p:panelGrid  columns="2" rendered="#{not empty dtSelectionView.selectedCar}" columnClasses="label,value">
                <h:outputText value="Id:" />
                <h:outputText value="#{dtSelectionView.selectedCar.id}" />

                <h:outputText value="Year" />
                <h:outputText value="#{dtSelectionView.selectedCar.year}" />

                <h:outputText value="Color:" />
                <h:outputText value="#{dtSelectionView.selectedCar.color}" style="color:#{dtSelectionView.selectedCar.color}"/>

                <h:outputText value="Price" />
                <h:outputText value="$#{dtSelectionView.selectedCar.price}" />
            </p:panelGrid>
        </p:outputPanel>
    </p:dialog>

    <p:dialog header="Selected Cars" widgetVar="multiCarDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" width="200">
        <p:outputPanel id="multiCarDetail" style="text-align:center;">
            <ui:repeat value="#{dtSelectionView.selectedCars}" var="car">
                <h:outputText value="#{car.id} - #{car.brand}" style="display:block"/>
            </ui:repeat>
        </p:outputPanel>
    </p:dialog>

</h:form>

模型

import java.io.Serializable;

public class Car implements Serializable{

    private String Id;
    private String Brand;
    private int Year;
    private String Color;
    private int Price;
    private boolean SoldState;

    public Car(String id, String brand, int year, String color, int price,
        boolean soldState) {
        super();
        Id = id;
        Brand = brand;
        Year = year;
        Color = color;
        Price = price;
        SoldState = soldState;
    }

    @Override
    public boolean equals(Object o){
        if(o == null)                return false;
        if(!(o instanceof Car)) return false;

        Car other = (Car) o;
        if(! this.Id.equals(other.Id))      return false;
        if(! this.Brand.equals(other.Brand)) return false;
        if(this.Year != other.Year)   return false;
        if(! this.Color.equals(other.Color)) return false;
        if(this.Price != other.Price)   return false;
        if(this.SoldState != other.SoldState) return false;
        return true;
    }

    @Override
    public int hashCode(){
        return (int) Id.hashCode() * 
                Brand.hashCode() *
                Year *
                Color.hashCode() *
                Price *
                (SoldState ? 31 : 32)
                        ;
    }

  /** getters/setters */
}

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

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

@ManagedBean(name="dtSelectionView")
@ViewScoped
public class SelectionView implements Serializable {

    private List<Car> cars1;
    private Car selectedCar;
    private List<Car> selectedCars;

    private boolean multiple= false;

    @ManagedProperty("#{carService}")
    private CarService service;

    @PostConstruct
    public void init() {
        cars1 = service.createCars(10);/** random generated List. See PF showcase, datatable examples*/
        selectedCars = new ArrayList<Car>();
    }


    public boolean checkSelection(Car car){
        if(isMultiple())
            return selectedCars.contains(car) ? true : false;
        else
            return car.equals(selectedCar) ? true : false;
    }

    public void addSelection(Car car){
        String summary = "Car was ";
        if(isMultiple())
            if(selectedCars.contains(car)){
                selectedCars.remove(car);
                summary += "removed from list.";
            }
            else{
                selectedCars.add(car);
                summary += "added to list.";
                RequestContext.getCurrentInstance().execute("PF('multiCarDialog').show();");
            }
        else
            if(car.equals(selectedCar)){
                selectedCar = null;
                summary += "unselected.";
            }
            else{
                selectedCar = car;
                summary += "selected.";
                RequestContext.getCurrentInstance().execute("PF('carDialog').show();");
            }
            FacesContext.getCurrentInstance().addMessage("basicDT", new FacesMessage(summary));
    }

    public void setService(CarService service) {
        this.service = service;
    }

    public Car getSelectedCar() {
        return selectedCar;
    }

    public List<Car> getSelectedCars() {
        return selectedCars;
    }    

    public void onRowSelect(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Car Selected", ((Car) event.getObject()).getId());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowUnselect(UnselectEvent event) {
        FacesMessage msg = new FacesMessage("Car Unselected", ((Car) event.getObject()).getId());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void addMessage() {
        selectedCar = null;
        selectedCars = new ArrayList<Car>();
        String summary = multiple ? "Checked" : "Unchecked";
        FacesContext.getCurrentInstance().addMessage("basicDT", new FacesMessage(summary));
    }
    public List<Car> getCars1() {
        return cars1;
    }

    public void setCars1(List<Car> cars1) {
        this.cars1 = cars1;
    }

    public CarService getService() {
        return service;
    }

    public boolean isMultiple() {
        return multiple;
    }

    public void setMultiple(boolean multiple) {
        this.multiple = multiple;
    }
}

希望你朝着正确的方向前进。