File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Using JavaScript for a conditional action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Using JavaScript for a conditional action" Watch "Using JavaScript for a conditional action" New topic
Author

Using JavaScript for a conditional action

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
Hello Ranchers,

I'm sure this is a very simple question, but I can't find an obvious solution.

I have a command button that on clicking "Clear List" clears a list of uploaded filenames displayed on the screen, but at the moment it does not delete the files in the folder that have been uploaded. I can make the changes for this, but I want to warn the user that his files will be deleted from the server when he clicks the button, and give him the option of aborting the process.

The xhtml code is here:

and on clicking "Clear List" a dialog box appears asking him if he is sure. The idea is that if he clicks "OK", the files are deleted and the display is reset, otherwise nothing happens. However, the display is always reset regardless of the reply, and thus the files will be deleted once I have written the code for that.

Without changing any of the Java code in the bean, how do I arrange for the button only to work when onclick returns true? I'm usinmg JFS 2.

There must be an easy way to do this and would most apprecaite some advice.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
Hello,

I made a few changes on got this to work, but there is a "page flip".

The code in the xhtml file is now:

The only change was to replace onclick="confirm('Are you sure?')" by onclick="uploadClear()" in the command button, with uploadClear() being in a separate js file as follows:

In my previous post I had not included the data table, which is displayed on the screen. This is part of a form containing much more information.

Before the JavaScript was added, this worked correctly when clicking the "Clear List" button, and the display was cleared without a "page flip". With the JavaScript added, when the user clicks "OK" in the popup box, the display is again cleared without a "page flip", as it should be. However, when the user clicks "Cancel" in the popup box, the page does flip, but the displayed contents in the data table are not cleared, which is what I want. However, why do I get an irritating "page flip" when the "Cancel" is selected and the display stays the same, but not when "OK" is selected and the list is cleared?

I noticed that I had to invert the logic in the JavaScript function to get it to work the correct way round. Does anybody have any suggestions how to fix this, I would be most grateful to hear?

The idea is eventually to get files in an uploaded folder cleared, but it would be nice to get the display fixed first.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21



uploadClear() returned results, but you didn't return them in their turn to the commandButton.

This works exactly the same way as it does for a bog-standard HTML SUBMIT button, incidentally.


Customer surveys are for companies who didn't pay proper attention to begin with.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
OK, many thanks, being so tied up in the details of JSF 2, it's easy to forget bog standard HTML.

I put the whole JavaScript in the command button as follows:

and it now works correctly. "return confirm(...)" returns true or false, as needed and there is no more "page flip". Moreover, the logic is the correct way round.

Many thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using JavaScript for a conditional action