如何删除WinJS组绑定列表的最后一项

How to delete the last item of WinJS Group Binding List

本文关键字:最后 一项 列表 何删除 删除 WinJS 绑定      更新时间:2024-01-24

我正在用WinJS编写一个Windows 8应用程序。我正在使用分组绑定列表

var groupedSortedListProjection = list.createGrouped(groupKey, groupData, groupSorter);

这将有两组,键分别为"doing"answers"done"。因此,当用户单击组"doing"中的项目时,一个项目将添加到"done"中。

我试图将组"完成"中的项目数限制为仅5个项目。因此,每次向组"done"添加新项目时,最后一个(最旧的)项目都会被删除。我需要一个索引。然而,我不知道如何获取最后一项的索引。

更重要的是,我在MSDN中找到了这个方法:

List.lastIndexOf

例如:

var number = list.lastIndexOf(searchElement, fromIndex);

我认为这是答案,但我不知道如何使用这种方法。

这是我为ListView:编写的HTML代码

<div class="appViewContentMask">
        <div class="appViewContent">
            <div id="gridViewToDo" data-win-control="WinJS.UI.ListView" data-win-options="{
 itemTemplate:select('#toDoTemplate'),
 groupHeaderTemplate: select('#headerTemplate'),
 layout:{type:WinJS.UI.GridLayout},
 itemsDraggable : true,
 itemsReorderable : true}"></div>
        </div>
</div>

我用Javascript连接数据:

感谢您的阅读。我希望我的问题足够清楚。如果你需要更多的信息,告诉我,我会发布更多。

删除最旧完成任务的一种方法是将finishTime添加到项目中,并将其作为排序键的一部分。这样,最古老的项目可以在列表的末尾找到。基于该键的排序需要在createSorted投影中完成。

此后,需要使用createGrouped投影对项目进行分组并对组进行排序。

要在iteminvoked处理程序中修改和删除该项。

有关完整的详细信息和代码列表,请参阅此处。

您可以使用拼接方法(http://msdn.microsoft.com/en-us/library/windows/apps/hh700810.aspx)在List上,它的操作方式类似于数组。一旦你找到你想删除的项目的索引,你只需说:

groupedSortedListProjection.splice(index, 1);

为了找到索引,你可以询问小组,或者在你的情况下,因为你知道"完成"小组是第二个,是集合中的最后一个小组,那么该小组中的最后个元素也将方便地成为集合中的最终项目,你的代码是:

var index = groupedSortedListProjection.length - 1;
groupedSortedListProjection.splice(index, 1);

然而,如果你没有这个不变量,而是必须找到正确的组,那么代码会看起来像:

var groups = groupedSortedListProjection.groups;
var group = {}; // start with an empty object to make the for loop condition simpler
for (var i = 0; i < groups.length && group.key !== "done"; i++) {
  group = groups.getItemAt(i);
}
var index = group.firstItemIndexHint + group.groupSize - 1;
groupedSortedListProjection.splice(index, 1);

希望能有所帮助。

-josh