1

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

3 回答 3

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 示例。

于 2012-12-14T14:17:09.963 回答
1

您可以通过使用避免 getItems() 调用和 then 阻止:iSelection.getIndices();为您提供了一个包含您需要删除的索引的数组。

所以代码会更像这样。没有测试过这个。

// 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);
}

将其封装成一个实用类,如下所示:

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

巴姆,你有一个班轮。

于 2012-12-21T14:27:34.960 回答
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();

    }
});

}
于 2014-01-21T15:42:16.077 回答