在Blade(Laravel)模板循环中添加确认模式(Bootstrap)

时间:2014-04-13 01:36:51

标签: php twitter-bootstrap laravel

我正在尝试在用户表中单击所需用户的删除按钮时创建一个很好的确认屏幕。我的问题是我无法使用Bootstrap模式来删除用户,因为如果模态没有放在循环中,删除最后一个用户,但如果在循环内部,它将打印与找到的条目一样多的模态

我的代码如下(将模态置于循环之外,它应该在哪里!)

<div class="panel panel-default">
    <div class="panel-body">
        <div class="row">
            <div class="col-md-9">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td>Usuario</td>
                            <td>Nombre</td>
                            <td>Apellido</td>
                            <td>Email</td>
                            <td>Acceso</td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($users as $key => $value)
                            <tr>
                                <td>{{ $value->username }}</td>
                                <td>{{ $value->first_name }}</td>
                                <td>{{ $value->last_name }}</td>
                                <td>{{ $value->email }}</td>
                                <td>{{ $value->role }}</td>
                                <td>
                                    <a class="btn btn-small btn-success" href="{{ URL::to('usuarios/' . $value->id) }}">Mostrar</a>
                                    <a class="btn btn-small btn-warning" href="{{ URL::to('usuarios/' . $value->id . '/edit') }}">Editar</a>
                                    <a class="btn btn-small btn-danger" href="#" data-toggle="modal" data-target=".delete-user-modal">Borrar</a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <div class="col-md-3">
                <a class="btn btn-small btn-info" href="#" data-toggle="modal" data-target=".create-user-modal">Crear usuario</a><br />
                {{ HTML::ul($errors->all()) }}
            </div>
        </div>
    </div>
</div>

<div class="modal fade delete-user-modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" 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">Eliminar usuario</h4>
            </div>
            <div class="modal-body">
                <p>¿Estás seguro que deseas elminar el usuario <strong>{{ Auth::User()->username }}</strong>?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
                &nbsp;
                {{ Form::open(array('url' => 'usuarios/' . $value->id, 'class' => 'pull-right'))  }}
                    {{ Form::hidden('_method', 'DELETE') }}
                    {{ Form::submit('Borrar', array('class' => 'btn btn-primary')) }}
                {{ Form::close() }}
            </div>
        </div>
    </div>
</div>

public function destroy($id)
{
    // buscar y eliminar usuario
    $user = User::find($id);
    $user->delete();

    // redirigir a usuarios con mensaje mensaje
    return Redirect::to('usuarios');
}

1 个答案:

答案 0 :(得分:0)

通过将{{$ value-&gt; id}}添加到模态CSS类来轻松修复它,允许我只打开我在循环中单击的那个。代码如下:

<a class="btn btn-small btn-danger" href="#" data-toggle="modal" data-target=".delete-user-modal{{ $value->id }}">Borrar</a>

<div class="modal fade delete-user-modal{{ $value->id }}" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" 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">Eliminar usuario</h4>
        </div>
        <div class="modal-body">
            <p>¿Estás seguro que deseas eliminar el usuario <strong>{{ $value->username }}</strong>?</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
            &nbsp;
            {{ Form::open(array('url' => 'usuarios/' . $value->id, 'class' => 'pull-right'))  }}
                {{ Form::hidden('_method', 'DELETE') }}
                {{ Form::submit('Borrar', array('class' => 'btn btn-primary')) }}
            {{ Form::close() }}
        </div>
    </div>
</div>