我有一个表格,在每行中我通过foreach传递一些变量。
当我点击“删除”时,它会打开一个模式,它具有表格的第一个值,如果我点击ID为5的行中的删除按钮无关紧要,这将获得ID 1的值。 / p>
代码是这样的:
<tbody>
@foreach ($admins as $key => $admin)
<tr>
<td class="idadmin tdcenter"><p id="margindata" class="tdmenuadmin">{{$admin->id}}</p></td>
<td class="nameadmin"><p id="margindata" class="tdmenuadmin">{{$admin->name}}</p></td>
<td class="emailadmin"><p id="margindata" class="tdmenuadmin">{{$admin->email}}</p></td>
<td class="actionsadmin tdmenuadmin">
<button type="button" class="btn btn-danger btn-sm deletemenuadmin" data-toggle="modal" data-target="#formdeleteadmin" id="margindata">Delete</button>
<div id="formdeleteadmin" class="modal fade" role="dialog"> <!-- DIV TO SHOW THE CREATE PROJECT FORM 1 START HERE-->
<div class="modal-dialog" style="background-color:#23517F;">
<div class="modal-content" style="background-color:#23517F;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" style="color:black;">{{$admin->id}}¿Estas seguro de borrar al administrador?</h4>
</div>
<div class="modal-body">
<div class="col-sm-6">
<a href="{{ route('admin.admins') }}" class="btn btn-danger btn-block">No</a>
</div>
<div class="col-sm-6">
<form method="POST" action="{{route('admin.admins.destroy',$admin->id)}}">
<input type="submit" value="Si" class="btn btn-danger btn-block">
<input type="hidden" name="_token" value="{{Session::token()}}">
{{method_field('DELETE')}}
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="closemodal">Close</button>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</tbody>
当我点击删除时,它会显示我的模态:(它被修剪)
如果我单击是,它会进入指示代码并执行控制器功能的路径,功能如下:
public function destroyAdmin($id) //Eliminar la informacion de un admin
{
$admin = Admin::find($id);
$admin->delete();
Session::flash('success','El admin ha sido eliminado con éxito.');
return redirect()->route('admin.admins');
}
当我打开模态时,如何保留行的ID?
答案 0 :(得分:1)
对于所有删除按钮,您的数据目标是相同的。这就是为什么总是打开第一个模型。使用不同的名称为所有data-target.u可以通过添加主键与数据目标来创建动态数据目标 编辑后的代码如下
<tbody>
@foreach ($admins as $key => $admin)
<tr>
<td class="idadmin tdcenter"><p id="margindata" class="tdmenuadmin">{{$admin->id}}</p></td>
<td class="nameadmin"><p id="margindata" class="tdmenuadmin">{{$admin->name}}</p></td>
<td class="emailadmin"><p id="margindata" class="tdmenuadmin">{{$admin->email}}</p></td>
<td class="actionsadmin tdmenuadmin">
<button type="button" class="btn btn-danger btn-sm deletemenuadmin" data-toggle="modal" data-target="#formdeleteadmin{{$admin->id}}" id="margindata">Delete</button>
<div id="formdeleteadmin{{$admin->id}}" class="modal fade" role="dialog"> <!-- DIV TO SHOW THE CREATE PROJECT FORM 1 START HERE-->
<div class="modal-dialog" style="background-color:#23517F;">
<div class="modal-content" style="background-color:#23517F;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" style="color:black;">{{$admin->id}}¿Estas seguro de borrar al administrador?</h4>
</div>
<div class="modal-body">
<div class="col-sm-6">
<a href="{{ route('admin.admins') }}" class="btn btn-danger btn-block">No</a>
</div>
<div class="col-sm-6">
<form method="POST" action="{{route('admin.admins.destroy',$admin->id)}}">
<input type="submit" value="Si" class="btn btn-danger btn-block">
<input type="hidden" name="_token" value="{{Session::token()}}">
{{method_field('DELETE')}}
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="closemodal">Close</button>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
答案 1 :(得分:0)
在表格外只创建一个模态,并使用javascript和打开模态的事件填充字段。
您的HTML将更清晰,并且可以按预期工作。
您可以在按钮上使用属性
<button type="button"
class="btn btn-danger btn-sm deletemenuadmin"
data-toggle="modal"
data-target="#formdeleteadmin"
data-url="{{ route('admin.admins.destroy',$admin->id) }}"
data-username="{{ $admin->name }}"
data-userid="{{ $admin->id}}">
Delete
</button>