2012-12-14 37 views
1

Controls_ListViewWorkingWithDataSources MSDN 샘플에서 WinJS.Binding.List에서 항목을 제거하는 방법을 살펴 보았습니다. 여기에 그 해결책이 나와 있습니다. 더 쉬운 방법이 있다고 말해주세요. 해당 항목이 연속적인 순서가 아닌지 때 목록에서 여러 항목을 삭제하는 지원하기 때문에 MSDN 샘플에서 항목을 제거하는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

그리고 빵은 효과가있었습니다! 고마워요. –

1

코드는 더 복잡하다. 코드에서 list2.selection.getItems()을 사용하여 목록에서 현재 선택된 항목을 모두 검색하는 위치에 유의하십시오. 예를 들어 [1,2,3,4,5,6,7,8,9,0]이 포함 된 목록이 있으면 MSDN 샘플 코드를 통해 사용자는 항목 1,2,4,4를 여러 번 선택하고 삭제할 수 있습니다. 7,9 목록에 [3,5,6,8,0]을 남겨 둡니다.

WinJS.Binding.List (또는 여러 개의 연속 항목)에서 하나의 항목 만 삭제하려는 경우 WinJS.Binding.List.splice()를 한 번 호출하고이 항목을 모두 건너 뛰고 MSDN 샘플의 추가 코드

+0

나는 여러 항목을 제거하고 있습니다. –

1

나는 데이터 소스에 제거 방법을 사용하므로 같은 일을하지만 더 이상 :) :

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(); 

    } 
}); 

}