2017-02-22 99 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

你需要ŧ o在成功回調中刪除DOM中的關聯元素。您需要在視圖中顯示代碼。 –

+0

刪除後只需發送新的數據綁定爲AjaxResult或重定向到新聞列表頁面。 – Amit

+0

@Amit這是怎麼回事?請寫下代碼 – 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

編輯問題。 。 。 – Kianoush

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("حذف نشد . خطایی رخ داده"); 
       } 
      } 
     }); 
    } 

修改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或父母身份。 – Amit