TabView with add tab functionality

UI Components for JSF
Post Reply
honyk
Posts: 150
Joined: 28 Sep 2010, 11:14

02 Dec 2011, 23:33

Hello Everyone,

I am trying to implement major browser tab behaviour using tabView component - to have closable tabs and one, the last, not closable, but capable to create a new tab. Here is my idea:

JSF page:

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>TabViewTest</title>
    </h:head>
    <h:body>
        <h:form>
            <p:tabView var="tabItem" value="#{tab.tabItemList}" widgetVar="tabView" onTabChange="checkIfNewTabIsRequired(index == tabView.getLength() - 1)">
                <p:ajax event="tabClose" listener="#{tab.onTabClose(event)}"/>
                <p:tab title="#{tabItem}" closable="#{tabItem == '+' ? false : tab.closable}">  
                    #{tabItem}
                </p:tab>
            </p:tabView>
        </h:form>
        <script> 
            /* <![CDATA[ */
            function checkIfNewTabIsRequired(isNewTabRequired) {
                if (isNewTabRequired) {
                    alert(isNewTabRequired);    
                    // addTab somehow
                }
            }            
            /* ]]> */
        </script>        
    </h:body>
</html>
bean:

Code: Select all

@SessionScoped
@ManagedBean(name = "tab")
public class TabBean {

    private Boolean closable = true;
    private Integer tabCount = 1;
    private ArrayList<String> tabItemList = new ArrayList<String>();

    public TabBean() {
        tabItemList.add("1");
        tabItemList.add("2");
        tabItemList.add("3");
        tabItemList.add("+");
        updateVariables();
    }
    
    private void updateVariables() {
        tabCount = tabItemList.size();
        closable = tabCount > 2;
    }
    
    public void onTabClose(TabCloseEvent event) {  
        // getCurentIndex somehow
        //tabItemList.remove(index);
        updateVariables();
    }  
    
    public void addTab() {
        String title = Integer.toString(tabItemList.size());
        tabItemList.add(tabItemList.size() - 1, title);
        updateVariables();
    }

    public Boolean getClosable() {
        return closable;
    }

    public void setClosable(Boolean closable) {
        this.closable = closable;
    }

    public Integer getTabCount() {
        return tabCount;
    }

    public void setTabCount(Integer tabCount) {
        this.tabCount = tabCount;
    }

    public ArrayList<String> getTabItemList() {
        return tabItemList;
    }

    public void setTabItemList(ArrayList<String> tabItemList) {
        this.tabItemList = tabItemList;
    }       
}
Several problems I've encountered (PF 3.0M4/Mojarra 2.1.4):
1) close tab listener is never called.
2) I have no idea how to call addTab method of backing bean from javascript code
3) when tab before + is closed, + tab is selected (what would create a new tab)

Has anybody implemented something like this?

Regards,
Jan

voyagerx
Posts: 70
Joined: 10 Feb 2011, 03:52

04 Dec 2011, 06:24

I didn't look too carefully at what you were trying to do, but some quick suggestions:

Instead of:
<p:ajax event="tabClose" listener="#{tab.onTabClose(event)}"/>

try

<p:ajax event="tabClose" listener="#{tab.onTabClose}"/>
(My IDE complains about it this way and suggests I do it the way you have it to, but it doesn't work that way)


Also, you can call a bean method easily with javascript with the RemoteCommand component.

xbellox
Posts: 20
Joined: 13 Jan 2012, 20:12

21 Mar 2012, 22:29

Hi,

That is exactly what I'm trying to do.

Did you succeed?

I think the best way to accomplish that would be with a method like 'onBeforeTabChange' where I would be able to access the previousTab and the nextTab, and also be able to abort the event.

By the way, is there a way to prevent the any event? If not, it would be a huge feature to implement in next versions... Methods like: 'onBeforeTabChange', 'onBeforeClose', 'onBeforeRender', 'onAfterRender' etc... And the possibility to prevent those methods to propagate
PrimeFaces 3.3.1 | Tomcat 7.0.29 | MyFaces 2.1.8 (JSF 2.1)
Eclipse Indigo SR2 | JDK1.7 | Java EE 6 | Oracle 10.2
Windows 7 | Google Chrome
--
Eduardo Bello

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

22 Mar 2012, 01:18

Add feature request in Issue Tracker; prefix the title of the new issue as FEATURE REQUEST, or something like that. Optimus Prime is honoring feature requests that are requested by community users as per what he stated in his latest PrimeFaces release (Mobile version 0.9.2). http://blog.primefaces.org
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

xbellox
Posts: 20
Joined: 13 Jan 2012, 20:12

22 Mar 2012, 15:37

I just did that, thank you.
PrimeFaces 3.3.1 | Tomcat 7.0.29 | MyFaces 2.1.8 (JSF 2.1)
Eclipse Indigo SR2 | JDK1.7 | Java EE 6 | Oracle 10.2
Windows 7 | Google Chrome
--
Eduardo Bello

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

22 Mar 2012, 16:38

Issue 3789

If many people click the Star to vote on this feature request, then Optimus Prime will fix this issue (feature request). Please remember to provide issue # and URL to new issue in forum topic as I did above, so others that read this forum topic, will not have to search issue tracker for the new issue (feature request). :)
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests