Search code examples
viewjsf-2customizationprimefacestabbed

Primefaces tabView: embed text near title attribute of p:tab


Perhaps,somebody faced such a problem,looks like specific one and concerning Primefaces tabView component.
There is tabbed view comments module and I would like to display comments number within tab,like this:

enter image description here

How is it possible to embed text if <p:tab> generated dynamically and contains a lot of HTML sub-elements (ul , li etc.)?

Thank you for help.


Solution

  • You can add output in the tab title.

    <p:tab title="Comments #{myBean.number}">
    </p:tab>
    

    Bean would look something like this:

    @ManagedBean
    @ViewScoped
    public class MyBean implements Serializable {
    
      private int number;
    
      public MyBean() {
        this.number = 5;
      }
    
      public int getNumber() {
        return number;
      }
    
      public void setNumber(int number) {
        this.number = number;
      }
    
      public void addComment() {
        setNumber(number + 1);        
      }
    
    }
    

    enter image description here

    Update Comments total with ajax:

    <h:form>                                
      <p:tabView id="tabs">
        <p:tab title="Comments #{myBean.number}" >
          <p:commandButton value="Add Comment" 
               action="#{myBean.addComment}" update="tabs"/>
        </p:tab>
        <p:tab title="tab" ></p:tab>
      </p:tabView>
    </h:form>
    

    I couldn't get update to work correctly on the tab level but updating the entire tabView works.