Laravel - 关于创造"删除"按钮和显示确认

时间:2018-03-01 04:44:09

标签: php laravel laravel-5 delete-row

我有index.blade.php:

(((quser) -replace '^>', '') -replace '\s{2,}', ',').Trim() | ForEach-Object {
    if ($_.Split(',').Count -eq 5) {
        Write-Output ($_ -replace '(^[^,]+)', '$1,')
    } else {
        Write-Output $_
    }
} | ConvertFrom-Csv

正如您所看到的那样,我点击了来源:"

@extends('backend.layouts.app')
@section('title')
Danh sách sinh viên
@endsection
@section('content')
@if(session()->has('status'))
<div class="callout callout-info">
     {{ session('status') }}
    </div>
@endif
<div class="box">
            <div class="box-header">
               {{csrf_field()}}
              <h3 class="box-title">Danh sách sinh viên</h3> <br>
              <a href="{{route('sinhvien.create')}}"><i class="fa fa-fw fa-user-plus"></i> Bấm vào đây để thêm dữ liệu </a>
              <div class="box-tools">
                <div class="input-group input-group-sm" style="width: 150px;">
                  <input type="text" name="table_search" class="form-control pull-right" placeholder="Search">
                  <div class="input-group-btn">
                    <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
                  </div>
                </div>
              </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body table-responsive no-padding">
              <table class="table table-hover">
                <tbody><tr>
                  <th>MSSV</th>
                  <th>Họ Tên</th>
                  <th>Ngày sinh</th>
                  <th>Ngày đăng kí </th>
                  <th>Giới tính</th>
                  <th>SĐT</th>
                  <th>CMND</th>
                  <th>Email</th>
                  <th>Quê quán</th>
                  <th>Sồ tiền TK</th>
                  <th>Phòng</th>
                  <th>Hành động</th>
                </tr>
                @foreach($dssv as $sinhvien)
                <tr>
                  <td>{{$sinhvien->sv_ma}}</td>
                  <td>{{$sinhvien->sv_hoTen}}</td>
                  <td>{{$sinhvien->sv_ngaySinh}}</td>
                  <td>{{$sinhvien->sv_ngayDk}}</td>
                   <td><?php echo ($sinhvien->sv_gioiTinh==1)?'Nam':'Nữ' ?></td>
                  <td>{{$sinhvien->sv_dienThoai}}</td>
                  <td>{{$sinhvien->sv_CMND}}</td>
                  <td>{{$sinhvien->sv_Email}}</td>
                  <td>{{$sinhvien->sv_queQuan}}</td>
                  <td>{{$sinhvien->sv_soTienTK}}</td>
                  <td>{{$sinhvien->phong->p_ten}}</td>
                  <td><button class="btn btn-default"><a href="{{ route('sinhvien.edit',['sinhvien' => $sinhvien->sv_id])}}"><i class="fa fa-fw fa-wrench"></i></a></button>
                      <button class="btn btn-danger" data-toggle="modal" data-target="#modal-danger" value="{{$id=$sinhvien->sv_id}}"><i class="fa fa-fw fa-trash-o"></i>
                      </button>

                  </td>
                </tr>
                    <!-- /.modal -->

                @endforeach
              </tbody></table>
            </div>
            <!-- /.box-body -->
          </div>
  <div class="modal modal-danger fade"  id="modal-danger">
          <div class="modal-dialog">
            <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">Cảnh báo</h4>
              </div>
              <div class="modal-body">
                <p>Bạn có muốn xóa sinh viên <?php echo $id?> không?</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Đóng</button>
                <form  method="POST" action="{{ route('sinhvien.destroy',['sinhvien' => $id])}}">{{ method_field("DELETE")}}
                       {{csrf_field()}}<input type="submit" name="btnxoa" class="btn btn-danger" value="Xóa"></input></form>
              </div>
            </div>
            <!-- /.modal-content -->
          </div>
          <!-- /.modal-dialog -->
        </div>  


@endsection

&#34; 然后,它将显示确认是&#34;您要删除此行吗?&#34;来自消息来源:

 <button class="btn btn-danger" data-toggle="modal" data-target="#modal-danger" value="{{$id=$sinhvien->sv_id}}"><i class="fa fa-fw fa-trash-o"></i>
                          </button>

我的问题是我弄错了#34; sv_id&#34; from(我要删除的每一行)。 我需要你的帮助,提出更好的想法。非常感谢你 Display incorrect sv_id enter image description here

3 个答案:

答案 0 :(得分:0)

首先,您无法将按钮值发送到bootstrap模型。你应该使用data-id =&#34; {{$ sinhvien-&gt; sv_id}}&#34;代替值=&#34; {{$ id = $ sinhvien-&gt; sv_id}}&#34;

然后在此按钮上使用Jquery onclickfunction并获取data-id属性的值。在模型表单中设置值,然后触发模型弹出窗口。

答案 1 :(得分:0)

这是我的App \ http \ SinhvienController.php:

public function destroy($id)
    {
           try
        {
            $sinhvien=Sinhvien::find($id);
            $sinhvien->delete();
            $phong=Phong::find($sinhvien->p_id);
            $phong->p_soNguoi=$phong->p_soNguoi-1;
            if($phong->p_trangThai==2)
            {
                $phong->p_trangThai=1;
            }
            $phong->save();
            return redirect(route('sinhvien.index'));
        }
        catch(QueryException $ex)
        {
            return response(['error'=> true ,'message'=> $ex->getMessage()],500);
        }
        //
    }

答案 2 :(得分:0)

您也可以使用jQuery或Javascript显示此删除确认消息。

HTML部分:

<button class="btn btn-danger deleteBtn" sv_id="{{$sinhvien->sv_id}}">
<i class="fa fa-fw fa-trash-o"></i>
</button>

Javascript部分:

<script>
    $(document).on('click', '.deleteBtn', function(){
    var sv_id = $(this).attr('sv_id');
    var confirmation = confirm('Do you want to delete this row '+sv_id);
    if(confirmation == true){
    //Do whatever you want
    } else {
    return false;
    }
    });
</script>