0

I've got following situation:

There is a JSF page:

<h:form id="form">

            <!-- MENU SHOULD GOES HERE -->
            <p:commandButton action="#{bean.sched.addWeek}" value="Add week" update="weeksTab" />

            <p:tabView id="weeksTab" value="#{bean.sched.weekList}" var="week" >

                <p:tab title="#{week.name}" >

                    <!-- THIS BUTTONS SHOULD BE IN MENU -->
                    <p:commandButton action="#{week.addDay}" value="Add day" update="weeksTab" />
                    <p:commandButton action="#{bean.sched.removeWeek(week)}" value="Remove week" update="weeksTab" />

                    <p:tabView id="daysTab" value="#{week.dayList}" var="day" >
                        <p:tab title="#{day.name}">

                        <!-- THIS BUTTON SHOULD BE IN MENU -->
                        <p:commandButton action="#{week.removeDay(day)}" value="Remove day" update="weeksTab" />

                        <!-- PROCESSING DAY : ADDING MEETINGS, ROUTINES ETC.
                            THOSE FUNCTIONS SHOULD BE ALSO AVAILABLE
                            IN MENU -->

                        </p:tab>
                    </p:tabView>
                </p:tab>
            </p:tabView>

I want to place all action controlls, such as adding/deleting/editing weeks, days, and user's daily acitivites buttons to the menu at the top of the page. The problem is how to pass to the outer tag variables that are menaged inside p:tabView ? Is it possible anyhow? I add sth like this inside h:form:

<p:toolbar>
                <p:toolbarGroup align="left">
                    <p:commandButton
                        onclick="jQuery('#hiddenAddWeekButton').click();return false;"
                        value="Add week" icon="ui-icon-circle-plus" ajax="false" />

                </p:toolbarGroup>
            </p:toolbar>

            <p:commandButton id="hiddenAddWeekButton"
                action="#{bean.sched.addWeek}" value="add week"
                update="weeksTab" style="display:none" />

but it's still not working..

Sancho.Pansa
  • 183
  • 1
  • 2
  • 10
  • OK, it works, I forgot to escape :. Can anybody explain why I should escape this character? – Sancho.Pansa Aug 15 '12 at 19:43
  • jQuery doesn’t play well with the JSF clientIds containing the colon “:” so special characters like this need to be escaped. https://www.google.com/search?q=jsf+jquery+escape+colon&rlz=1C1CHEU_enIL465IL465&sugexp=chrome,mod=19&sourceid=chrome&ie=UTF-8 – Daniel Aug 15 '12 at 19:55
  • http://stackoverflow.com/questions/7927716/how-to-select-primefaces-ui-or-jsf-components-using-jquery/7928290#7928290 – BalusC Aug 16 '12 at 11:04

1 Answers1

1

try something like this

make your inner button hidden

<p:commandButton id="myHiddenButton" action="#{week.removeDay(day)}" value="Remove day" update="weeksTab" style="display:none"/>

than place new button above the tab

<p:commandButton value="Remove day" onclick="jQuery('#weeksTab\\:myHiddenButton').click();return false;" />

to be sure about the right id that is being genereated for the hidden button just do view source in your browser , i think it should be something like weeksTab:myHiddenButton (ussing the \:) to escape the :


UPDATE

Since its located in tab you should try using ends with selector , like this

<p:commandButton value="Remove day" onclick="myJsFunctionThatSitsInMyJsFile();return false;" />

where myJsFunctionThatSitsInMyJsFile

is as follows

function myJsFunctionThatSitsInMyJsFile(){
    jQuery('input[id$="hiddenAddDayButton"]:visible').click(); //added visible selector , so the only button that being visible , should be clicked
}
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • I will try it. Btw, do you know how to display generated JSF view component tree? It is displayed when an error occurs (under Tomcat), but is it a chance to display it "manually"? – Sancho.Pansa Aug 15 '12 at 18:15
  • nope , maybe this might help you http://publib.boulder.ibm.com/infocenter/rbdhelp/v8r0m0/index.jsp?topic=%2Fcom.ibm.egl.pg.doc%2Ftopics%2Fpegl_jsf_sourceassistant.html or this https://www.google.com/search?q=display+jsf+view+component+tree&rlz=1C1CHEU_enIL465IL465&sugexp=chrome,mod=19&sourceid=chrome&ie=UTF-8 – Daniel Aug 15 '12 at 18:24
  • Unfortunately this is not working in case specified in topic - there are tabs, so button id is changing on every tab. For example: subview:form:weeksTab:0:hiddenAddDayButton; subview:form:weeksTab:1:hiddenAddDayButton; How to distinguish which one should by "clicked" by jQuery? – Sancho.Pansa Aug 15 '12 at 20:16
  • try "end with selector" `jQuery('input[id$='hiddenAddDayButton']').click();` , look here http://api.jquery.com/attribute-ends-with-selector/ – Daniel Aug 15 '12 at 20:21
  • but wouldn't it find ALL the buttons, not only the one that is displayed? – Sancho.Pansa Aug 15 '12 at 20:26
  • "but wouldn't it find ALL the buttons," , nope cause all the other are not being renderd , you can type this in console of your web brwoser to see how many it found jQuery('input[id$="hiddenAddDayButton"]').length – Daniel Aug 15 '12 at 20:49
  • create some small test js file and place `function myJsFunctionThatSitsInMyJsFile(){ jQuery('input[id$="hiddenAddDayButton"]').click(); }` inside it , see my edit... i think the "reloads whole page after clicking the button" cause of using `'` instead of `"` – Daniel Aug 15 '12 at 20:52
  • As I suspected - jQuery('button[id$="hiddenAddDayButton"]').length is equal to the number of tabs created, so unfortunately this is still not working. When I click "add day" button on one tab, the day is added on every tab. – Sancho.Pansa Aug 15 '12 at 21:19
  • oki , lets try this : should work - `jQuery('input[id$="hiddenAddDayButton"]:visible').click();` (edited my answer too) . here a jsfiddle to demonstrate http://jsfiddle.net/vedmack/VE4xx/ – Daniel Aug 16 '12 at 05:48
  • but the thing is, that all of that buttons are NOT visible ;-) so no one of them would be clicked by jQuery. – Sancho.Pansa Aug 16 '12 at 18:38
  • but there always supposed to be at least one visible button (for currently visible tab) isn't it right ? – Daniel Aug 16 '12 at 18:42
  • The only visible buttons should appear in toolbar in menu. I'm trying to do some kind of workaround using listeners now. Thank you for your help! – Sancho.Pansa Aug 16 '12 at 19:10
  • You are welcome , Now I understood the problem of using the visible selector (i told you to hide them all myself :) ) , You can make use of widgetVar of the tab i think to get the currently open/active tab index and make your jquery selector more precise... good luck... – Daniel Aug 16 '12 at 19:27
  • You can select a visible tab with jquery selector and then find inside it a button with ends with selector – Daniel Aug 16 '12 at 19:48