2017-02-22 3 views
-1

나는이 주석을 삭제 의견에 쓰십시오. 하지만 내가 데이터베이스에서 제거 할 주석을 삭제할 때 페이지 (보기) 그래서 내가 새로 고침이 페이지에서 제거 할 필요가 제거 할 제거합니다. 이 문제를 어떻게 해결할 수 있습니까?요소를 삭제할 때 목록을 업데이트하지 않습니다

function DeleteNews(id) { 
    jQuery.ajax({ 
     url: "/admin/news/deletenews/" + id, 
     type: 'POST', 
     dataType: "json", 
     success: function (data) { 
      if (data === true) { 
       alert("خبر با موفقیت حذف گردید"); 
      } else { 
       alert("حذف نشد . خطایی رخ داده"); 
      } 
     } 
     }); 
    } 

보기

<table id="example" class="display" width="100%" cellspacing="0"> 
    <thead> 
     <tr> 
      <th>کد خبر</th> 
      <th>عنوان خبر</th> 
      <th>عملیات</th> 
     </tr> 
    </thead> 
    <tbody> 
     @foreach (var item in Model.ListNews) 
     { 
      <tr> 
       <td id="news(@item.NewsID)">@item.NewsID</td> 
       <td>@item.NewsTitle</td> 
       <td> 
        <a href="@Url.Action("/EditNews/",new { NewsID=item.NewsID})" class="btn btn-success btn-lg">ویرایش</a> 
        <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 
         جزئیات 
        </button> 
        <button type="button" onclick="NewsComment(@item.NewsID)" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal"> 
         نظرات 
        </button> 
        <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-default btn-lg" data-toggle="modal" data-target="#myModal"> 
         فایل های مریوطه 
        </button> 
        <button class="btn btn-danger btn-lg" onclick="DeleteNews(@item.NewsID)">حذف</button> 
       </td> 
      </tr> 
     } 
    </tbody> 
</table> 
+0

처럼 조금 수정이

function DeleteNews(id) { jQuery.ajax({ url: "/admin/news/deletenews/" + id, type: 'POST', dataType: "json", success: function (data) { if (data === true) { alert("data deleted"); //below are the different ways to remove the element $('#post-id-'+id).remove(); // removes the element itself leaving others untouched $('#post-id-'+id).empty();// keeps the element but removes all children $('#post-id-'+id).closest("#parent_id").empty(); // travels up the DOM searching for the first parent with the class/id and empties it keeping the parent itself $('#post-id-'+id).closest("#parent_id").remove();// travels up the DOM searching for the first parent and removes it and all its children $('#post-id-'+id).html(' //my new html code here'); // can be used to show that the post has been deleted without showing an alert, much like Facebook does when you unfollow a friend, can also be ("") to empty it } else { alert("حذف نشد . خطایی رخ داده"); } } }); } 

뭔가를 시도 할 수 o 성공 콜백에서 관련 요소를 DOM에서 제거합니다. 뷰에 코드를 표시해야합니다. –

+0

삭제 후 AjaxResult로 바인딩하거나 뉴스 목록 페이지로 리디렉션하기 위해 새로운 데이터를 전송하십시오. – Amit

+0

@ 이것을 어떻게하는가? 해당 코드를 작성하시기 바랍니다 – Kianoush

답변

0
=== // It's a strong comparison 


function DeleteNews(id) { 
    jQuery.ajax({ 
     url: "/admin/news/deletenews/" + id, 
     type: 'POST', 
     dataType: "json", 
     success: function (data) { 
      if (data) { // Just it 
       // And here is select your comment element and remove him 
       $('#comment-' + id).remove(); 
       alert("Comment removed"); 
      } else { 
       alert("Failed"); 
      } 
     } 
    }); 
} 
+0

감사합니다. 그러나 작동하지 않습니다. – Kianoush

+0

edit 질문. . . – Kianoush

0

당신은 당신의 HTML 코드 당신은 t이 필요하므로

<table id="example" class="display" width="100%" cellspacing="0"> 
    <thead> 
     <tr> 
      <th>کد خبر</th> 
      <th>عنوان خبر</th> 
      <th>عملیات</th> 

     </tr> 
    </thead> 
    <tbody> 
     @foreach (var item in Model.ListNews) 
     { 
      <tr id="post-id-news(@item.NewsID)""> 
       <td id="news(@item.NewsID)">@item.NewsID</td> 
       <td>@item.NewsTitle</td> 
       <td> 
        <a href="@Url.Action("/EditNews/",new { NewsID=item.NewsID})" class="btn btn-success btn-lg">ویرایش</a> 
        <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 
         جزئیات 
        </button> 
        <button type="button" onclick="NewsComment(@item.NewsID)" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal"> 
         نظرات 
        </button> 
        <button type="button" onclick="DetailNews(@item.NewsID)" class="btn btn-default btn-lg" data-toggle="modal" data-target="#myModal"> 
         فایل های مریوطه 
        </button> 
        <button class="btn btn-danger btn-lg" onclick="DeleteNews(@item.NewsID)">حذف</button> 
       </td> 
      </tr> 
     } 
    </tbody> 
</table> 
+0

고마워,하지만 그게 작동하지 – Kianoush

+0

편집 질문. . . – Kianoush

+0

@Kianoush - 이것은 단지 논리 일뿐입니다. 나는 당신이 올바른 요소 ID 또는 부모 ID를 이것에 맞추어야한다고 생각합니다. – Amit

관련 문제