Thymeleaf modal data-target and id update in the loop - spring-boot

I'm building a Thymeleaf+SpringBoot application and I have a moment with Thymeleaf foreach loop where I have a modal inside one div - it repeats few times and I want to update index of a modal and the button which shows it so that my modal will be shown by particular button. I have an issue with updating index because when I do it then my button stop to work and no modal is shown...
I checked:
a) How can I do data-target="#userId" in thymeleaf
b) using data-* attribute with thymeleaf
but still it does not work...
here is part of the code:
The loop:
<div th:each="myth : ${allMyths}">
here is button (commented code also does not work...):
<button type="button" class="button" style="float: right; margin-right: 2%" data-toggle="modal"
data-th-target="'#'+${myth.getId()}">More...
<!--th:attr="data-target='#'+${myth.getId()}">More...-->
</button>
and here is top of the modal:
<div data-th-id="${myth.getId()}" class="modal fade" role="dialog">
here also th:id does not work...
Do you have any idea why button and modal does not find each other by ID?
Thanks in advance for your answers/suggestions!
Piotr

Button:
<button type="button" class="button" style="float: right; margin-right: 2%" data-toggle="modal" th:attr="data-target='#'+${myth.getId()}">More...</button>
Top of the Modal:
<div th:attr="id=${myth.getId()}" class="modal fade" role="dialog">

Here is how it works in my Spring book app + thymeleaf views:
<tbody>
<tr th:each="stockItem,index :${stock}">
<td th:text="${stockItem.id}"></td>
<td th:text="${stockItem.productName}"></td>
<td th:text="${stockItem.productPrice}"></td>
<td th:text="${stockItem.productQuantity}"></td>
<td>
<a class="btn btn-danger" role="button"
th:href="#{/stock/removeAll/{id}(id=${stockItem.id})}">
Remove all
</a>
<br>
<a data-target="#removeSetStockQuantityModal" data-toggle="modal"
th:attrappend="data-target=${stockItem.id}" class="btn btn-danger" role="button">Set Quantity</a>
<!-- Modal -->
<div class="modal fade" id="removeSetStockQuantityModal"
th:attrappend="id=${stockItem.id}" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete <span
th:text="${stockItem.productName}"></span></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="#" method="get"
th:action="#{/stock/delete}"
th:object="${stockItem}">
<input hidden name="id" th:value="${stockItem.id}"/>
<label th:for="${stockProduct.productQuantity}">
<input type="number"
placeholder="Enter quantity to delete" th:default="0"
th:field="${stockProduct.productQuantity}"/>
</label>
<input type="submit" class="btn btn-danger" value="Delete"/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">Cancel
</button>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>

Related

What are the possible reasons why modal doesn't shows?

I have two button for modal and created two modal included in my blade file which I'm trying to use but it doesn't seem to work, I tried deleting the other button but doesn't seem to work, which I assumed to be the case
What could have been the reason for the modals not to show up?
this is my button modals
<td class="table-danger"><button id="aaaBtn" class="soldbtn btn-warning" data-toggle="modal" data-target="#soldModal{{$data->id}}">SOLD</button></td>
<td class="table-warning"><button id="bbbBtn" class="updatebtn btn-success" data-toggle="modal" data-target="#updateModal{{$data->id}}" >UPDATE</button></td>
<!-- Modal -->
<div class="modal fade" id="updateModal{{$data->id}}" tabindex="-1" role="dialog" aria-labelledby="updateModalLabel{{$data->id}}" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<img src="assets/images/rice.png">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="col-lg-12">
<div class="contact-form">
<form id="contact" action="{{url('/reservation')}}" method="post" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-lg-12 col-sm-12" align="center">
<h3>{{$data->title}}</h3>
<img height="200" width="200" src="productimage/{{$data->image}}">
<h5 class='m-2'>~ KILOS ~</h5>
</div>
</div>
<div class="row col-12">
#foreach($modal_data as $modal_data)
<div class="row sm-6 ml-4 mb-1" class="no-gutters" style="height:25px; width: auto;">
<p class='text-dark mr-2'><input type="checkbox" name="prod_kilos[]" value="{{$modal_data->kg}}" id="checkitem" class="check-cls"/> {{$modal_data->kg}}kg </p>
<p class='text-dark'>Qty:</p><input style="width:80px; height:25px;" type="number" name="prod_qty[{{$modal_data->kg}}]" min="1" value="1" max="{{$modal_data->warehouse_qty}}" class="form-control ml-2">
<input type="hidden" name="product_name[{{$modal_data->kg}}]" value="{{$modal_data->title}}">
<input type="hidden" name="product_fee[{{$modal_data->kg}}]" value="{{$modal_data->price}}">
<input type="hidden" name="prod_id[{{$modal_data->kg}}]" value="{{$modal_data->id}}">
</div>
#endforeach
</div>
<div class=" col-lg-12 mt-5">
<button name="submit" type="submit" id="form-submit" class="submit-btn" >ADD TO CART</button>
</div>
</form>
</div>
</div>
<script>
</div>
</div>
</div>
<!-- End Modal -->
What is the problem?
I think the reason why it is not working is because you set wrong data-target to the button:
<button data-target="#updateModal">UPDATE</button>
But when creating modal you use:
<!-- Modal -->
<div id="updateModalModal{{$data->id}}">
</div>
<!-- End Modal -->
Your data-target attributes does not correspond with each other.
How to solve?
The data-target attribute should point to an existing element in page, changing it to:
<button data-target="#updateModal{{$data->id}}">UPDATE</button>
<!-- Modal -->
<div id="updateModal{{$data->id}}">
</div>
<!-- End Modal -->
should solve the problem.
Hope it helps. Read more about it here.

