Skip to content

Instantly share code, notes, and snippets.

@azaharafernandezguizan
Created December 21, 2018 11:11
Show Gist options
  • Save azaharafernandezguizan/3b184909396aeed097e4a28123dc3549 to your computer and use it in GitHub Desktop.
Save azaharafernandezguizan/3b184909396aeed097e4a28123dc3549 to your computer and use it in GitHub Desktop.
Example of grid pagination with Bootstrap (TypeScript file)
import { StudentService } from 'src/app/services/student.service';
import {NgbPaginationConfig} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-students',
templateUrl: './students.component.html',
styleUrls: ['./students.component.css'],
providers: [NgbPaginationConfig]
})
export class StudentsListComponent implements OnInit {
studentsList: Student[];
totalItems: number;
page: number;
previousPage: number;
showPagination: boolean;
constructor(
private route: ActivatedRoute,
private studentService: StudentService,
private config: NgbPaginationConfig,
private router: Router) {
this.config.boundaryLinks = true;
}
ngOnInit() {
this.page =1;
this.previousPage =1;
this.fillStudents();
}
fillStudents(page : number) : void {
this.studentService.getStudents(page).subscribe(
data => {
if ((!data && !data.result) || (data && data.result && data.result.length ==0)) {
this.studentsList = [];
this.showPagination = false;
}
else {
this.studentsList = data.result;
this.totalItems = data.totalAmount;
this.showPagination = true;
}
},
error => {
// Aquí se debería tratar el error, bien mostrando un mensaje al usuario o de la forma que se desee.
}
);
}
loadPage(page: number) {
if (page !== this.previousPage) {
this.previousPage = page;
this.fillStudents(this.page-1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment