Need to pass the current tab name to the backing managed bean from dynamically generated tab

1.6k views Asked by At

I am using primefaces. And my requirement is to have number of tabs which will be generated based on a list specified in the backing bean. Now the second criteria is that if the tab changes the content under that tab should also changes. So I kept the onChange event and tried to get the value through event.getTab().getTitle(), but it is returning null to the backing bean.

<p:tabView id="tabView" binding="#{dndProductsView.tabView}">
        <p:ajax event="tabChange" listener="#{dndProductsView.onTabChange}"/>
</p:tabView>

Managed Bean required codes are as :-

@PostConstruct
public void init() {
    user = SessionBean.getUserName();
    categorylist = categoryLogics.findAllOrderedByCategoryName();
    productList = productLogics.findAllOrderedByProductName();
    droppedProducts = new ArrayList<Product>();
}

private TabView tabView;

@Inject
private FacesContext facesContext;

public void setTabView(TabView tabView) {
    this.tabView = tabView;
}

public TabView getTabView() {
    tabView = new TabView();
    for (Category c : categorylist) {
        Tab t = new Tab();
        t.setTitle(c.getCategoryName());
        tabView.getChildren().add(t);
    }
    return tabView;
}

public void onTabChange(TabChangeEvent event) {
    String titleName =  event.getTab().getTitle();
    System.out.println("" + titleName);
}

The tab is getting generated properly and when the tab changes, the onTabChange() method is called but event.getTab().getTitle() returns null.

1

There are 1 answers

5
Dijana Cukic On BEST ANSWER
Integer index=(Integer)tabview.getActiveIndex();

It is not a name but it is index of activ tab witch in this case is the one you are interested in. Index starts from 0 being the first tab :)