从WinJS.Binding.List中删除项目

时间:2012-12-14 04:33:51

标签: javascript windows-8 winjs

我查看了Controls_ListViewWorkingWithDataSources 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);
        }
    });

3 个答案:

答案 0 :(得分: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示例中的额外代码。

答案 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)

我会在数据源上使用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();

    }
});

}