Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

Jquery and play framework 2 javascript router issue

I am new to Javascript and trying to implement AJAX generation of html DELETE request to Play 2 Framework server. In Scala template I have this for now. It prints all fields of my model class Item and then I need to handle a click at a href using JQuery.

How can I fix the code and also pass appropriate item.id into AJAX request?

@(item: Item)

<ul>
  @for(field <- item.getClass().getFields()) {
    <li>@field.getName() = @field.get(item)</li>
  }
</ul>

<a href="#" data-id="@item.id" id="delete">delete</a>



   <script type="text/javascript">
    $("#delete").click(function() {
        var id = $(this).attr("data-id");
        alert(id);
        jsRoutes.controllers.Items.delete(id).ajax({});
        return false;
    });
   </script>

This link is very useful but does not give example of integrating jquery and javascript router in detail:

Play 2.x: How to make an AJAX request with a common button

Upvotes: 0

Views: 630

Answers (1)

johanandren
johanandren

Reputation: 11479

You could add the id as an attribute on the delete link:

<a href="#" data-id="@item.id">delete</a>

And then read that with jquery in your js, something like:

$('delete').click( function() {
    var id = $(this).attr("data-id");
    jsRoutes.controllers.Application.Items.delete(id).ajax({
    ...

Upvotes: 1

Related Questions