2015-08-21 164 views
0

記錄(comment)嵌套在foreach循環中。當我刪除記錄時,它從數據庫中刪除,但不從列表中刪除。當我刷新頁面時,它從列表中刪除。已刪除的記錄沒有從列表中刪除

我的HTML代碼是:

<span data-bind="foreach:showAds"> 
    //some stuff 
    <span data-bind="foreach:showComment"> 
     <span data-bind="attr:{'id':id}"> 
      <span data-bind="text:description"></span> 
      <span data-bind="text:postedById"></span> 
      <span data-bind="click:function(){ $parent.deleteComment($data.id)}">delete</span> 
     </span> 
    </span> 
</span> 

js代碼是:

function comment(data) { 
     var self = this; 
     data = data || {}; 
     self.description = ko.observable(data.description); 
     self.postedById = data.postedById; 
     self.adId = data.adId; 
     self.id = data.id; 
    } 
    function ad(data) { 
     var self = this; 
     data = data || {}; 
     //some stuff 
     self.showComment = ko.observableArray(); 
     if (data.comment) { 
      var cmt = $.map(data.comment, function (item) { return new comment(item); }); 
      self.showComment(cmt); 
     } 
     self.deleteComment = function (id) { 
      $.ajax({ 
       url: '/api/Comment/DeleteComment/' + id, 
       dataType: "json", 
       contentType: "application/json", 
       cache: false, 
       type: 'POST', 
       success: function (data) { 
        //Also how to map single object? $.map() is not working for single object so I mapped it manually. 
        var com = new comment(); 
        com.id = data.Id; 
        com.description = data.description; 
        com.adId = data.adId; 
        self.showComment.remove(com); //whats wrong here? 
       }, 
       error: function() { 
        toastr.error("failed to delete comment", "Error!"); 
       } 
      }); 
     } 
    } 
    function viewModel() { 
     var self = this; 
     self.showAds = ko.observableArray(); 
     //load data using ajax and map in showAds. 
    } 
    ko.applyBindings(new viewModel()); 
+0

您的代碼需要注意幾點: 'VAR CMT = $ .MAP( data.comment,function(item){return new comment(item);});' 如果您執行'new',建議以大寫字母開頭函數名稱。所以這將是新的評論(數據);另外,如果您的後端具有用大寫字母I編寫的'id'屬性,則在函數構造函數中,您應該使用'data.Id'來定義'self.id'。這樣你可以直接把BE結果扔給構造函數。 –

+0

感謝您的解釋。 –

回答

2

看看這個官方文檔例子

<ul data-bind="foreach: places"> 
    <li> 
     <span data-bind="text: $data"></span> 
     <button data-bind="click: $parent.removePlace">Remove</button> 
    </li> 
</ul> 

<script type="text/javascript"> 
    function MyViewModel() { 
     var self = this; 
     self.places = ko.observableArray(['London', 'Paris', 'Tokyo']); 

     // The current item will be passed as the first parameter, so we know which place to remove 
     self.removePlace = function(place) { 
      self.places.remove(place) 
     } 
    } 
    ko.applyBindings(new MyViewModel()); 
</script> 

見的第一個參數是當前元素(所以你可以要求該元素的id) 。

self.deleteComment = function (comment) { 
    $.ajax({ 
     url: '/api/Comment/DeleteComment/' + comment.id, 
     type: 'POST', 
     success: function (data) { 
      self.showComment.remove(comment); 
     } 
    }); 
} 

現在還記得,即可使用方法,而無需創建一個新的功能

click: $parent.deleteComment 

,而不是

click: function(){ $parent.deleteComment($data.id)} 
+0

謝謝!如何在quesiton(註釋代碼) –

+0

[$ .map](http://api.jquery.com/jquery.map/)中提到的如何映射單個對象需要一個數組,它用於對每個元素進行轉換。 。我不覺得它在你的部分有用。 我會做的是使用它的構造函數實例化註釋。 var com = new comment({ id:data.Id, description:data.description, adId:data。adId });' 但是請記住,現在使用這個新修復程序,不需要創建要刪除的評論的新實例,所以你很好:) –

1

嘗試使用自定義remove()函數。正常刪除正試圖匹配一個完全匹配的對象,而且您沒有填充所有字段。既然你已經有了ID,只需使用:

self.deleteComment = function (id) { 
     $.ajax({ 
      url: '/api/Comment/DeleteComment/' + id, 
      dataType: "json", 
      contentType: "application/json", 
      cache: false, 
      type: 'POST', 
      success: function (data) { 
       self.showComment.remove(function(item){ 
        return item.id == id; 
       }); 
      }, 
      error: function() { 
       toastr.error("failed to delete comment", "Error!"); 
      } 
     }); 
    } 
+0

我不明白自定義刪除功能。請提供執行自定義刪除功能。 –

+0

我做到了。你甚至看到我的答案? – dfperry

+0

是啊它的接收項目作爲參數,使我困惑 –