Different colors of options in selectOneMenu (Primefaces)

前端 未结 1 961
囚心锁ツ
囚心锁ツ 2020-12-06 06:37

I need to display different colors of options in Primefaces.

I have a selectOneMenu with dynamical items (List)



        
1条回答
  •  时光说笑
    2020-12-06 07:12

    The solution is to use the 'advanced' way of displaying this in PrimeFaces 4.0 and newer.

    You can combine f:selectItems tag with p:column tags for p:selectOneMenu (see the showcase), with an iteration var for the columns themselves, like you do in tables.

    Then the ideal thing would be to set the styleClass to the entire column depending on the condition, but unfortunatelly it doesn't work. Luckily, adding some Javascript/jQuery code you can achieve your goal, check out this SSCCE:

    XHTML Page

    
    
        
    
    
        
            
            
                
                
                    
                
            
        
        
    
    
    

    Bean.java

    @ManagedBean
    @ViewScoped
    public class Bean implements Serializable {
    
        public class Car implements Serializable {
            String name;
            boolean sold;
    
            public Car(String name, boolean sold) {
                this.name = name;
                this.sold = sold;
            }
    
            public String getName() {
                return name;
            }
    
            public boolean isSold() {
                return sold;
            }
        }
    
        private List allCars = Arrays.asList(new Car("Audi", true), new Car(
                "Mercedes", false));
    
        public List getAllCars() {
            return allCars;
        }
    
        private Car selectedCar;
    
        public Car getSelectedCar() {
            return selectedCar;
        }
    
        public void setSelectedCar(Car selectedCar) {
            this.selectedCar = selectedCar;
        }
    
        public void send() {
            System.out.println("Sent " + selectedCar.name);
        }
    
    }
    

    You might also be interested in setting only the font color and not the background:

    
    
        
    
    
        
            
            
                
                
                    
                
            
        
    
    
    

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