2
This is the web api:
[Route("{pagina:int}/{tamanhoPagina:int}")]
[HttpGet]
public IHttpActionResult UsuarioPaginado(int pagina = 1, int tamanhoPagina=10)
{
var _aluno = iAluno.Listar()
.Where(x => x.Nome != string.Empty)
.OrderBy(y => y.Nome).Skip(tamanhoPagina * (pagina - 1)).Take(tamanhoPagina);
return Ok(_aluno);
}
How to make my Angular application know when it’s page 2 or page 3 or 4 and so on?
Below is the form and the user needs to navigate to the Next or Previous:
The class below is the Service where I request the Web Api:
@Injectable({
providedIn: 'root'
})
export class UsuarioService {
constructor(private http: HttpClient) { }
getListaUsuarios(): Observable<Usuario[]> {
const url = `${environment.apiUrl}`;
return this.http.get<Usuario[]>(url);
}
getListaAlunoPaginado(pagina: number, tamanhoPagina: number): Observable<Usuario[]> {
const url = `${environment.apiUrl}/${pagina}/${tamanhoPagina}`;
return this.http.get<Usuario[]>(url)
}
}
And this is the class of the Component:
Obs.: in the code snippet where this.pagina = 1 the value is fixed and cannot, it has to be a dynamic value as the user clicks on next and there is my question: How to implement this dynamic value ?
export class ListarUsuarioComponent implements OnInit {
public usuarios: Usuario[];
public pagina: number;
public tamanhoPagina: number;
constructor(private usuarioService: UsuarioService) { }
ngOnInit() {
this.getListaAlunoPaginado(1,10);
}
getListaAlunoPaginado(pagina: number, tamanhoPagina: number) {
this.usuarioService.getListaAlunoPaginado(pagina, tamanhoPagina)
.subscribe((dados: Usuario[]) => {
this.usuarios = dados,
this.pagina = 1, <<--Aqui o valor está fixo, mas tem que ser um valor dinâmico conforme o usuário vai Clicando em Próximo.
this.tamanhoPagina = 10;
}, () => { 'Erro'; });
}
}
The problem with this technique is that it wouldn’t follow the route, it could be a problem. or else it would have to change the route!
– novic
Thank you @Paulo Felipe Martins for your solution, but my question is not how send data and how to know when is page 2 or page 3, because I need to make a 10 in 10 records pagination, however I am seeing that my doubt was not so clear so I will edit my post and add more information to be clearer.
– hard123