2016-08-28 2 views
1

보기에 범주가 나열되어 있습니다. 카테고리 삭제 단추는 뷰에 있으며 클릭하면 카테고리가 작동하고 삭제됩니다.Laravel 5.2 - Sweet Alert 확인 상자

내가하고 싶은 일은 카테고리를 삭제하기 전에 달콤한 경고 대화 상자가 나타나서 확인을 요청하는 것입니다. 확인 된 경우 정의 된 경로로 이동하여 카테고리를 삭제해야합니다.

<a id="delete-btn" href="{{ route('admin.categories.destroy', $category->id) }}" class="btn btn-danger">Delete</a>

를하고 스크립트는 다음과 같이 정의된다 :

삭제 링크는 다음과 같이 정의된다

<script> 
    $(document).on('click', '#delete-btn', function(e) { 
     e.preventDefault(); 
     var link = $(this); 
     swal({ 
      title: "Confirm Delete", 
      text: "Are you sure to delete this category?", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Yes, delete it!", 
      closeOnConfirm: true 
     }, 
     function(isConfirm){ 
      if(isConfirm){ 
       window.location = link.attr('href'); 
      } 
      else{ 
       swal("cancelled","Category deletion Cancelled", "error"); 
      } 
     }); 
    }); 
</script> 

그러나, 나는이 범주를 삭제 삭제 버튼을 클릭하면,하지만 달콤한 경고 메시지가 표시되지 않습니다.

Route::get('/categories/destroy/{category}', [ 
    'uses' => '[email protected]', 
    'as' => 'admin.categories.destroy', 
]); 

및 제어기 기능은 다음과 같이 정의된다 :

경로는 다음과 같이 정의되는 어떤 도움이 인정 될 것이다

public function destroy(Category $category) 
{ 

    $category->delete(); 

    //this alert is working fine. however, the confirmation alert should appear 
    //before this one, which doesn't 
    Alert::success('Category deleted successfully', 'Success')->persistent("Close"); 

    return redirect()->back(); 
} 

. 감사.

답변

2

이 시도 :

<script> 
    var deleter = { 

     linkSelector : "a#delete-btn", 

     init: function() { 
      $(this.linkSelector).on('click', {self:this}, this.handleClick); 
     }, 

     handleClick: function(event) { 
      event.preventDefault(); 

      var self = event.data.self; 
      var link = $(this); 

      swal({ 
       title: "Confirm Delete", 
       text: "Are you sure to delete this category?", 
       type: "warning", 
       showCancelButton: true, 
       confirmButtonColor: "#DD6B55", 
       confirmButtonText: "Yes, delete it!", 
       closeOnConfirm: true 
      }, 
      function(isConfirm){ 
       if(isConfirm){ 
        window.location = link.attr('href'); 
       } 
       else{ 
        swal("cancelled", "Category deletion Cancelled", "error"); 
       } 
      }); 

     }, 
    }; 

    deleter.init(); 
</script> 

편집을 : 귀하의 코멘트에서 @ 칼리 - 싱 - 토레의 대답에, 나는 당신이 제대로 블레이드 템플릿에 스크립트를 주입하지 않을 생각합니다. 기본 레이아웃을 확장하는 경우 스크립트를 포함했는지 또는 하위 레이아웃에서 생성했는지 확인하십시오.

+0

일했습니다! 고마워. – byteseeker

0

아래의 방법으로 앵커를 작성하십시오.

<a href="#" customParam="URL">Delete</a> 

이제 href를 customParam으로 변경하십시오.

function (isConfirm) { 
    if (isConfirm) { 
     window.location = link.attr('customParam'); 
    } else { 
     swal("cancelled", "Category deletion Cancelled", "error"); 
    } 
} 

기본적으로 귀하의 경우에는 href와 이벤트 리스너가 모두 클릭됩니다.

+0

작동하지 않았습니다. 이번에는 카테고리가 삭제되지 않습니다. – byteseeker

+0

jquery 이벤트 수신기에서 디버그 및 호출을 한 번 확인하십시오. –