Skip to content

Instantly share code, notes, and snippets.

@gorkemozkan
Last active September 16, 2023 21:53

Revisions

  1. gorkemozkan revised this gist Sep 16, 2023. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pagination.js
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,6 @@ class PaginationHelper {
    if (itemIndex < 0 || itemIndex >= this.itemCount()) {
    return -1;
    }

    return Math.floor(itemIndex / this.itemsPerPage);
    }
    createPagination() {
  2. gorkemozkan revised this gist Sep 16, 2023. 1 changed file with 5 additions and 17 deletions.
    22 changes: 5 additions & 17 deletions pagination.js
    Original file line number Diff line number Diff line change
    @@ -7,40 +7,28 @@ class PaginationHelper {
    return this.collection.length;
    }
    pageCount() {
    let pageCount = 0;
    const collection = this.#getCollection();

    while (collection.length !== 0) {
    collection.splice(0, this.itemsPerPage);
    pageCount++;
    }

    return pageCount;
    return Math.ceil(this.itemCount() / this.itemsPerPage);
    }
    pageItemCount(pageIndex) {
    const pages = this.#createPagination();

    const pages = this.createPagination();
    return pages[pageIndex]?.length ?? -1;
    }
    pageIndex(itemIndex) {
    if (itemIndex < 0 || itemIndex >= this.itemCount()) {
    return -1;
    return -1;
    }

    return Math.floor(itemIndex / this.itemsPerPage);
    }
    #createPagination() {
    createPagination() {
    const pages = [];
    const pageCount = this.pageCount();
    const collection = this.#getCollection();
    const collection = [...this.collection];

    for (let i = 0; i < pageCount; i++) {
    pages.push(collection.splice(0, this.itemsPerPage));
    }

    return pages;
    }
    #getCollection() {
    return [...this.collection];
    }
    }
  3. gorkemozkan created this gist Sep 16, 2023.
    46 changes: 46 additions & 0 deletions pagination.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    class PaginationHelper {
    constructor(collection, itemsPerPage) {
    this.collection = collection;
    this.itemsPerPage = itemsPerPage;
    }
    itemCount() {
    return this.collection.length;
    }
    pageCount() {
    let pageCount = 0;
    const collection = this.#getCollection();

    while (collection.length !== 0) {
    collection.splice(0, this.itemsPerPage);
    pageCount++;
    }

    return pageCount;
    }
    pageItemCount(pageIndex) {
    const pages = this.#createPagination();

    return pages[pageIndex]?.length ?? -1;
    }
    pageIndex(itemIndex) {
    if (itemIndex < 0 || itemIndex >= this.itemCount()) {
    return -1;
    }

    return Math.floor(itemIndex / this.itemsPerPage);
    }
    #createPagination() {
    const pages = [];
    const pageCount = this.pageCount();
    const collection = this.#getCollection();

    for (let i = 0; i < pageCount; i++) {
    pages.push(collection.splice(0, this.itemsPerPage));
    }

    return pages;
    }
    #getCollection() {
    return [...this.collection];
    }
    }