2

I'm trying to implement custom pagination component. This is template.

<div *ngIf="totalItems != 0">
    <ul class="pagination">
        <li *ngFor="let page of pages" [ngClass]="{'active': currentPage == page.title}">
            <a (click)="changePage(page)">{{page.title}}</a>
        </li>
    </ul>
    <select>
        <option *ngFor="let size of pageSizes">{{size}}</option>
    </select>
</div>

Component code:

@Component({
    selector: 'pager',
    templateUrl: 'templates/pager.component.html',
    styleUrls: ['styles/pager.component.css']
})
export class PagerComponent {
    @Input() totalItems: number = 0;
    @Input() lastText: string = "»";
    @Input() firstText: string = "«";
    @Input() nextText: string = "›";
    @Input() prevText: string = "‹";
    public currentPage: number = 1;
    pageSizes: Array<number> = [10, 15, 30];
    public currentSize: number = this.pageSizes[0];

    @Output() pageChanged = new EventEmitter();

    get pages(): Array<IPage> {
        var list = new Array<IPage>();
        let pageCount = Math.ceil(this.totalItems / this.currentSize);
        let start = Math.max(this.currentPage - 1, 1);
        let end = Math.min(this.currentPage + 2, pageCount);
        list.push({ title: this.firstText, number: 1 });
        list.push({ title: this.prevText, number: this.currentPage - 1});
        for (let i = start; i <= end; i++) {
            list.push({ title: String(i), number: i });
        }
        list.push({ title: this.nextText, number: this.currentPage + 1});
        list.push({ title: this.lastText, number: end});
        return list;
    }

    public changePage(page: IPage) {
        this.currentPage = page.number;
        this.pageChanged.emit(null);
    };

    public resetCurrentPage(): void {
        this.currentPage = 1;
    }
}

I was using simple array of numbers. Then I wanted to add Last/Firts buttons. I wrote interface that contains two properties title and page number. Now click event doesn't work. What's wrong with my code?

3
  • do you get any error? Commented Sep 27, 2016 at 9:22
  • What does "doesn't work" mean exactly? Commented Sep 27, 2016 at 9:22
  • @galvan there's no any error. @Günter Zöchbauer it ins't firing. I've tried to create custom <div> and bind the same function on click It works good with div Commented Sep 27, 2016 at 9:24

2 Answers 2

2

I think the problem is function changePage() has an argument of type IPage and you pass a number

 public changePage(page: IPage) {
    this.currentPage = page.number;
    this.pageChanged.emit(null);
};

change it to

 public changePage(num: number) {
    this.currentPage = num;
    this.pageChanged.emit(null);
};
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry, I forgot to change the template. And it doesn't work this way as well.
1

I've fixed it! It's similar to this question. It generated new array each time. So angular can't bind an event to array item. @Günter Zöchbauer, it should be familiar to you.

2 Comments

I'm having the same problem. Would be great if you could tell how did you solved it?
I can't remember details of implementation, just try to save an array in your class field and return it in a property. Please, see the link in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.