2012-12-14 73 views
1

我查看了Controls_ListViewWorkingWithDataSources MSDN示例,瞭解如何從WinJS.Binding.List中刪除一個項目,以下是他們的解決方案。請告訴我有一個更簡單的方法。從WinJS.Binding.List中刪除一個項目

if (list2.selection.count() > 0) { 
    list2.selection.getItems().done(function (items) { 

     //Sort the selection to ensure its in index order 
     items.sort(function CompareForSort(item1, item2) { 
      var first = item1.index, second = item2.index; 
      if (first === second) { 
       return 0; 
      } 
      else if (first < second) { 
       return -1; 
      } 
      else { 
       return 1; 
      } 
     }); 

     //Work backwards as the removal will affect the indices of subsequent items 
     for (var j = items.length - 1; j >= 0; j--) { 
      // To remove the items, call splice on the list, passing in a count and no replacements 
      lettersList.splice(items[j].index, 1); 
     } 
    }); 

回答

1

您可以通過使用:iSelection.getIndices();來避免getItems()調用和then塊。 This爲您提供了一個需要刪除的索引的數組。

所以代碼會更像這樣。沒有測試過這個。

// nothing in docs guarantees these are returned in sorted order, so we need to sort 
var indicesList = list2.selection.getindices().sort(function(a,b){return a-b}); 
for (var j = indicesList .length - 1; j >= 0; j--) { 
    // To remove the items, call splice on the list, passing in a count and no replacements 
    lettersList.splice(indicesList[j], 1); 
} 

封裝成這樣的utily類:

function deleteSelectedItemsFromList(selection, list) { 
    var indicesList = selection.getIndices().sort(function(a,b){return a-b}); 
    for (var j = indicesList .length - 1; j >= 0; j--) { 
     // To remove the items, call splice on the list, passing in a count and no replacements 
     list.splice(indicesList[j], 1); 
    } 
} 

這樣調用:

Utils.deletedSelectedItemsFromList(listview.selection, listViewList); 

巴姆,你有一個襯墊。

+2

而Bam它的工作!謝謝,大師! –

1

的代碼刪除MSDN樣本中的產品更爲複雜,因爲它支持刪除從列表中選擇多個項目時,這些項目可能不連續的順序。請注意,他們通過在代碼中使用list2.selection.getItems()來檢索列表中當前選定的所有項目。例如,給定一個包含[1,2,3,4,5,6,7,8,9,0]的列表,MSDN示例代碼將允許用戶多選和刪除項目1,2,4, 7,9將[3,5,6,8,0]留在列表中。

如果您只想從WinJS.Binding.List(或多個連續項目)中刪除單個項目,則可以通過一次調用WinJS.Binding.List.splice()來完成此操作,並跳過所有MSDN示例中的額外代碼。

+0

我刪除多個項目。 –

1

我會使用的數據源remove方法,所以同樣的事情,但不再:):

if (list2.selection.count() > 0) { 
    list2.selection.getItems().done(function (items) { 

    //Sort the selection to ensure its in index order 
    items.sort(function CompareForSort(item1, item2) { 
     var first = item1.index, second = item2.index; 
     if (first === second) { 
      return 0; 
     } 
     else if (first < second) { 
      return -1; 
     } 
     else { 
      return 1; 
     } 
    }); 

    //Work backwards as the removal will affect the indices of subsequent items 
    for (var j = items.length - 1; j >= 0; j--) { 

      var _dataSource = list2.itemDataSource; 
      //Start the sequence of edits 
      _dataSource.beginEdits(); 

      //Get new Items that will be added to the existing item source 
      var newItems = { "id": selection[i].data.id, "name": selection[i].data.name }; 
      //remove the last item 
      _dataSource.remove(_dataSource.itemFromIndex(indicesList[j])._value.key); 

      //YOU CAN EVEN ADD A NEW ITEM IF YOU WANT 
      //_dataSource.insertAtStart(null, newItems); 

      //Ends the batch of edits 
      _dataSource.endEdits(); 

    } 
}); 

}