How to split list across several pages (SPFX/React)

%3CLINGO-SUB%20id%3D%22lingo-sub-1388787%22%20slang%3D%22en-US%22%3EHow%20to%20split%20list%20across%20several%20pages%20(SPFX%2FReact)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1388787%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20creating%20a%20webpart%20that%20surfaces%20a%20SharePoint%20list.%20I'm%20trying%20to%20split%20the%20list%20up%20into%20pages%2C%20but%20getting%20stuck%20on%20basic%20JavaScript%20here%2C%20mixed%20with%20some%20React.%3C%2FP%3E%3CP%3EHere's%20the%20function%20that%20gets%20the%20list%2C%20at%20the%20top%20is%20where%20I%20am%20trying%20to%20work%20out%20how%20many%20pages%20are%20needed%20if%20I%20wanted%20to%20display%202%20items%20per%20page%20and%20my%20attempt%20to%20slice%20the%20mapping%20of%20the%20new%20array.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Epublic%20_getListItems()%20%7B%0A%20%20%20%20%0A%20%20%20%20sp.web.lists.getByTitle(%22Reports%22).items.get().then((items%3A%20any%5B%5D)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20let%20returnedItemsFullA%3A%20IListAItem%5B%5D%20%3D%20items.map((item)%20%3D%26gt%3B%20%7B%20return%20new%20ListAItem(item)%3B%20%7D)%3B%0A%20%20%20%20%20%20console.log(returnedItemsFullA.length)%3B%0A%20%20%20%20%20%20let%20returnedItemsFullALength%20%3D%20returnedItemsFullA.length%3B%0A%0A%20%20%20%20%20%20let%20howManypagesA%20%3D%20Math.ceil(returnedItemsFullALength%2F2)%3B%20%2F%2Fthis%20works%20out%20how%20many%20pages%20will%20be%20needed%20to%20just%20show%202%20items%20per%20page%0A%20%20%20%20%20%20this.setState(%7BListAPages%3A%20howManypagesA%7D)%3B%0A%20%20%20%20%20%20console.log(this.state.ListAPages%2B'state.ListAPages')%3B%20%20%20%20%20%0A%20%20%20%20%20%0A%20%20%20%20%20%20let%20pageNumber%20%3D%20this.state.ListAPage%3B%0A%0A%20%20%20%20%20%20let%20returnedItemsSlice%3A%20IListAItem%5B%5D%20%3D%20items.slice(pageNumber%2C%20pageNumber*2).map((item)%20%3D%26gt%3B%20%7B%20return%20new%20ListAItem(item)%3B%20%7D)%3B%0A%0A%20%20%20%20%20%20let%20returnedItems%3A%20IListAItem%5B%5D%20%3D%20items.map((item)%20%3D%26gt%3B%20%7B%20return%20new%20ListAItem(item)%3B%20%7D)%3B%0A%20%20%20%20%20%20console.log(returnedItemsSlice)%3B%20%20%0A%20%20%20%20%20%20this.setState(%7B%0A%20%20%20%20%20%20%20%20Items%3A%20returnedItems%2C%0A%20%20%20%20%20%20%20%20ListAItems%3A%20returnedItemsSlice%2C%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20only%20getting%201%20item%20on%20the%20first%20page%20and%20nothing%20on%20the%20second!%3C%2FP%3E%3CP%3EI%20have%20a%20function%20which%20updates%20a%20page%20number%20in%20state%2C%20which%20the%20above%20function%20uses%20to%20know%20what%20page%20number%20has%20been%20selected%20by%20the%20user.%3C%2FP%3E%3CP%3EWould%20I%20need%20to%20trigger%20the%20function%20above%2C%20every%20time%20the%20next%20or%20prev%20page%20is%20entered%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I'm creating a webpart that surfaces a SharePoint list. I'm trying to split the list up into pages, but getting stuck on basic JavaScript here, mixed with some React.

Here's the function that gets the list, at the top is where I am trying to work out how many pages are needed if I wanted to display 2 items per page and my attempt to slice the mapping of the new array.

 

public _getListItems() {
    
    sp.web.lists.getByTitle("Reports").items.get().then((items: any[]) => {
      let returnedItemsFullA: IListAItem[] = items.map((item) => { return new ListAItem(item); });
      console.log(returnedItemsFullA.length);
      let returnedItemsFullALength = returnedItemsFullA.length;

      let howManypagesA = Math.ceil(returnedItemsFullALength/2); //this works out how many pages will be needed to just show 2 items per page
      this.setState({ListAPages: howManypagesA});
      console.log(this.state.ListAPages+'state.ListAPages');     
     
      let pageNumber = this.state.ListAPage;

      let returnedItemsSlice: IListAItem[] = items.slice(pageNumber, pageNumber*2).map((item) => { return new ListAItem(item); });

      let returnedItems: IListAItem[] = items.map((item) => { return new ListAItem(item); });
      console.log(returnedItemsSlice);  
      this.setState({
        Items: returnedItems,
        ListAItems: returnedItemsSlice, 
        
      });

    });

 

I'm only getting 1 item on the first page and nothing on the second!

I have a function which updates a page number in state, which the above function uses to know what page number has been selected by the user.

Would I need to trigger the function above, every time the next or prev page is entered?

 

0 Replies