我在js中有一个对象数组:
[{name:'John',number:1},{name:'Peter',number:2},{name:'Mario',number:3},{name:'Jack',number:4}]
尝试创建分页组件并想知道如何选择下一对对象。我试过切片,但无法让它工作。例如:
SelectPage(1) would return nr 3 and 4, SelectPage(0) nr 1 and 2 etc
这是否有lodash功能?
答案 0 :(得分:2)
有什么事情吗?
var list = [{name:'John',number:1},{name:'Peter',number:2},{name:'Mario',number:3},{name:'Jack',number:4},{name:'Mario',number:5},{name:'Jack',number:6}]
function SelectPage(list, index, pageSize) {
return list.slice(index*pageSize, index*pageSize+pageSize)
}
更新了改进,感谢@nnnnnn
答案 1 :(得分:0)
以下是让您运行的示例代码,您可以根据自己的要求进行修改。
public class PagerAdapter extends FragmentStatePagerAdapter {
private int numberTabs;
public PagerAdapter(FragmentManager fm, int numberTabs) {
super(fm);
this.numberTabs=numberTabs;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentNovedades();
case 1:
return new FragmentCuenta();
case 2:
return new FragmentContacto();
default:
return null;
}
}
@Override
public int getCount() {
return numberTabs;
}
}