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