/* * Copyright 2009-2014 PrimeTek. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.primefaces.showcase.view.data.datatable; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ViewScoped; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import org.primefaces.showcase.domain.Car; import org.primefaces.showcase.service.CarService; @ManagedBean(name="dtColumnsView") @ViewScoped public class ColumnsView implements Serializable { private final static List<String> VALID_COLUMN_KEYS = Arrays.asList("id", "brand", "year", "color", "price"); private String columnTemplate = "id brand year"; private List<ColumnModel> columns; private List<Car> cars; private List<Car> filteredCars; @ManagedProperty("#{carService}") private CarService service; @PostConstruct public void init() { cars = service.createCars(10); createDynamicColumns(); } public List<Car> getCars() { return cars; } public List<Car> getFilteredCars() { return filteredCars; } public void setFilteredCars(List<Car> filteredCars) { this.filteredCars = filteredCars; } public void setService(CarService service) { this.service = service; } public String getColumnTemplate() { return columnTemplate; } public void setColumnTemplate(String columnTemplate) { this.columnTemplate = columnTemplate; } public List<ColumnModel> getColumns() { return columns; } private void createDynamicColumns() { String[] columnKeys = columnTemplate.split(" "); columns = new ArrayList<ColumnModel>(); for(String columnKey : columnKeys) { String key = columnKey.trim(); if(VALID_COLUMN_KEYS.contains(key)) { columns.add(new ColumnModel(columnKey.toUpperCase(), columnKey)); } } } public void updateColumns() { //reset table state UIComponent table = FacesContext.getCurrentInstance().getViewRoot().findComponent(":form:cars"); table.setValueExpression("sortBy", null); //update columns createDynamicColumns(); } static public class ColumnModel implements Serializable { private String header; private String property; public ColumnModel(String header, String property) { this.header = header; this.property = property; } public String getHeader() { return header; } public String getProperty() { return property; } } }