背景
我定义并设置了一组对象(用户),如下所示:
// object definition
function Users()
{
this.Id = null;
this.Name = null;
this.FirstName = null;
this.LastName = null;
this.IsActive = null;
this.Title = null;
this.index = null;
this.selected = null;
}
// object array
var AllUsers = [];
// ...
// an ajax call retrieves the Users and adds them to the AllUsers array
// ...
每个用户的索引值设置为检索它们的顺序。一旦检索到用户,就可以一个一个地选择它们,并将它们从列表移动到页面上的表中。选择后,该selected
属性设置为 true,用于数组中的选定对象。
我正在使用 grep 返回所有选定的用户。
var SelectedUsers = $.grep(AllUsers,function(obj){
return obj["selected"] == true;
});
以下是返回数据的示例:
[
Object {
Id="00540000001AbCdEFG",
Name="First Last1",
FirstName="First",
LastName="Last1",
Title="Title",
index=56,
selected=true
},
Object {
Id="00540000001AbChIJK",
Name="First Last2",
FirstName="First",
LastName="Last2",
Title="Title",
index=12,
selected=true
},
Object {
Id="00540000001AbClMNO",
Name="First Last3",
FirstName="First",
LastName="Last3",
Title="Title",
index=92,
selected=true
}
]
问题
我希望能够对数据进行分页,为此,我需要能够按索引获取下一个和上一个选定的用户。我怎样才能做到这一点?
例如,如果我在表中打开第一个选定用户(索引 = 56),我怎样才能获得具有下一个索引的用户(索引 = 92 的第三个选定用户)?