0

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?

Kungen
  • 607
  • 1
  • 11
  • 24
  • The question was why the `generateExcel(List list)` runs every single page load. So I don't understand your answer. – Kungen Jul 05 '13 at 07:16
  • No, sry for bad explaining. The file _"mydata.xls"_ is created even if I don't click on the button. It is created every time I enter deliverables site. – Kungen Jul 05 '13 at 07:43
  • I've tried `@controllers.Application.generateExcel(currentPage);` in my Javascript function, but it's still creating the file even if I don't click the button. Gaaaaah! – Kungen Jul 05 '13 at 08:35

3 Answers3

1

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.

  1. Do you ever see a complex list in a URL
  2. URLs have to be less than 2000 characters

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

cjds
  • 8,268
  • 10
  • 49
  • 84
  • Thanks for this explanation. The route code that you suggested doesn't work (I had to try, even if it sounds as a bad solution) `(thing: List list)` Error: "`)' expected but `<' found"...think it has to be [] instead of <>. `thing: List[path.InfoObject] list` Error: "`)' expected but `l' found"... `thing: List[path.InfoObject]` Error: "not found: value path"...no further ideas. I will try the POST solution. – Kungen Jul 08 '13 at 13:52
  • No That wouldn't be quite right... Maybe we can help you? The real question is how is this generated? Is it static data? Is it a form a user fills? – cjds Jul 09 '13 at 03:47
  • "currentPage" is a List of 'Infoobject'. On the deliverables site, there is a table that shows this list. Infoobject has designation, description, id and so on. There is a 'Export to Excel' button, when I click on it I want to generate a excel file. Why can't it just be simple like...Button>OnClick>Generate File? Why do I have to do it in a POST/GET? – Kungen Jul 09 '13 at 06:34
  • Try this in the route instead `thing: java.util.List` (if InfoObject is contained in the models package otherwise whatever package its in) – cjds Jul 09 '13 at 06:55
  • My guess would be routes doesn't support generic T-Lists.. You could just have `java.util.List` But then in your controller do `generateExcel(List l) {List list=(List) l;....` – cjds Jul 09 '13 at 07:28
  • `GET /generate/ controllers.Application.generateExcel(list: java.util.List)` gets error: "trait List takes type parameters". Is this right approach to do this? – Kungen Jul 09 '13 at 08:22
  • Try `GET /generate/ controllers.Application.generateExcel(list: java.util.ArrayList)` – cjds Jul 09 '13 at 08:31
  • "class ArrayList takes type parameters" – Kungen Jul 09 '13 at 08:37
1

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).

Community
  • 1
  • 1
Saffron
  • 682
  • 6
  • 14
0

I would call routes-> /generateExcelReport from the button/link and make it return the file

How to send a file to browser for downloading?

How to download a file with Play Framework 2.0

Community
  • 1
  • 1
Johan
  • 33
  • 5