Skip to main content
Inspiring
January 21, 2016
Question

How can I display only a certain numbers of items contain in an Array?

  • January 21, 2016
  • 1 reply
  • 284 views

I've got this code in AS3

var products:Array;

var list:Sprite = new Sprite();

function complete(e:Event):void {

addChild(list); products = JSON.parse(loader5.data) as Array;

for(var i:int = 0, l:int = products.length - 1; l >= 0; i++, l--){

createListItem(i, products);

if(products.length >=10){

next.visible=true; }

}

showList();

}

It displays a list of all the products contains in my table database.

Now I added if(products.length >=10){ and it's working.

I would like to add :

if(products.length >=10){

What shoud I put here for showing only 10 products.

next.visible=true;

next.addEventListener(MouseEvent.CLICK, show10Next);

} 


function show10Next(event:MouseEvent){

What should I put here for showing the 10 next products ?

}

I've tried  createListItem(i, products[10]); but it's showing me the product number 10 x times. How can I tell the code to show me the 10 first products only ?

Thx

This topic has been closed for replies.

1 reply

lagoon_bbo
Known Participant
January 21, 2016

Hi, you can do something like this

// keep a page variable

var page:int = 1;

function show10Next(event:MouseEvent):void

{

  // you must add a check to not go after the last page

  var start:int = ++page * 10;

  var stop:int = start + 10;

  var l:int;

  for( var i:int = start; i<stop; ++i )

  {

      l = products.length - i;

      createListItem( i, products );

  }

}