Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Sort a paginated array

I have a paginated array and I have to sort by columns but when I sort from the current page I get the begenning or the end of the sorted array but if I change the page I get the right sorted values. It seems the current page becomes the 1st page when I trigger the function.

I use the pagination from ng-bootstrap

Function triggered when I click on a column

 sort() {
    if (this.sortAsc == false) {
      this.service.getSortedData().subscribe((data) => {
        this.array = data['data'];
        this.nextPage = data['next_page_url'];
        this.collectionSize = data['total'];
      });
    } else {
      this.service.getSortedDataDesc().subscribe((data) => {
        this.array = data['data'];
        this.nextPage = data['next_page_url'];
        this.collectionSize = data['total'];
      });
    }
  }

HTML

 <th scope="col" (click)="sort()">
              <fa-icon *ngIf="sortAsc == true" [icon]="faChevronUp"></fa-icon>
              <fa-icon *ngIf="sortAsc == false" [icon]="faChevronDown"></fa-icon>
              Values
            </th>

current result with order by desc example:

on click column from page 2 :

1000
150
20

navigate to sorted page 1 :

1000
150
20

expected result :

on click column from page 2 :

10
6
5

navigate to sorted page 1 :

1000
150
20

data content for page 2:

{
current_page: 1
data: (30) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
first_page_url: "http://localhost:8000/api/values?page=1"
from: 1
last_page: 4
last_page_url: "http://localhost:8000/api/values?page=4"
links: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
next_page_url: "http://localhost:8000/api/values?page=2"
path: "http://localhost:8000/api/values"
per_page: 30
prev_page_url: null
to: 30
total: 118
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Although you havent provide the cruicial information, I'm guessing that probably the problem is in the getSortedData() and getSortedDataDesc() functions, what are they returning? You need to offset the data, somehow read current page and subtract those pages from the result passed to

this.array = data['data'];

also, you set the array in the first condition and you're not doing that in the second, maybe that's the problem?

about 4 years ago · Juan Pablo Isaza Relatório

0

this is because you're subscribing the value changes on a click event. try this instead:

  async sort() {
    let data = [];
    if (this.sortAsc == false) {
      data = await this.service.getSortedData().toPromise();
    } else {
      data = await this.service.getSortedDataDesc().toPromise();
    }

    this.array = data['data'];
    this.firstPageUrl = data['first_page_url'];
    this.nextPage = data['next_page_url'];
    this.collectionSize = data['total'];
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

I found a solution, I don't think this is really clean but it works. I store the current page when I display another page then add a condition in the sort() function

    sort(){
      if (this.sortAsc == false) {
         this.service.getSortedData().subscribe((data) => {
            this.nextPage = data['next_page_url'];
            this.collectionSize = data['total'];
            if (this.currentPage == 1) {
                this.array = data['data'];
            } else {
              this.service
                .getPage(this.firstPageUrl, this.currentPage)
                .subscribe((data) => {
                         this.array = data['data'];
                });
            }
          });
        } else {
           this.service.getSortedDataDesc().subscribe((data) => {
            this.nextPage = data['next_page_url'];
            this.collectionSize = data['total'];
            if (this.currentPage == 1) {
               this.array = data['data'];
            } else {
              this.service
                .getPage(this.firstPageUrl, this.currentPage)
                .subscribe((data) => {
                         this.array = data['data'];
                });
            }
          });
        }

}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda