2017-01-16 10 views
0

Я использую CakePHP 3.3, у меня есть список users, и я хочу показать окно подтверждения SweetAlert перед подтверждением отмены отмены конкретного пользователя.CakePHP 3.3 подтвердить удаление записи с помощью SweetAlert

Это фрагмент из src/Template/Users/index.ctp файла, который содержит список пользователей:

<tbody> 
     <?php foreach ($users as $user): ?> 
     <tr> 
      <td><?= $this->Html->image('../'.$user->avatar, ['class' => 'img-avatar']); ?></td> 
      <td><?= h($user->username) ?></td> 
      <td><?= h($user->role) ?></td> 
      <td><?= h($user->created) ?></td> 
      <td><?= h($user->modified) ?></td> 
      <td class="actions"> 
       <span class="label label-default"><?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?></span> 
       <span class="label label-default"><?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?></span> 
       <span class="label label-default"><?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?></span> 
      </td> 
     </tr> 
     <?php endforeach; ?> 
</tbody> 
<button class="btn-del">DUMMY BUTTON</button> 

Тогда у меня есть сценарий в моей src/Template/Layout/default.ctp:

<script> 
    document.querySelector('.btn-del').onclick = function(){ 
     swal({ 
       title: "Are you sure?", 
       text: "You will not be able to recover this imaginary file!", 
       type: "warning", 
       showCancelButton: true, 
       confirmButtonColor: "#DD6B55", 
       confirmButtonText: "Yes, delete it!", 
       closeOnConfirm: false 
      }, 
      function(){ 
       swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
      }); 
    }; 
</script> 

Я попробовал сценарий, он работает с использованием Dummy Button, но я не знаю, как заставить его работать, чтобы он мог подтвердить или отменить удаление записи user с перенаправлением на index.ctp после каждого удаления.

Извините, что я начинаю как в CakePHP, так и в JS & Спасибо заранее.

ответ

0

я решил это, используя понятия ниже:

HTML

<tr class="home-loan-table"> 
    <td><?= $this->Html->image($image, array('class' => 'profile-user-img img-responsive img-circle', 'width' => 100, 'height' => 100, 'alt' => 'User profile picture')) ?></td> 
    <td><?= $testimonial->has('FromUser') ? $testimonial->FromUser->first_name : $testimonial->name ?></td> 
    <td><?= $profile ?></td> 
    <td><?= $testimonial->title ?></td> 
    <!--<td><?= $testimonial->has('user') ? $this->Html->link($testimonial->user->id, ['controller' => 'Users', 'action' => 'view', $testimonial->user->id]) : '' ?></td>--> 
    <td> 
     <div class="table-wrap-elps"> 
      <?= ($testimonial->description) ?> 
     </div> 
    </td> 
    <td><?= $this->Number->format($testimonial->rating) ?></td> 
    <td class="actions text-center" style="white-space:nowrap"> 
     <?= $this->Html->link(__('View'), ['action' => 'view', $testimonial->id], ['class' => 'view-btn']) ?> 
     <?= $this->Html->link(__('Edit'), ['action' => 'edit', $testimonial->id], ['class' => 'edit-icon']) ?> 
     <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $testimonial->id], ['confirm' => __('Confirm to delete this entry?'), 'class' => 'crose-btn hide-postlink']) ?> 
     <a href="javascript:;" class="crose-btn postlink-delete">Delete</a> 
    </td> 
</tr> 

CSS

<style> 
.hide-postlink{ 
    display: none; 
} 
.postlink-delete{ 
    display: inline-block; 
} 

сценарий

<script> 
$(document).on('click', '.postlink-delete', function() { 
    var delete_form = $(this).parent().find('form'); 
    //add sweetalert here then form submit 
    delete_form.submit(); 
});