2017-03-21 48 views
0

編輯防止重複模式我試圖利用引導模態編輯。我的問題是,我目前的方式是在foreach循環中包含模態。在Laravel

@foreach($users as $user) 
    <tr> 
     <td align="center"><input type="checkbox" class="i-checks" name="input[]" value="{{ $user->name }}"></td> 
     <td>{{ $user->name }}</td> 
     <td>{{ $user->email }}</td> 
     <td> 
      {{ $user->role->name }} 
     </td> 

     <td> 
     @if($user->team_id) 
      {{ $user->team->name }} 
     @else 
      {{ 'N/A' }} 
     @endif 
     </td> 

     <td> 
      @if($user->is_active == 0) 
       {{ 'Blocked' }} 
      @elseif($user->is_active == 1) 
       {{ 'Active' }} 
      @endif 
     </td> 

     <td> 
      <a data-toggle="modal" href="#modal-edit-{{ $user->id }}"><i class="fa fa-pencil"></i> Edit</a> 
     </td> 
     @include('partials.modals.editUser') 
    </tr> 
@endforeach 

我的模式是這樣的

<div id="modal-edit-{{$user->id}}" class="modal fade" aria-hidden="true" role="dialog"> 

<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-body"> 
      <div class="row"> 
       <button type="button" class="close" data-dismiss="modal">&times;</button> 

       <div class="col-sm "><h3 class="text-center m-t-none m-b">Edit User</h3> 

        <form method="POST" action="{{ route('users.update', $user->id)}}"> 
        {{ method_field('PATCH') }} 
        {{ csrf_field() }} 

         <div class="form-group"><label>Name</label> <input type="text" placeholder="Enter name" class="form-control" name="name" value="{{ $user->name }}"></div> 

         <div class="form-group"><label>Email</label> <input type="email" placeholder="Enter email" class="form-control" name="email" value="{{ $user->email }}"></div> 


         <div class="form-group"><label>Password</label> <input type="password" placeholder="Password" class="form-control" name="password" value="{{ $user->password }}"></div> 

         <div class="form-group"> 
          <label for="role">Activation</label> 
           {{ Form::select('activation', 

           ['1' =>'Active', 
           '0' => 'Blocked', 
           ], $user->is_active, array('class' => 'chosen-select', 'data-placeholder' => 'Choose Activation', 'tab-index' => '4')) }} 
         </div> 


         <div class="form-group"> 
          <label for="role">Select Role</label> 
           {{ Form::select('role', 
           [1 =>'Admin', 
           2 => 'Team Leader', 
           3 => 'Team Member', 
           ], $user->role_id, array('class' => 'chosen-select', 'data-placeholder' => 'Choose a role', 'tab-index' => '4')) }} 
         </div> 


         <div class="form-group"><label>Team</label> <input type="text" placeholder="Enter Team" class="form-control" name="team_id" value="{{ $user->team_id }}"> </div> 

         <input type="submit" class="btn btn-primary btn-rounded btn-block" onclick="pressOnlyOnce()" value="Save Changes"> 

        </div> 
       </form> 
      </div> 
     </div> 
    </div> 
</div> 

我怎麼能只用一個模態編輯我的數據,這樣我可以把它放在foreach循環之外?

回答

0

您可以移動包括@foreach循環外,並添加您的變量來觸發與JavaScript中使用它們的模式的按鈕。

的按鈕應該是這樣的:

<a data-toggle="modal" href="#modal-edit-{{ $user->id }}" data-user-id="{{ $user->id }}" data-user-active="{{ $user->active }}"><i class="fa fa-pencil"></i> Edit</a>