Does anyone know why my action runs every time I reload a page. I have a page with a button that should run when I click on it. But now it seems to run when I load the page.
Here is my button and javascript in file deliverables.scala.html
<input type="button" class="btn success" id="add" value="Export to Excel" onclick="JavaScript:generateExcelClick()" />
<script>
function generateExcelClick(event)
{
window.location = "@Application.generateExcel(currentPage)";
}
</script>
And my controller:
public static void generateExcel(List<Infoobject> list) {
...creating a file (works)
...No return
}
How can I change this code so it only runs when I click on the button? The action should not render an another page or something like that. I only want to generate a file.
Thanks!
Edit
I've tried @controllers.Application.generateExcel(currentPage); and @Application.generateExcel(currentPage); in my Javascript function, but it's still creating the file even if I don't click the button.
I have also checked that @Application.generateExcel(currentPage); doesn't runs from an another site och function.
Someone, please?
A little explanation:
When writing in your Scala view and you say
@Application.generateExcel(currentPage)
It runs the function generateExcel
in controller Application
What to do
You don't want it to run the function immediately. You want it to go there onClick
So use
@routes.Application.generateExcel(currentPage)
This outputs a link to that function
However for this to work there has to be a (GET) link to that function in your routes
Add this to routes
GET /whatever/:thing controllers.Application.generateExcel(thing: List<path.InfoObject> list)
HOWEVER
This is a bad idea.Why? Because putting your entire list in a URL just isn't nice.
WHAT TO DO INSTEAD
Send the list as a POST data. Depending on how you take in your data you'll have to figure it out
If you are trying to just call a controller when clicking on a button, you could try :
<a href="@routes.YourController.YourMethod(args)"><button>Mybutton</button></a>
I don't think you need javascript here (if I understood your situation).
Edit: The idea of this answer is to say that the less javascript in your page, the better.
Edit2 : Can't comment the discussion below, so I put it here: As I said here : link , your have to declare your object like this :
controllers.Application.method(list : java.util.List[your.package.Infoobject])
Replace your.package
with the package in which your object is (maybe models
)
But you will get an errror : No QueryString binder found for type
This is because you can only put Strings and numerals in URLS, so the framework tells you to transform your Object (List) in a String (with the QueryStringBinder).
I would call routes-> /generateExcelReport from the button/link and make it return the file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.