Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting values from the model using javascript  RSS feed

 
Tim Resh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get values from the Model using javascript. I have tried using #{model.field} but it only retrieves the value at the time of the model display. I need to put a button on the form and on the actionlistener go get the new data and then execute some javascript to display the data. An example would be to show a xy scattered plot graph with the current information from the screen which would be the most recent information being typed in and saved via the command buttom. I have tried using a dialogue and the oncomplete to execute the java script but the data is not updated. Any ideas on the correct approach to this would be great.

Thanks

Example code I tried
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript cannot access data on the server. For that you need something like AJAX. AJAX will submit a JSF form (or partial form) and return with a partial page update with the current data values as of the time the AJAX request/response was made.

If you change the div whose ID is "mycube" to be a JSF h:panelGrid control (a div is basically a 1x1 grid) and use the f:ajax qualifier on a form submit control with a "render="mycube"", then you'll get the updates values set in the sub-elements of mycube..

And, just to be tiresome, I'll point out that despite what NetBeans may say, JSF backing beans are Models and not Controllers. In JSF, the Controllers are part of JSF itself and not application code.
 
Tim Resh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, I'm not sure I am understanding exactly what you are saying, this is the last thing I tried and it did not work. Even my js code is not creating a chart anymore. I use the OnShow to call the code to create a chart where ever "mycube" is.

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't show what your callback function looks like, so if there's anything wrong there, I can't tell.

However, if there's a "onbeforeshow" option for the PrimeFaces dialog, that's where you'd do the AJAX data refresh. Otherwise, onshow should be what does it. You definitely want the data updated before the "show" actually happens.

One thing to remember is that not only are all of the data values set on the original page load and unchanged forever afterwards unless you use AJAX to update the page, but that the dialog itself isn't depending on server action either. The show/hide methods simply alter the CSS attributes of the DIV that was generated to contain the dialog, so that the entire dialog is actually rendered at initial page load time and remains unchanged forever afterwards. Unless you use AJAX to update it.

Another VERY important thing is that in most JSF systems, the pop-up dialog should be contained in its own form, separate from the regular page form and NEVER nested inside another form (HTML does not allow nested forms). Because of limitations in Internet Explorer, it's best to define the dialog and its containing form before the main form(s) on the page being constructed.
 
Tim Resh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, I got everything working except I am always getting the wrong values since everything is determined at page load. I can't seem to get a refresh of the data . Here is the whole JSF file.

 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend that you save a copy of all that and strip the page down. Remove all but 2 or 3 of the controls whose values you want refreshed and remove the graphics control, too, to minimize complexity and side-effects. I think that instead of re-rendering the entire form, I'd make AJAX only re-render the element whose ID is "selectedValueOutputPanel".

If you can get the basic functions of data updating working, then you can start adding back in the stuff that was removed. It's what I'd be doing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!