Show only month and year in Primefaces Legend

Asked

Viewed 3,171 times

4

Like remover os dias of the ?

I tried the configuration below, but the days are displayed when the component is redeveloped.

<p:calendar id="dataTeste" pattern="MM/yyyy" />

Here’s the rendered component image:

inserir a descrição da imagem aqui

2 answers

6


The Primefaces component does not natively support this.

Options would be:

  • Customize the component (as in this SOEN response).
  • Using a third-party plugin like monthPicker (indicated in the question cited), applying it in a native text box of Primefaces.
  • Build your own JSF component (maybe using the previous item plugin).

However, unless the visual issue is something much needed, perhaps a simpler solution is recommended. Simply use a combo with the possible date range, each item containing mês/ano. Or even two combos, one with month and one with year.

Particularly, I see no advantage as a user in having to press arrows to advance or back the month as it takes more work if I want to go to a date further away.

4

Old topic, but I’ll leave an alternative solution that uses javascript and CSS to solve this problem...

OBS: Tested in Primefaces 5.2...

function setData(){
  ano = $("#data .ui-datepicker-year").val();
  mes = $("#data .ui-datepicker-month").val();
  $('#data_input').val(('0' + (++mes)).slice(-2)+'/'+ano);	  
}

$( "#data .ui-datepicker-month" ).change(function() {
  setData();   
});

$( "#daa .ui-datepicker-year" ).change(function() {
  setData();   
});

.ui-datepicker-inline .ui-datepicker-calendar{
  display: none;        
}
<p:calendar id="data" value="#{bean.data}" mode="inline" navigator="true" pattern="MM/yyyy"/>   

Upshot:

Resultado do código

  • 1

    Interesting alternative! Easier than creating the combos separately. Just take care of 3 things: (1) in practice you should not overwrite all calendars as in the example; (2) test the code at each Primefaces upgrade to check for changes that break the code; (3) beware of manipulating the object Date because it will come with some "random" day, so if the back end make some kind of manipulation on that date may incur unexpected results.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.