2014-09-20 3 views
0

cakephp.Itries에서 jquery로 레코드를 삭제할 때 다음 코드가 표시되지만 실제 출력을 제공하지 않습니다. 페이지를 새로 고칠 때 레코드가 삭제됩니다. 삭제 링크를 클릭하면 팝업 메시지가 나타납니다. 레코드를 삭제하십시오.cakephp에서 jquery로 레코드를 삭제하는 방법은 무엇입니까?

https://gist.github.com/taleeb35/e61f58af9944511b1669

이 내 요점 코드입니다.

public function delete($id = null) { 
    if($this->request->is('ajax')) { 
    //$this->autoRender = false; 
    if ($this->User->delete($id)) { 
     $response = $this->Session->setFlash(__('User deleted')); 
     $response .= $this->redirect(array('action' => 'index')); 
    } else { 
     $response = $this->Session->setFlash(__('User was not deleted')); 
     $response.= $this->redirect(array('action' => 'index')); 
    } 
    return json_encode($response); 
    } 
} 


$(document).ready(function() { 
    $('a.delete').click(function(e) { 
     var __this = this; 
     e.preventDefault(); 
     var parent = $(this).parent("td").parent("tr"); 
     $.ajax({ 
      type: 'get', 
      url: $(__this).attr("href"), 
      beforeSend: function() { 
       parent.animate({'backgroundColor':'#fb6c6c'},3000); 
      }, 
      success: function(response) { 
       if(response.success){ 
        parent.slideUp(300,function() { 
         parent.remove(); 
        }); 
       }else{ 
        alert("Failed to delete message"); 
        parent.animate({'backgroundColor':'#fff'},1000);//Restore your background back 
       } 
      } 
     }); 
    }); 
}); 
+0

같습니다, 내가 먼저 문서를 연구 것을 강력히 제안합니다. 'setFlash()'도'redirect()'도 아무것도 반환하지 않으며,'redirect()는 물론 AJAX 요청에 반환되는 인덱스 액션 출력을 가져 오는 리디렉션을 유발합니다. – ndm

답변

0

귀하의 행동에 몇 가지해야합니다. 먼저 autorender로 주석 처리를 제거하십시오. 이 작업은 아약스 전용이므로 아무 것도 렌더링 할 필요가 없으며 파일을 볼 필요도 없습니다. 두 번째 것은 레이아웃이며, null 또는 ajax 친화적으로 설정되어야합니다. 마지막으로 응답과 응답 방법이 있습니다. 당신은 die()return의 insetad를 사용하십시오. 응답이 js 스크립트에 필요한 모든 정보로 배열되어 있다면 더 좋을 것입니다. 따라서 귀하의 행동은 다음과 같이 보일 수 있습니다 :

public function delete($id = null) { 
    if ($this->request->is('ajax')) { 
     $this->autoRender = false; 
     $this->layout = null; 

     $response = array(); 

     if ($this->User->delete($id)) { 
      $response['success'] = false; 
      $response['message'] = __('User deleted'); 
      $response['redirect'] = Router::url(array('action' => 'index')); 
     } else { 
      $response['success'] = true; 
      $response['message'] = __('User was not deleted'); 
      $response['redirect'] = Router::url(array('action' => 'index')); 
     } 

     echo json_encode($response); 
    } 

    die(); 
} 

두 번째로해야 할 일은 ajax 호출로 반환되는 데이터 유형을 추가하는 것입니다. success 콜백 내 응답을 구문 분석 할 수도 있습니다. 그냥 실제로 당신이 무엇을하는지 모른 채 함께 일을 고집하는 것처럼

dataType: 'json' 

또는 (성공 콜백)

response = JSON.parse(response); 
+0

도움을 주셔서 감사합니다. 내 삭제 기능은 실제로 table.At에서 시간을 tr 화면에서 제거합니다. 내가 페이지를 새로 고침 tr 업데이 트 결과와 함께 다시 온다. 페이지를 새로 고치지 않고 기록하고 tr도 페이지에서 제거해서는 안됩니다. –

+0

그리고 레코드가 업데이트 될 때 확인 메시지를 표시하는 방법은 무엇입니까? –

관련 문제