【问题标题】:Alternate single and multiple selection with p:dataTable使用 p:dataTable 交替单选和多选
【发布时间】:2015-06-13 04:24:27
【问题描述】:

需要一些帮助来了解我的解决方案是否有效。

我有一个单行选择的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。

最好的问候和感谢您的帮助:)

【问题讨论】:

    标签: jsf-2 primefaces datatable selection


    【解决方案1】:

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

    查看

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

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

    【讨论】:

      【解决方案2】:

      您不能在选择中使用动态设置器值。

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

      【讨论】:

      • 我达到了更改 selectionMode 属性,但 selection 属性似乎不可编辑...
      猜你喜欢
      • 2015-06-21
      • 2015-02-14
      • 1970-01-01
      • 2017-06-20
      • 2012-04-10
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2020-11-13
      相关资源
      最近更新 更多