scalaplayframeworktwirl

How to add button handler in Scala Play framework?


I'm using scala play framework + scala template engine for front end. How can I add button handler? I need to call some function when clicking on it.

<body>
    <p>Database interface<br />
        <textarea style="margin: 0px; height: 193px; width: 533px;" cols="40" name="comment" rows="3"></textarea>
    </p>
    <p><input type="submit" value="Select" /> <input type="submit" value="Insert" />  <input type="submit" value="Update" /></p>
</body>

Solution

  • There is no special button handlers. You can write it in Javascript or plain HTML. The only trick thing is to get a link the server function. You can get the link automatically by reverse routing.

    For example you have the route

    GET     /count                      controllers.CountController.count
    

    Then you can get the link by calling controllers.routes.CountController.count in your code.

    In the template engine, link:

    <a href="@controllers.routes.CountController.count">Count</a>
    

    Plain HTML button:

    <form action="@controllers.routes.CountController.count">
      <input type="submit" value="HTML only" />
    </form>
    

    Button with Javascript:

    <input type="submit" onclick="location.href='@controllers.routes.CountController.count';" value="Javascript" />
    

    If you need some special thing on the client side to process then you need to write your own code in Javascript.