我有一个对象数组(Users)定义和设置如下:
// 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
// ...
Run Code Online (Sandbox Code Playgroud)
索引值在每个用户上设置为检索它们的顺序.检索到用户后,可以逐个选择这些用户,然后将其从列表移动到页面上的表格.在select时,selected对于数组中的选定对象,该属性设置为true.
我正在使用grep返回所有选定的用户.
var SelectedUsers = $.grep(AllUsers,function(obj){
return obj["selected"] == true;
});
Run Code Online (Sandbox Code Playgroud)
以下是返回数据的示例:
[
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
}
]
Run Code Online (Sandbox Code Playgroud)
我希望能够浏览数据,为此,我需要能够通过索引获取下一个和之前选择的用户.我怎样才能做到这一点?
例如,如果我在表中打开第一个选定用户(索引= 56),我如何获得具有下一个索引的用户(索引= 92的第三个选定用户)?
小提琴: http: //jsfiddle.net/iambriansreed/KEXwM/
添加了 JavaScript:
SelectedUsers.sort(function(a,b){
return a.index == b.index ? 0 : (a.index < b.index ? -1 : 1)});
Run Code Online (Sandbox Code Playgroud)
如果您不想修改原始SelectedUsers数组,则将排序定义为新变量:
var SortedSelectedUsers = SelectedUsers.slice(0);
SortedSelectedUsers.sort(function(a,b){
return a.index == b.index ? 0 : (a.index < b.index ? -1 : 1)});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2430 次 |
| 最近记录: |