-3

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[l]);
        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 ? 
}

------------------------------------------------------------------------- EDIT

With Aaron answer, here's what I did :

function complete(e:Event):void {
    addChild(list);
    products = JSON.parse(loader5.data) as Array;
    feedbackText.text = "complete";
    showNext() ;
}
function showNext():void {
    var currentNumShowing:int = list.numChildren;
    var nextNumShowing:int = Math.min(currentNumShowing + 10, products.length);
    for(var i:int = currentNumShowing; i < nextNumShowing; i++){
        createListItem(i, products[i]);
    }
    next.visible = nextNumShowing < products.length;
}

So now how can I add next.addEventListener(MouseEvent.CLICK,next10); ?

(as I can't do next.addEventListener(MouseEvent.CLICK,showNext); because showNext is not a MouseEvent...)

Secondly, my code was displaying the products from the end to the beginning (for(var i:int = 0, l:int = products.length - 1; l >= 0; i++, l--){. How can I keep that ?

I've tried : for(var i:int = currentNumShowing, l:int = products.length - 1; i < nextNumShowing;l >= 0; i++, l--){ but it's not working (error 1084 expecting rightparen before semicolon and expecting semicolon before rightparen)

cocoGeek
  • 9
  • 4
  • What's with the downvote ?? It's a legitimate question (and this question has been asked for php here : [link](http://stackoverflow.com/questions/1571427/returning-first-x-items-from-array)] or here [link] (http://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array) But never asked for AS3. – cocoGeek Jan 21 '16 at 04:53
  • Unexplained down-votes are annoying and unhelpful. I think your question is not very clear. Do you want to load all the products and only *show* 10 at a time? Or do you to load 10 at a time from your server? – Aaron Beall Jan 21 '16 at 14:32
  • Thx for the explanation. I meant "load all the products and only show 10 at a time" (if it's possible). – cocoGeek Jan 21 '16 at 21:14
  • I posted an answer, but actually there's another question: do you want to show 10 more items and keep the existing items, for example 10, then 20, then 30, etc, or do you want to show only 10 items at a time (pages), like items 1-9, then items 10-19, then items 20-29, etc? – Aaron Beall Jan 21 '16 at 21:58
  • Thx for the answer. The 2nd one ( only 10 items at a time (pages), like items 1-9, then items 10-19...etc) – cocoGeek Jan 21 '16 at 22:57
  • Ok, based on that I'll update my answer. – Aaron Beall Jan 22 '16 at 01:29

1 Answers1

0

If all you want to do is show 10 at a time from a products array, you can create a function to show a slice of the array like this:

const itemsPerPage:uint = 10;

var currentPageIndex:int = 0;

function displayPage(pageIndex:int):void {
    list.removeChildren();

    currentPageIndex = pageIndex;

    var firstItemIndex:int = pageIndex * itemsPerPage;
    var lastItemIndex:int = Math.min(firstItemIndex + itemsPerPage, products.length - 1);
    for(var i:int = firstItemIndex; i <= lastItemIndex; i++){
        createListItem(i - firstItemIndex, products[i]);
    }
    next.visible = lastItemIndex < products.length - 1;
}

Then from your complete handler you can show the first page like this:

displayPage(0);

Your next button can have a click handler that displays currentPageIndex + 1, like this:

next.addEventListener(MouseEvent.CLICK, nextClick);
function nextClick(e:MouseEvent):void {
    displayPage(currentPageIndex + 1);
}

Similarly, you could displayPage(currentPageIndex - 1) to go back a page.

If you want to display from the end to the beginning of the array, I recommend that you just reverse it: products.reverse()

Note that if your data is huge, just loading the JSON could take awhile. In this case you should paginate your JSON data itself to only load 10 at a time

Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
  • Thx; So I've added `showNext()` in my JSON `complete` handler. But I can't call the same function on my next button as it requiers 1 arguments `event:MouseEvent` (as it's a button..). So how can I do to call this function each time I click on "next" ? – cocoGeek Jan 21 '16 at 23:23
  • Perfect : Thx. But, just one thing..How can I do to display the products in a different order ? (the end to the beginning) Thx – cocoGeek Jan 22 '16 at 04:34
  • Hmm just a problem with this code. It seems that `displayPage` is removing an item. If I have only 1 item, it doesn't appears... Any idea why ? (the problem come from `displayPage`, as if delete `displayPage(0);` from `complete`, the unique item is displayed. If I put it back, the unique item is not displayed. – cocoGeek Jan 22 '16 at 05:44
  • Fixed. For loop condition should be `i <= lastItemIndex` to include the last index. – Aaron Beall Jan 22 '16 at 13:46