Primefaces calendar component show only month and year

后端 未结 12 1893
南笙
南笙 2020-12-14 03:25

primefaces calendar component is getting Date by default. Can I get only month and year without dates.

How to get only month and year

12条回答
  •  青春惊慌失措
    2020-12-14 03:43

    Utilized this feature from the Primefaces Documentation:

    Another handy event is the viewChange that is fired when month and year changes. An instance of org.primefaces.event.DateViewChangeEvent is passed to the event listener providing the current month and year information.

    Made the following code: XHTML

    
        
            FROM:
            
                
                    
                
            
            TO:
            
                
                    
                
            
            
                
            
        
    
    

    CSS

    .monthYearOnly .ui-datepicker-calendar{
        display: none;
    }
    

    Bean

    public void updateFrom(){ 
    
        Map requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        LOGGER.info("before: " + from);
        Calendar c = Calendar.getInstance();
        c.setTime(from);
        c.set(Calendar.MONTH, Integer.parseInt(requestParams.get("form:from_month")) - 1);
        c.set(Calendar.YEAR, Integer.parseInt(requestParams.get("form:from_year")));
        from = c.getTime();
    
        LOGGER.info("after: " + from);
    }
    
    public void updateTo(){ 
    
        Map requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        LOGGER.info("before: " + to);
        Calendar c = Calendar.getInstance();
        c.setTime(to);
        c.set(Calendar.MONTH, Integer.parseInt(requestParams.get("form:to_month")) - 1);
        c.set(Calendar.YEAR, Integer.parseInt(requestParams.get("form:to_year"))); 
            c.set(Calendar.DATE,c.getActualMaximum(Calendar.DAY_OF_MONTH)); 
            to = c.getTime();
    
        Calendar now = Calendar.getInstance();
        if(c.after(now)){
            to = now.getTime();
        } 
        LOGGER.info("after: " + to);
    }
    

    Basically the ajax trigger when month or year is changed is the main feature here. Unfortunately, this is a bit of a work around since I can't retrieve the org.primefaces.event.DateViewChangeEvent. This seems to be a bug and I posted a report on the Primefaces Forum just now. Will update this post when I get some feedback there.

    Hope it helps.

提交回复
热议问题