You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Anton Gavazuk <an...@gmail.com> on 2008/12/19 10:47:17 UTC

[JSF] using ActionListener for executing different actions from table

Hi all,

I"m making the master-detail scenario via ActionListeners and want to
use the same ActionListener

is there any "easy" way to know which link in table row performs the action?

Thanks.,
Anton

Re: [JSF] using ActionListener for executing different actions from table

Posted by Andrew Robinson <an...@gmail.com>.
Option A:

<h:table>
  <h:column>
    <h:commandLink id="link1"
      action="actionA" actionListener="processClick" />
  </h:column>
  <h:column>
    <h:commandLink id="link2" action="actionB"
      actionListener="processClick">
  </h:column>
</h:table>

public void processClick(ActionEvent event)
{
  System.out.println(event.getComponent().getId()); // link1 or link2
}

Option B:

<h:table>
  <h:column>
    <h:commandLink action="actionA" actionListener="processClick">
      <f:attribute name="actionId" value="A" />
    </h:commandLink>
  </h:column>
  <h:column>
    <h:commandLink action="actionB" actionListener="processClick">
      <f:attribute name="actionId" value="B" />
    </h:commandLink>
  </h:column>
</h:table>

public void processClick(ActionEvent event)
{
  System.out.println(event.getComponent()
    .getAttributes().get("actionId")); // A or B
}




On Fri, Dec 19, 2008 at 12:10 PM, Anton Gavazuk <an...@gmail.com> wrote:
> Hi Simon, Andrew
>
> thanks for the answers,
>
> sure, my question is confusing - I meant how to do something like:
>
> <h:table>
> <!-- its link1 -->
>   <h:column>
>  <h:commandLink action="actionA" actionListener="processClick">
>   </h:column>
>
> <!-- its link2 -->
>   <h:column>
>  <h:commandLink action="actionB" actionListener="processClick">
>   </h:column>
> </h:table>
>
> So my question was how to define in processClick that user clicks on
> link1 or on click 2?
>
> you cannot put certain Id in link component - its table, so id must be
> unique and its  generated by component,
> and actionEvent.getComponent() returns something like "id_lnk_141413"
> - sure, not very meaningful :)
>
> So for now I have gone with separate methods for every actionListener.
>
> Cheers,
> Anton
>
>
>
> 2008/12/19 Simon Kitching <sk...@apache.org>:
>> Anton Gavazuk schrieb:
>>> Hi all,
>>>
>>> I"m making the master-detail scenario via ActionListeners and want to
>>> use the same ActionListener
>>>
>>> is there any "easy" way to know which link in table row performs the action?
>>>
>> This should be covered by most JSF textbooks.
>>
>> There is also a page on the myfaces wiki. This page:
>>   http://wiki.apache.org/myfaces/
>> has a link to here:
>>  http://wiki.apache.org/myfaces/ExecutingMethodsFromLinkButtonParameters
>> which hopefully answers your question.
>>
>> Regards,
>> Simon
>>
>> --
>> -- Emails in "mixed" posting style will be ignored
>> -- (http://en.wikipedia.org/wiki/Posting_style)
>>
>>
>

Re: [JSF] using ActionListener for executing different actions from table

Posted by Anton Gavazuk <an...@gmail.com>.
Hi Simon,

thank you very much for the answer - it helps.

>>By the way, I normally ignore emails that have mixed posting style (see
>>my original sig). But as it's Christmas...

see the point, always trying to optimize bandwidth using :), will keep
in the mind next time

Best regards and have a great Christmas break!



