Find out which Primefaces component performed an action using jQuery function

Asked

Viewed 536 times

2

I want to make a refactoring in my code, and for that I need to get the id of a component that suffered an action.

My target would be that I have two datatables, and wanted to take the id and from the id define what action to take.

So I have the datatables as shown below

<code>Datables</code>

I have within them the following calls to ajax in each table.

<p:ajax event="rowEditInit" onstart="disableBtnCorrenteExcluir();"/>
<p:ajax event="rowEdit" oncomplete="if (args.validationFailed) disableBtnCorrenteExcluir(); else enableBtnCorrenteExcluir();"/>
<p:ajax event="rowEditCancel" oncomplete="enableBtnCorrenteExcluir();" />

For each table I run methods to lock the Add Track, Edit Duration and Delete Duration buttons. What this in question is, if when I perform the above methods call in the specific events of the Primefaces, will have to catch which object called it?

How can I do that?

  • 1

    Hello Macario, I reorganized the question to leave the question sequential. Feel free to reverse the edit if it hasn’t gotten good or changed what the doubt really was.

  • 1

    Macario, I could not quite understand what you intend to do. Know the ID of datatabledepends a lot on the type of event you want to capture and where capture will be required. Maybe I or someone can help if you can try to detail a little more, maybe with examples of the code where the event is configured and the code where it is necessary to recover the ID.

  • Great @utluiz, always helping, next guy, I have two datatables, and I do 4 methods, 2 for each datatable. I wanted to do the following, pick up which component clicked, because inside the datatable has a button to delete. And then I wanted to pick which one was clicked, understood?

  • @utluiz sorry but updated the question again to make it clearer. I was leaving work and it was difficult to answer.

1 answer

1

I don’t know if it’s exactly what you expect, but when dealing with some event (like onclick) an element within a datatable, you can use the following Javascript code to retrieve the id table:

$(this).closest('.ui-datatable').attr('id');

Remembering that this in that context refers to the button that has been clicked.

The above code locates in the "parents" elements of the clicked element, the first that contains the class ui-datatable.

It works so much in one onclick inline in the element tag, as in a function added with jQuery.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.