spring boot passing parameter from list to controller - spring

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.

Related

Thymeleaf iteration stuck at 0 in dynamically created form (Spring Boot)

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.

Error show data when event click livewire

I have an error when showing the data I do not see the problem because the data does not want to show it I hope you can help me.
this my event click on table its work display my modal
<td>
<a href="#" wire:click.prevent="edit({{$pais->id}})" data-toggle="modal" data-target="#update_pais">
<i class="fa fa-search text-warning"></i>
</a>
</td>
This is my component function edit
public $testNew;
public function edit(Pais $pais)
{
$this->testNew = '324234234234';
}
this my input
<input wire:model="testNew" type="text">
when click data doesn't show and input is empty on network show data is correct
Have you tried adding a value attribute to your input?
<input wire:model="testNew" type="text" value="{{ $testNew }}">

Checkbox value in controller in laravel

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

Why does a link with route set to posts.destroy in practice routes to posts.show

#extends('layouts.app')
#section('content')
<ul>
#foreach ($posts as $post)
<li>
<a href="{{route('posts.show', $post->id)}}">
{{$post->title}}
</a>
 
<a href="{{route('posts.edit', $post->id)}}">
Edit
</a>
 
<a href="{{route('posts.destroy', $post->id)}}">
Delete
</a>
#endforeach
</ul>
#endsection
I am new to laravel and in the process of learning. In the code above i have made a simple Unordered list of posts that is in the database. Next to each post is a edit and delete links. The edit link works just fine and invokes edit method in posts controller. But the delete link doesn't work. I think it runs the show method instead of destroy method in posts controller. Why is this the case?
To be more clear i have already somewhat solved the problem with the code bellow inside the foreach directive
<form action="/posts/{{$post->id}}" method="post">
#csrf
<input type="hidden" name="_method" value=" DELETE">
<input type="submit" name="delete" value="Delete" id="">
</form>
But why do this in the first place. I understand when we use form since a html form doesn't support DELETE method i had to add the hidden input. But why doesn't the hyper link tag work. This is the list of routes:
Links always perform GET method by default.
You can mimic DELETE method behavior using some javascript.

Spring + Thymeleaf - Call a function using received parameter

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}" />

Resources