FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled

前端 未结 3 504
广开言路
广开言路 2020-12-09 13:59

I am trying to learn PF so I started with displaying datatable first and navigating to next page on rowClick passing parameters, but got stuck with the following error. I fo

相关标签:
3条回答
  • 2020-12-09 14:17

    It worked for my project using a composite key for rowkey e.g.: rowKey="#{course.getCompositeKey()

    Since I didn't have a compositeKey variable I simply created one in my Course Class (in your case the Car Class). The 2 primary keys are strings so I just said this.compositeKey=this.courseNumber+this.product--you just use whatever 2 primary keys are in your Car class instead of courseNumber and product.

    | |
    0 讨论(0)
  • 2020-12-09 14:19

    The #{tableBean.cars} must implement SelectableDataModel if you don't specify the rowKey attribute on the <p:dataTable>.

    You have however implemented it on the #{tableBean} instead. This is not right. You can find correct examples in the PrimeFaces showcase. Basically, your TableBean class must look like this according to the showcase code example:

    @ManagedBean
    @ViewScoped
    public class TableBean implements Serializable {
    
        private List<Car> cars;
        private Car car;
        private CarDataModel carsModel;
    
        public TableBean() {
            cars = new ArrayList<Car>();
            // Populate cars here and thus NOT in the getter method!
            carsModel = new CarDataModel(cars);
        }
    
        // ...
    }
    

    Where the CarDataModel look like this (again, just copied from PrimeFaces showcase code example):

    public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car> {  
    
        public CarDataModel() {
        }
    
        public CarDataModel(List<Car> data) {
            super(data);
        }
    
        @Override
        public Car getRowData(String rowKey) {
            List<Car> cars = (List<Car>) getWrappedData();
    
            for(Car car : cars) {
                if(car.getModel().equals(rowKey))
                    return car;
            }
    
            return null;
        }
    
        @Override
        public Object getRowKey(Car car) {
            return car.getModel();
        }
    
    }
    

    Finally use #{tableBean.carsModel} instead of #{tableBean.cars} as value of <p:dataTable>. Exactly as in the showcase example.

    <p:dataTable value="#{tableBean.carsModel}" var="car" ... />
    

    Another, easier, way is to just specify the rowKey attribute on the <p:dataTable>.

    <p:dataTable value="#{tableBean.cars}" var="car" rowKey="#{car.model}" ... />
    

    This way you don't need the whole SelectableDataModel. You only need to ensure that it's never null and always unique across the rows. See also DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled, but I have already defined rowKey.

    0 讨论(0)
  • 2020-12-09 14:22

    Ensure to set the rowKey parameter in the bean method that populates the "value=.." of the datatable. Mine returned this error because the rowKey was null.

    0 讨论(0)
提交回复
热议问题