1

我希望用户从列表视图中选择一个项目,然后从应用程序栏中单击删除按钮以删除所选项目....
我以前做过很多次这种事情,但是在一个没有任何组的简单列表视图上,但是从 listView 中删除项目的方法在这里不适用,因为它是一个组列表视图,因为它以某种顺序对对象进行排序,因此混合了所有索引(索引)。

这是我用来从简单(未分组)列表视图中删除元素的方法:

var deleteIndex=0;
var listView=document.getElementById('listview').winControl;
listView.addEventListener('selectionchanged', function () {
          deleteIndex=listView.selection.getIndices();
        });

  function deleteButtonOnClick(){
         data.splice(deleteIndex,1);
     }

这不适用于组列表视图,它会删除列表视图中的其他一些项目......我真的被困在这一点上,我必须在明天之前完成我的应用程序。

4

2 回答 2

1

因为我缺少你的数据模型,所以我不能给你一个代码示例,但一般概念是

  1. 在数组中的每个对象中都有一些可识别的数据。如果它来自数据库,您可能有一个 ID 字段。
  2. 在 selectionchanged 上获取所选项目的 id
  3. 在您的 deleteButtonOnClick 函数中,首先循环遍历您的数据数组,检查 id 是否与所选项目的匹配,当匹配时,您拥有对象的索引
  4. 现在使用该索引,将它从你的数组中拼接出来
于 2013-05-03T19:34:27.900 回答
0

如果列表视图显示分组数据,您将从列表中创建组。splice 需要在组对象上调用。

// this is for example. your data model will be different and hence, 
// the createGrouped call details will be different.
var list = new WinJS.Binding.List(data);
var groups = list.createGrouped(
    function groupKey(item)
    {
        return item.brand;
    },
    function groupData(item)
    {
        var result = { title: item.brand };
        return result;
    }
    );
this.groups = groups;
listView.winControl.itemDataSource = groups.dataSource;
listView.winControl.groupDataSource = groups.groups.dataSource;

现在,您的 appbar cmd 处理程序将删除选定的列表项,如下所示。注意 - 它不应该在 selectionchanged 事件处理程序中完成。

_oncmdclick: function oncmdclick(event)
{
    var indices = listView.winControl.selection.getIndices();
    // assuming that delete should only handle single item selection
    if (indices && indices.length == 1)
    {
        this.groups.splice(indices[0], 1);
    }
},
于 2013-05-04T04:05:33.617 回答