Cannot call modal in another page

I make button on index.php file and want call modal in another page, because when modal in same file and i refresh the page modal seen temporarily.
index.php
<a href="modal.php" data-target="#cancel{{ $form->id }}" class='btn btn-danger btn-sm' data-toggle="modal">Cancel</button>
modal.php
#foreach($list_form as $form)
<div class="modal fade" id="cancel{{ $form->id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header" style="background-color: red;">
<h3 class="modal-title h3" id="exampleModalSmLabel" style="color: white;"><b>Cancel Description</b>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><h1><b>×</b></h1></span>
</button>
</h3>
</div>
<div class="modal-body">
#if(auth()->user()->role == 'Admin')
<form action="{{ route('form001.cancel', $form->id) }}" method='post'>
#csrf
<div class="form-group">
<label for="cancel_deskripsi">Alasan Cancel</label>
<textarea class="form-control" id="cancel_deskripsi" name="cancel_deskripsi" placeholder="tuliskan alasan anda..."></textarea>
</div>
<div class="modal-footer">
<button type='submit' class='btn btn-danger btn-sm'>Submit</button>
</div>
</form>
#endif
{{ $form->cancel_deskripsi }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endforeach
Am I doing writing anything wrong?

How to retrieve the id from iteration to the bootstrap modal?

I need to capture the id from the iteration, but it seems to capture the last id which it's seems to be obviously,
Am using bootstrap modal which i even don't know how they code their jQuerys'.
Thus how can i do to make this button not capturing the last id?
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#applications_{{$user->id}}" id="#applications_{{$user->id}}">View applications</button>
<!-- Modal -->
<div class="modal fade" id="applications_{{$user->id}}" role="dialog" >
<div class="modal-dialog" >
<!-- Modal content-->
<div class="modal-content" style="width: 600px !important;">
<div class="modal-header">
{{-- <button type="button" class="close" data-dismiss="modal">×</button> --}}
<h4 class="modal-title">Applications</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
<table class="table table-hover">
<tr>
<th>Company name</th>
<th>Location</th>
<th>Program</th>
<th>Application</th>
<th>Actions</th>
</tr>
#foreach($user->post as $form)
<tr>
<td>{{$form->pivot->company_name}}</td>
<td></td>
<td>{{$form->pivot->id}}</td>
<td>
/* this button is only capturing the last id from iteration */
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#application_{{$form->pivot->id}}" >View {{$form->pivot->file}}</button> </td>
<td></td>
</tr>
#endforeach
{{$user->name}}
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="application_{{$form->pivot->id}}" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Application</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
<div class="center image">
<img src="/storage/file/{{$form->pivot->file}}" class="img-round" alt="User Image">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Expected to retrieve an image from it's respectively post, but it only works with the last id, no errors.
Your second modal is outside the foreach, then id="application_{{$form->pivot->id}}" only works with the last value it took in the loop. To do it with a blade, you can redo the loop and create a modal for each id and image:
<!-- 2nd Modal -->
#foreach($user->post as $form)
<div class="modal fade" id="application_{{$form->pivot->id}}" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Application</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
<div class="center image">
<img src="/storage/file/{{$form->pivot->file}}" class="img-round" alt="User Image">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endforeach

pass data from table to modal using angular

Im using Spring MVC and AngularJS to build my app and I been looking for a way to pass a table registry data to a modal in order to edit that registry but I haven´t find a working example so, thats my question how can a I pass the registry data to a modal?
This is what I have so far:
Table
<table class="table table-hover table-responsive">
<thead class="table-striped">
<th>Email</th>
<th>Nombre</th>
<th>Apellidos</th>
<th>Rol</th>
<th>Acciones</th>
</thead>
<tbody>
<tr data-ng-show="allUsuarios.length === 0">
<td colspan="4" class="warning">No hay registros para mostrar</td>
</tr>
<tr data-ng-repeat="usuarios in allUsuarios">
<td>{{usuarios.email}}</td>
<td>{{usuarios.nombre}}</td>
<td>{{usuarios.apellidos}}</td>
<td>{{usuarios.idRol.tipoRol}}</td>
<td>
<input type="hidden" value="{{usuarios.idUsuario}}"/>
<button class="btn btn-primary" data-toggle="modal" data-target="#formModalEditar"><i class="glyphicon glyphicon-pencil"></i></button>
<button class="btn btn-danger" data-toggle="modal" data-target="#formModalEliminar"><i class="glyphicon glyphicon-trash"></i></button>
</td>
</tr>
</tbody>
</table>
And the Modal:
<!--EDIT MODAL-->
<div class="col-lg-12">
<div class="modal fade" id="formModalEditar" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="H2">Editar Usuario</h4>
</div>
<div class="modal-body">
<form role="form" action="add" method="post">
<div class="form-group">
<label>Email</label>
<input class="form-control" type="text" required=""/>
<label>Nombre</label>
<input class="form-control" type="text" required=""/>
<label>Apellidos</label>
<input class="form-control" type="text" required=""/>
<label>Rol</label>
<br>
<div class="btn-group open">
<button data-toggle="dropdown" class="btn dropdown-toggle">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li>Action</li>
</ul>
</div>
</div>
<div class="text-right">
<button type="submit" class="btn btn-success">Guardar</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!--END EDIT MODAL-->
PS: I don´t know why when I press Ctrl+K to format the code, it gets displayed like that, sorry.
First of all you have to define a registery in you angular component
currentRegistery : Registery;
In your table you have to add an onclick on the edit icon of each registery to be able to set the current Registery with the selected registery
<button class="btn btn-primary" data-toggle="modal" data-target="#formModalEditar" onclick=“currentRegistery=usuarios”><i class="glyphicon glyphicon-pencil"></i></button>
Finally on your modal you can deal directly with the currentRegistery object.

How get id from other user in Laravel

I am trying to create a user manager in Laravel, I have implemented methods for editing and deleting registered users in my database, but when I try to delete a selected user, it picks up the id of the logged in user, thus excluding the logged in user account.
Function Destroy from Admin Controller
public function destroy($id)
{
$user = User::find($id);
$user->delete($id);
}
View from Admin index
#extends('layouts.sidebar')
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="panel-heading">
<h2>Gerenciamento de usuários</h2>
<div class="panel-body">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#insertUser">
Adicionar Usuário
</button>
<!-- Modal -->
<div class="modal fade" id="insertUser" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Cadastro de usuários</h4>
</div>
<div class="modal-body">
<form class="" action="{{ url('/admin/criausuario')}}" method="post">
{{ csrf_field() }}
<label for="nome">Nome</label>
<input type="text" class="form-control" id="nome" placeholder="Nome Completo" name="nome"></br>
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Email" name="email"></br>
<label for="senha">Senha</label>
<input type="password" class="form-control" id="senha" placeholder="Senha" name="senha"></br>
<label for="tipo">Tipo</label>
<select class="form-control">
<option value="0">Usuário</option>
<option value="1">Administrador</option>
</select>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<button type="submit" class="btn btn-default">Cadastrar</button>
</div>
<div class="modal-footer">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<table class="table">
<tr>
<th>Avatar</th>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
<th>Ações</th>
</tr>
<tr>
#foreach($users as $user)
<td> <img src="{{$user->avatar}}" alt="" style="width: 75px; border-radius: 50%;"> </td>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td><i class="glyphicon glyphicon-edit"></i>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#confirmacao">
<i class="glyphicon glyphicon-remove"></i>
</button></td>
</tr>
<div class="modal fade" id="confirmacao" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Confirmação</h4>
</div>
<div class="modal-body">
Você tem certeza que deseja excluir o usuário selecionado?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<button type="button" class="btn btn-danger">Excluir</button>
</div>
</div>
</div>
</div>
#endforeach
</table>
</div>
</div>
</div>
</div>
{{ $users->links()}}
#endsection
Route
Route::get('/admin', 'AdminController#index')->middleware('admin');
Route::get('/admin/editar/{id}', 'AdminController#edit')->middleware('admin');
Route::post('/admin/editar/{id}', 'AdminController#update')->middleware('admin');
Route::post('/admin/criausuario/', 'AdminController#store');
Route::get('/admin/deletar/{id}', 'AdminController#destroy')->name("user::deleteGet")->middleware('admin');
Any suggestion?

Resources