0

I'm completely editing the original question sience I've got an answer in this post that gave me some guidance:

I've got this ThymeLeaf template:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/app.css}" />
</head>
<body>
<div class="container">
    <select name="gustos" id="selectGustos">
        <option th:each="gusto : ${gustos}" th:text="${gusto.nombre}" th:value="${gusto.id}"> </option>
    </select>
    <div class="row delete">
        <div class="col s12 l8">
            <form th:action="@{'/gustos/' + ${gusto.id} + '/delete'}" method="post">
                <button type="submit" class="button">Borrar</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

In the <form> I'm doing a post with the variable ${gusto.id} that's not being binded to anything (and not working properly).

What I need to do is bind the selected <option>'s id value to the form's ${gusto.id} variable so that my controller then know's which id needs to be deleted.

So basically I need the selected <option>'s (which it will be an Object of type Gusto) id attribute to travel in my <form> to my controller.

The controller is expecting an int as the id !!

3 Answers3

1

Add th:action="@{/gustos/{id}/delete(id=${gusto.id})}" like this.

Refer thymeleaf-construct-url-with-variable and standardurlsyntax-thymeleaf

Alien
  • 15,141
  • 6
  • 37
  • 57
1

You could always use jquery for this. In fact, I believe it would be the easiest solution for your current issue. This way, you will always have the correct id in the action url.

$('#selectGustos').on('change', function() {
  var value = $(this).val();
  $('#gustosForm').attr('action', 'gustos/' + value + '/delete');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/app.css}" />
</head>
<body>
<div class="container">
    <select name="gustos" id="selectGustos">
        <option value="-1" selected="selected">Escoger Gusto</option>
        <option th:each="gusto : ${gustos}" th:text="${gusto.nombre}" th:value="${gusto.id}"> </option>
    </select>
    <div class="row delete">
        <div class="col s12 l8">
            <form id='gustosForm' th:action="@{'/gustos/' + ${gusto.id} + '/delete'}" method="post">
                <button type="submit" class="button">Borrar</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

I added an id to your form, so that it would be easier to fetch it's value. One more thing, you should add a default option in the beginning of your select, so that the user can't choose a wrong value.

Alain Cruz
  • 4,757
  • 3
  • 25
  • 43
0
th:onclick="'javascript:consultar();'"

<script type="text/javascript"> 
   function consultar() {

  var gustoId = $("#selectGustos option:selected").val();

    $.ajax({

        url: "/gustos/"+ gustoId + "/delete}",
        success: function(response) {

        }
    });         
}
</script>`
Kaique Dias
  • 45
  • 1
  • 10