2017-02-22 113 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 successコールバックでDOMから関連する要素を削除します。ビューにコードを表示する必要があります。 –

+0

削除後、AjaxResultまたはNewslistページにリダイレクトするための新しいデータを送信してください。 – 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 question。 。 。 – Kianoush

0

あなたのhtmlコードあなたがトンを必要とするので、

<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

関連する問題