2008/12/20 Simon Kitching <sk...@apache.org>:
> Ah. So in that case, Andrew understood your question correctly and I did
> not. You really *did* mean "which link in table row"...
>
> Yes, you *can* add an id to each of the commandLink components; there is
> only one instance of each column no matter how many rows there are in
> the table (table components uses the "flyweight" pattern). Note that the
> "clientId" is different for each row, as the table automatically ensures
> that components have clientId values of
>  tableId:rownum:componentId
>
> So just go ahead and add the ids, then as you (and Andrew) point out,
> the ActionEvent passed to the processClick method will return the
> component whose id (not clientId) you can then check.
>
> By the way, I normally ignore emails that have mixed posting style (see
> my original sig). But as it's Christmas...
>
> Regards,
> Simon
>
> On Fri, 2008-12-19 at 21:10 +0200, Anton Gavazuk wrote:
>> Hi Simon, Andrew
>>
>> thanks for the answers,
>>
>> sure, my question is confusing - I meant how to do something like:
>>
>> <h:table>
>> <!-- its link1 -->
>>    <h:column>
>>   <h:commandLink action="actionA" actionListener="processClick">
>>    </h:column>
>>
>> <!-- its link2 -->
>>    <h:column>
>>   <h:commandLink action="actionB" actionListener="processClick">
>>    </h:column>
>> </h:table>
>>
>> So my question was how to define in processClick that user clicks on
>> link1 or on click 2?
>>
>> you cannot put certain Id in link component - its table, so id must be
>> unique and its  generated by component,
>> and actionEvent.getComponent() returns something like "id_lnk_141413"
>> - sure, not very meaningful :)
>>
>> So for now I have gone with separate methods for every actionListener.
>>
>> Cheers,
>> Anton
>>
>>
>>
>> 2008/12/19 Simon Kitching <sk...@apache.org>:
>> > Anton Gavazuk schrieb:
>> >> Hi all,
>> >>
>> >> I"m making the master-detail scenario via ActionListeners and want to
>> >> use the same ActionListener
>> >>
>> >> is there any "easy" way to know which link in table row performs the action?
>> >>
>> > This should be covered by most JSF textbooks.
>> >
>> > There is also a page on the myfaces wiki. This page:
>> >   http://wiki.apache.org/myfaces/
>> > has a link to here:
>> >  http://wiki.apache.org/myfaces/ExecutingMethodsFromLinkButtonParameters
>> > which hopefully answers your question.
>> >
>> > Regards,
>> > Simon
>> >
>> > --
>> > -- Emails in "mixed" posting style will be ignored
>> > -- (http://en.wikipedia.org/wiki/Posting_style)
>> >
>> >
>
>

Re: [JSF] using ActionListener for executing different actions from table

Posted by Simon Kitching <sk...@apache.org>.
Ah. So in that case, Andrew understood your question correctly and I did
not. You really *did* mean "which link in table row"...

Yes, you *can* add an id to each of the commandLink components; there is
only one instance of each column no matter how many rows there are in
the table (table components uses the "flyweight" pattern). Note that the
"clientId" is different for each row, as the table automatically ensures
that components have clientId values of
  tableId:rownum:componentId

So just go ahead and add the ids, then as you (and Andrew) point out,
the ActionEvent passed to the processClick method will return the
component whose id (not clientId) you can then check.

By the way, I normally ignore emails that have mixed posting style (see
my original sig). But as it's Christmas...

Regards,
Simon

On Fri, 2008-12-19 at 21:10 +0200, Anton Gavazuk wrote:
> Hi Simon, Andrew
> 
> thanks for the answers,
> 
> sure, my question is confusing - I meant how to do something like:
> 
> <h:table>
> <!-- its link1 -->
>    <h:column>
>   <h:commandLink action="actionA" actionListener="processClick">
>    </h:column>
> 
> <!-- its link2 -->
>    <h:column>
>   <h:commandLink action="actionB" actionListener="processClick">
>    </h:column>
> </h:table>
> 
> So my question was how to define in processClick that user clicks on
> link1 or on click 2?
> 
> you cannot put certain Id in link component - its table, so id must be
> unique and its  generated by component,
> and actionEvent.getComponent() returns something like "id_lnk_141413"
> - sure, not very meaningful :)
> 
> So for now I have gone with separate methods for every actionListener.
> 
> Cheers,
> Anton
> 
> 
> 
> 2008/12/19 Simon Kitching <sk...@apache.org>:
> > Anton Gavazuk schrieb:
> >> Hi all,
> >>
> >> I"m making the master-detail scenario via ActionListeners and want to
> >> use the same ActionListener
> >>
> >> is there any "easy" way to know which link in table row performs the action?
> >>
> > This should be covered by most JSF textbooks.
> >
> > There is also a page on the myfaces wiki. This page:
> >   http://wiki.apache.org/myfaces/
> > has a link to here:
> >  http://wiki.apache.org/myfaces/ExecutingMethodsFromLinkButtonParameters
> > which hopefully answers your question.
> >
> > Regards,
> > Simon
> >
> > --
> > -- Emails in "mixed" posting style will be ignored
> > -- (http://en.wikipedia.org/wiki/Posting_style)
> >
> >


Re: [JSF] using ActionListener for executing different actions from table

Posted by Anton Gavazuk <an...@gmail.com>.
Hi Simon, Andrew

thanks for the answers,

sure, my question is confusing - I meant how to do something like:

<h:table>
<!-- its link1 -->
   <h:column>
  <h:commandLink action="actionA" actionListener="processClick">
   </h:column>

<!-- its link2 -->
   <h:column>
  <h:commandLink action="actionB" actionListener="processClick">
   </h:column>
</h:table>

So my question was how to define in processClick that user clicks on
link1 or on click 2?

you cannot put certain Id in link component - its table, so id must be
unique and its  generated by component,
and actionEvent.getComponent() returns something like "id_lnk_141413"
- sure, not very meaningful :)

So for now I have gone with separate methods for every actionListener.

Cheers,
Anton



2008/12/19 Simon Kitching <sk...@apache.org>:
> Anton Gavazuk schrieb:
>> Hi all,
>>
>> I"m making the master-detail scenario via ActionListeners and want to
>> use the same ActionListener
>>
>> is there any "easy" way to know which link in table row performs the action?
>>
> This should be covered by most JSF textbooks.
>
> There is also a page on the myfaces wiki. This page:
>   http://wiki.apache.org/myfaces/
> has a link to here:
>  http://wiki.apache.org/myfaces/ExecutingMethodsFromLinkButtonParameters
> which hopefully answers your question.
>
> Regards,
> Simon
>
> --
> -- Emails in "mixed" posting style will be ignored
> -- (http://en.wikipedia.org/wiki/Posting_style)
>
>

Re: [JSF] using ActionListener for executing different actions from table

Posted by Simon Kitching <sk...@apache.org>.
Anton Gavazuk schrieb:
> Hi all,
>
> I"m making the master-detail scenario via ActionListeners and want to
> use the same ActionListener
>
> is there any "easy" way to know which link in table row performs the action?
>   
This should be covered by most JSF textbooks.

There is also a page on the myfaces wiki. This page:
   http://wiki.apache.org/myfaces/
has a link to here:
  http://wiki.apache.org/myfaces/ExecutingMethodsFromLinkButtonParameters
which hopefully answers your question.

Regards,
Simon

-- 
-- Emails in "mixed" posting style will be ignored
-- (http://en.wikipedia.org/wiki/Posting_style)


Re: [JSF] using ActionListener for executing different actions from table

Posted by Andrew Robinson <an...@gmail.com>.
actionEvent.getComponent()

On Fri, Dec 19, 2008 at 2:47 AM, Anton Gavazuk <an...@gmail.com> wrote:
> Hi all,
>
> I"m making the master-detail scenario via ActionListeners and want to
> use the same ActionListener
>
> is there any "easy" way to know which link in table row performs the action?
>
> Thanks.,
> Anton
>