Primefaces tabview tabChange event fired after displaying tab

前端 未结 3 1187
南方客
南方客 2021-01-14 03:31

I am working on a project using Primefaces 3.5 & JSF2.1 (Mojarra)

I have created a primefaces wit

相关标签:
3条回答
  • 2021-01-14 03:59

    It work fine in Primefaces 3.5 and mojarra 2.1.20, my code: bean:

    @ManagedBean(name = "tabview")
    @ViewScoped    
    public class TabView implements Serializable {
    
            private static final long serialVersionUID = 1L;
            private List<Car> l1;
            private List<Car> l2;
            private List<Car> l3;
    
            public TabView() {
                l1 = new ArrayList<Car>();
                l2 = new ArrayList<Car>();
                l3 = new ArrayList<Car>();
    
                Car c1 = new Car("c1", "a1");
                Car c2 = new Car("c21", "a21");
                Car c3 = new Car("c31", "a31");
                Car c4 = new Car("c41", "a41");
                Car c5 = new Car("c51", "a51");
                Car c6 = new Car("c61", "a61");
                Car c7 = new Car("c71", "a71");
                Car c8 = new Car("c81", "a81");
                Car c9 = new Car("c91", "a91");
                l1.add(c1);
                l1.add(c2);
                l1.add(c3);
    
                l2.add(c4);
                l2.add(c5);
                l2.add(c6);
    
                l3.add(c7);
                l3.add(c8);
                l3.add(c9);
            }
    
            public void hand(TabChangeEvent event) {
                Car c1 = new Car("c1", "a1");
                Car c2 = new Car("c1", "a1");
                Car c3 = new Car("c1", "a1");
                Car c4 = new Car("c1", "a1");
                Car c5 = new Car("c1", "a1");
                Car c6 = new Car("c1", "a1");
                Car c7 = new Car("c1", "a1");
                Car c8 = new Car("c1", "a1");
                Car c9 = new Car("c1", "a1");
                l1.add(c1);
                l1.add(c2);
                l1.add(c3);
    
                l2.add(c4);
                l2.add(c5);
                l2.add(c6);
    
                l3.add(c7);
                l3.add(c8);
                l3.add(c9);
            }
    
            public List<Car> getL1() {
                return l1;
            }
    
            public void setL1(List<Car> l1) {
                this.l1 = l1;
            }
    
            public List<Car> getL2() {
                return l2;
            }
    
            public void setL2(List<Car> l2) {
                this.l2 = l2;
            }
    
            public List<Car> getL3() {
                return l3;
            }
    
            public void setL3(List<Car> l3) {
                this.l3 = l3;
            }
        }
    

    xhtml:

      <p:tabView id="mtrlDtlTabs" dynamic="true" cache="false">  
                        <p:ajax event="tabChange" update="t1 t2 t3" listener="#{tabview.hand}"/>
                        <p:tab id="t1" title="1">
                            <p:dataTable id="l1" value="#{tabview.l1}" var="l1">
                                <p:column headerText="l1">#{l1.manufacturer}</p:column>
                                <p:column headerText="l2">#{l1.model}</p:column>
                            </p:dataTable>
                        </p:tab>
                        <p:tab id="t2" title="2">
                            <p:dataTable id="l2" value="#{tabview.l2}" var="l2">
                                <p:column headerText="l2">#{l2.manufacturer}</p:column>
                                <p:column headerText="l22">#{l2.model}</p:column>
                            </p:dataTable>
                        </p:tab>
                        <p:tab id="t3" title="3">
                            <p:dataTable id="l3" value="#{tabview.l3}" var="l3">
                                <p:column headerText="l3">#{l3.manufacturer}</p:column>
                                <p:column headerText="l33">#{l3.model}</p:column>
                            </p:dataTable>
                        </p:tab>
                    </p:tabView>
    
    0 讨论(0)
  • 2021-01-14 04:00

    I think the answer of ali saleh is right.

    If you get problems with the tabchangelistener perhaps this can help you:

    I had a similar problem with Primefaces 5.1

    As long as i put the tabview into a form everything worked fine. But because i wanted to use seperate forms in my tabs i had to remove the surrounding form of the tabview to avoid nested forms. Without the surrounding form the ajax event didn´t get triggered any more when changing the tab.

    My solution was to use a remotecommand in a form parallel to the tabview.
    The remotecommand is triggered by the onTabChange attribute of the tabview element. At that call i forwarded the index parameter to the global request parameters.

    <p:tabView id="rootTabMenu" styleClass="tabcontainer" prependId="false" 
            activeIndex="#{sessionData.activeTabIndex}" widgetVar="rootTabMenu"
            onTabChange="tabChangeHelper([{name: 'activeIndex', value: index}])">
        // Tabs...  
    </p:tabView>
    
    <h:form id="tabChangeHelperForm">
        <p:remoteCommand name="tabChangeHelper" actionListener="#{sessionData.onTabChange()}" />
    </h:form>    
    

    In the backing bean i catched the value again from the request parameter map and set the active index.

    public void onTabChange()
    {   
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> paramMap = context.getExternalContext().getRequestParameterMap();
        String paramIndex = paramMap.get("activeIndex");
        setActiveTabIndex(Integer.valueOf(paramIndex));
        System.out.println("Active index changed to " + activeTabIndex);
    }    
    

    Hope that can help you

    0 讨论(0)
  • 2021-01-14 04:08

    Well I think that its a primefaces BUG, I found a workaround which as the following

    1. Do not use a global form (form for the tabView itself) instead use a form for each tab (inside it that surround the datatable)

    2. You have to add a dummy tab to be the first one that must include a form which some static data or pre-loaded data inside

    Thats all,

    the problem was that the ajax request is inside the global form and it cause the datatables to get their data first before ajax request, the strange thing with Primefaces that if you do not add the first dummy tab it will always execute the first form inside the tabs and get its data and this will cause a problem

    regards ,

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