Javascript 从WinJS.Binding.List中删除项目

Javascript 从WinJS.Binding.List中删除项目,javascript,windows-8,winjs,Javascript,Windows 8,Winjs,我查看了使用DataSources 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.

我查看了使用DataSources 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);
        }
    });
if(list2.selection.count()>0){
list2.selection.getItems().done(函数项){
//对所选内容进行排序,以确保其索引顺序正确
items.sort(函数CompareForSort(item1,item2){
var first=item1.index,second=item2.index;
如果(第一个===第二个){
返回0;
}
否则如果(第一次<第二次){
返回-1;
}
否则{
返回1;
}
});
//向后工作,因为删除会影响后续项目的索引
对于(var j=items.length-1;j>=0;j--){
//要删除这些项目,请调用列表上的splice,传入计数,不进行替换
字母列表拼接(项目[j].索引,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示例中的所有额外代码即可完成此操作。

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

所以代码应该更像这样。我还没有测试过这个

// 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);
这样称呼:

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

Bam,你有一个单行程序。

我会在数据源上使用remove方法,所以同样的事情,但是更长:):

if(list2.selection.count()>0){
list2.selection.getItems().done(函数项){
//对所选内容进行排序,以确保其索引顺序正确
items.sort(函数CompareForSort(item1,item2){
var first=item1.index,second=item2.index;
如果(第一个===第二个){
返回0;
}
否则如果(第一次<第二次){
返回-1;
}
否则{
返回1;
}
});
//向后工作,因为删除会影响后续项目的索引
对于(var j=items.length-1;j>=0;j--){
var _dataSource=list2.itemdasource;
//开始编辑序列
_dataSource.beginEdits();
//获取将添加到现有项目源的新项目
var newItems={“id”:selection[i].data.id,“name”:selection[i].data.name};
//删除最后一项
_dataSource.remove(_dataSource.itemFromIndex(indicesList[j])。_value.key);
//如果需要,您甚至可以添加新项目
//_insertAtStart(null,newItems);
//结束该批编辑
_dataSource.endEdits();
}
});
}

我正在删除多个项目。