Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to retrieve client id generated for a component in dataTable to call javascript in jsf 1.2  RSS feed

 
Sachin Yewale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , i am working on jsf 1.2 project. i have one basic requirement to multiply values of two text box and place it in third text box.
I have used dataTable to create row with three textBox component.

problem is client id in jsf are added dynamic as (formName:rowNo:componentId).

how to get that id to call javascript function?

Any help appreciated ,
Thanks in advanced.
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Sachin!

The easiest way to do what you want is to supply fixed IDs to the control and to its "naming containers". That way you'll always know that the JavaScript ID of the "field1" control in the fifth row of table1 on form1 is going to be "form1:table1:4:field1". Note that row numbering starts at 0. If you want a general method for taking 2 fields on ANY row, summing them and updating a third field, you can do things like attach the function to their onblur attributes, pass the "this" parameter to the method, and then pull the control ID from the "this" parameter, dissect it to build the IDs of the other 2 controls.

jQuery can be very helpful for stuff like this. Just don't code using the "$" shorthand notation, though, or you'll confuse the JSF EL processor.
 
Sachin Yewale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim and sorry for replying after so many days, actually i am working on multiple project.
Going back to my problem i am looking for method that will send row no to javascript function. As you said pass 'this' to javascript but i dont know how to do that, can you explain a bit with little bit code .
Thanks in advanced.
 
Sachin Yewale
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim and sorry for replying after so many days, actually i am working on multiple project.
Going back to my problem i am looking for method that will send row no to javascript function. As you said pass 'this' to javascript but i dont know how to do that, can you explain a bit with little bit code .
Thanks in advanced.
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




controlId should be a string containing the JSF control ID, which is going to look something like this: "formId:tableId:rownum:controlId". You can then strip out what you need from there. For example, if you have 2 controls whose values are summed and placed into a third control and they're all in the same table row, you can save their base ID (formId:tableId:rownum) and concatenate it with the control IDs to get the absolute IDs of the 3 controls in question which you then locate in the DOM.

I should point out, however, that this is rather nasty stuff, especially if you have to (and you should) add client-side validation logic to the javascript. It's simpler easier to use AJAX and let server-side code do the work, although the downside of that is that a trip to/from the server is required to do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!