I have a table filled with a List of String that I receive from a controller, and then I want to put a button for each one to call to another function and get some objects related with that string.
<tbody th:each="titulo : ${listaColecciones}">
<tr>
<th th:utext="${titulo}"></th>
<th>
<form class="navbar-form navbar-left" action="#" th:action="#{/twittercontrolador/recuperarColeccion}" th:object="${textocoleccion}" th:value="${titulo}" method="post">
<button type="submit" class="btn btn-primary" value="Filtrar">Recuperar coleccion</button>
</form>
</th>
</tr>
</tbody>
But it seems not to work, it doesn't get the ${titulo} as a parameter for the function
Edit: Here I have a picture of what I'm trying to do:
As you can see, I get a List (Thre're database table names) in the controller from a method1, and I pass that list to the view.
There, I'm trying to put a table with 2 columns, first is the string/table name, and second is a button to call a second method which will return the objects in that table.
So, as you may suppose, the <tbody th:each="titulo : ${listaColecciones}">is the list of database table names.
<th th:utext="${titulo}"></th>
The names to know which table are you getting from the database
<form class="navbar-form navbar-left" action="#" th:action="#{/twittercontrolador/recuperarColeccion}" th:object="${textocoleccion}" th:value="${titulo}" method="post">
<button type="submit" class="btn btn-primary" value="Filtrar">Recuperar coleccion</button>
</form>
And here is where I'm getting the problems, the button. th:action="#{/twittercontrolador/recuperarColeccion}" is the second method in the controller and I don't know how to pass it the string (${titulo}) as a parameter for it.
Note that th:object="${textocoleccion}"is the name of the string I will receive in the second method but I can not set it to the value of the strings.
For all those who may have the same problem, this works for me:
<form class="navbar-form navbar-left" action="#" th:action="#{/twittercontrolador/recuperarColeccion}" th:object="${textocoleccion}" method="post" >
<button class="btn btn-success" type="submit" id="textocoleccion" name="textocoleccion" th:value="${titulo}">RECUPERAR</button>
</form>
I guess the key is to use id and name tags :D
Thanks to all
If you want to pass informations about the button you have clicked, then you should use input or button as tag and (that's important) a name and value attribute.
<input type="submit" name="somePostParamName" th:value="${titulo}" />
Related
I'm trying to create a table from a list phases where each row has forms that will relate to the list item phase for that row - so row 1 has a cell for "Phase 1" and then a button that will rename "Phase 1", then the next row has "Phase 2" and a button for that.
However, when I try to access the index of that list iteration from within the form it always returns 0, so the first cell of row 2 still says "Phase 2" but the form still passes the index of "Phase 1" to the HTTP request. It can definitely still see the list, because if I change it to return phaseStat.size rather than the phaseStat.indexit gives the correct number.
The cells containing the phase name work fine, it's only once I get inside the <form> tag that it gets stuck on the first item of the list. How do I get the form to keep pace with the iteration with the rest of the table?
<table class="table">
<tr th:each="phase : ${phases}">
<td class="table-light" th:text="${phase}"></td>
<td>
<form th:action="#{/tasks/phases/rename}">
<button type="button" class="btn btn-outline-primary btn-sm" title="Rename" onclick="openForm('renamephaseform')">Rename</button>
<div class="form-popup card mb-3" id="renamephaseform">
<h3 class="card-header">Rename
<button type="button" class="btn btn-outline-secondary btn-sm closebutton" title="Close menu" onclick="closeForm('renamephaseform')">⛌</button>
<br>
</h3>
<div class="card-body">
<input type="text" class="form-control" id="phasename" placeholder="Phase name" th:name="newname">
<button type="submit" class="btn btn-primary" title="Rename" th:name="phasenumber" th:value="${phaseStat.index}">Rename <span th:text="${phase}"></span></button>
</div>
</div>
</form>
</td>
</tr>
</table>
<script>
function openForm(name) {
document.getElementById(name).style.display = "block";
}
function closeForm(name) {
document.getElementById(name).style.display = "none";
}
</script>
Edit:
The weird thing is, the iteration works perfectly fine in this example and I can't see what the difference is...
<table class="table">
<tr th:each="album : ${albums}">
<td class="table-light" th:text="${album.name} + ' (' + ${album.artist} + ')'"></td>
<td>
<form th:action="#{/index}">
<button type="submit" class="btn btn-primary btn-sm" th:name="id" th:value="${album.id}">Open album</button>
</form>
</td>
</tr></table>
So, what I ended up doing that made this work (thanks to andrewJames) is including the iteration index in the form id to create a unique name.
So for the Open button I included
th:attr="onclick='openForm(\'renamephaseform' + ${phaseStat.index} + '\')'", in the Closebutton th:attr="onclick='closeForm(\'renamephaseform' + ${phaseStat.index} + '\')'" and in the form tag th:attr="id='renamephaseform' + ${phaseStat.index}".
Elswhere, I've sometimes used string values for this (eg the phase name, task name etc) to make it easier to review in the Inspector.
How can I access a list variable contained inside the object in HTML using Thymleaf
Eg :All the aClient properties are String objects.They can be accessed as
<tr th:each="aClient : ${clientList}">
<td th:text="${aClient.firstName}"/>
<td th:text="${aClient.lastName}"/>
<td th:text="${aClient.address}"/>
<td th:text="${aClient.clientType}"/>
But for property like
List<Service> services.
How can I access it?
Tried doing below after some reading. But it did not pick up the names of services.
<div th:each="service, serv : *{services}">
<input type="text" th:field="*{service[__${serv.index}__].name}" />
</div>
"name" is the property in Service class with getters and setters.
The contents of an input field are identified by the value attribute.
Also, you don't need to use the Thymeleaf serv iterator tracker, since in this case, you are already iterating each Service object - and you just want to get the value of each object's name field.
This can all be simplified to the following:
<div th:each="service : ${services}">
<input type="text" th:value="${service.name}" />
</div>
(It's basically the same approach as you used for your th:text data.)
The generated HTML is as follows:
<div>
<input type="text" value="service 1 name">
</div>
<div>
<input type="text" value="service 2 name">
</div>
This assumes you want each input field in its own div.
Needed to insert the serivce names in the column.This is achieved by
<td>
<div th:each="service : ${aClient.services}" th:text="${service.name}"></div>
</td>
I am trying to get all the payments that is been selected through the checkboxes and then update the value in the database by '1' which means its paid. But when I try to get the values of checkbox array I only get 1 data in the array and not all selected. Any help?
View
#foreach($sellerID as $p)
<form action="/paid/{{$p->id}}" method="post" enctype="multipart/form-data">
#csrf
<td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
<td> {{$p->order_number}} </td>
<td>
<input id="commission" type="text" class="form-control" name="commission"
value="{{ $p->commission_value }}" readonly autocomplete="notes" style="width: 15%;" autofocus>
</td>
<td> {{$p->product_name}} </td>
<td>
#if($p->sellet_commission_paid == '0')
<button type="submit" class="btn btn-default" style="background-color:red">
<b><em>UNPAID</b></em>
</button>
#else
<b><em>PAID</b></em>
#endif
<td>
</form>
#endforeach
Controller
$checked = $request->input('checked');
// $checkboxes = $request->checked;
dd($checked);
die();
// dd(checkboxes);
you can put almost anything you like inside a form, but not inside a table. table has a more restricted structure which you must follow.
instead of wrapping table rows with forms, you can wrap the whole table with 1 form and submit all of it and just extract the details you need,
You need to put all input element inside form :
<form action="/route">
<input ...../>
<div>
<input ...../>
</div>
</form>
A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.
If a form-associated element has a form attribute specified, then that attribute's value must be the ID of a form element in the element's owner Document.
Try using a foreach loop.
foreach($request->input('checked') as $check){
[sample variable] = $check;
}
The array will now be stored as $check and you should be able to get each value from the input
I have a list of objects in my view:
<tbody>
<tr th:each="object : ${myObjects}">
<td><span th:text="${object.id}"> </span></td>
<td><span th:text="${object.name}"> </span></td>
<td><span th:text="${object.address}"></span></td>
<td>
<a href="#" data-toggle="modal" data-target="#ADD_SOME_INFO_MODAL">
<i class="fas fa-plus"></i>
</a>
</td>
</tr>
</tbody>
button in last column starts a modal. In modal I can add some value for a record:
<form th:action="#{/add-data}"
method="post"
class="form">
<input name="NEW-DATA">
<div class="modal-footer">
<button name="login-submit"
type="submit">
<i class="fas fa-plus"></i>
</button>
</div>
</form>
and this is my controller:
#PostMapping("/add-data")
public String addData(#RequestParam String URL) {
... do logic...
return "index";
}
I get correct input value in my addData() method, but I cannot find a way to pass an id from object that was clicked on a list in a first place.
I need this object id to be able to correctly store additional information.
I think of maybe doing separate page for adding this information for each record but that seems not right with easy and small eddits/additions.
Is there any way to be able to add edits on modals in Spring Boot? I was not able to find proper answer for that anywhere, so here I am.
You are passing the URL param to the Controller, simply pass the value to the controller in the same way.
The glue you need is in JavaScript. When the user clicks the button to open the modal, use JS to copy the value to a hidden input field on the modal Form tag. When the form is submitted, then your copied value will also be submitted and available in your controller. You can write the hidden input field with a dummy value and then change the value via JS.
<input type="hidden" id="myHiddenField" name="myHiddenField" value="CHANGE_ME">
Your button to open the modal can look like this:
<i class="fas fa-plus" onclick="document.getElementById('myHiddenField').value='${object.id}'"></i>
Now hidden field in modal form has the value you want.
Hi i have spring application which shows some values from the database in a table from jsp.
Now i want to add functionality that when a user clicks on one row to know which row he has clicked to delete or edit row
How is that to be accomplished.
When you compile jsp page you can assign an id to every row. Put this id inside hidden and send it back on delete or update action.
<tr>
<td>
<form name="myform" action="..." method="...">
<input type="hidden" name="Language" value="rowNumber">
<input type="submit" value="Delete">
<br><br>
</div>
<td>
</tr>