Code Monkey home page Code Monkey logo

ember-simple-pagination's Introduction

Ember-simple-pagination

Build Status Code Climate GitHub license

This Ember addon is a simple pagination component which uses Twitter Bootstrap markup. It works with both server-side and client-side pagination.

screenshot

Installation

  • Ember.js v3.4 or above
  • Ember CLI v2.13 or above
  • Node.js v8 or above

ember install ember-simple-pagination

Usage

In your templates:

{{simple-pagination
  recordCount=recordCount
  pageSize=pageSize
  pageNumber=pageNumber
  maxPagesInList=maxPagesInList
  dataTestSelector='my-test-selector'
  onPageSelect=(action "getPage")}}

Properties

  • recordCount: The total number records in the collection being paginated.
  • pageSize: The number of records in each page.
  • pageNumber: The current page number. Note that page numbers begin at 1, not 0.
  • maxPagesInList: The maximum of page numbers to display. Defaults to 10.
  • dataTestSelector: adds a data-test-selector attribute to the outer div. Defaults to null.

Events

onPageSelect: This fires when the user clicks a page number link, or the next page or previous page links. It will invoke the external action specified. The page number selected by the user will be passed to the action. The action is not invoked if the user selects the current page link, or the previous page link if the current page === 1, or the next page link if the current page === the total number of pages.

Example

This example assumes the Ember JSONAPIAdapter and on the server JSONAPI::Resources.

app/components/display-posts.js:

import Component from '@ember/component';
import { computed } from '@ember/object';
import { sort } from '@ember/object/computed';
import { inject as service } from '@ember/service';

export default Component.extend({
  store: service(),
  posts: computed(function() {
    return this.get('store').peekAll('post');
  }),

  pageSize: 20,
  pageNumber: null,
  recordCount: null,

  sortProps: ['createdAt:desc'],
  sortedPosts: sort('posts', 'sortProps'),

  loadPosts(getPageNumber) {
    const pageSize = this.get('pageSize');

    this.get('store').unloadAll('post');
    this.get('store').
      query('post', {page: {number: getPageNumber, size: pageSize}}).
      then((result) => {
        this.setProperties({
        	'recordCount': result.get('meta.record-count'),
        	'pageNumber': getPageNumber
        });
      };
  },

  init() {
    this._super(...arguments);
    this.loadPosts(1);
  },

  actions: {
    getPage(getPageNumber) {
      this.loadPosts(getPageNumber);
    }
  }
});

app/templates/components/display-posts.hbs:

<ul class="list-unstyled">
  {{#each sortedPosts as |post|}}
    <li>{{display-post post=post}}</li>
  {{/each}}
</ul>

{{simple-pagination
  recordCount=recordCount
  pageSize=pageSize
  pageNumber=pageNumber
  onPageSelect=(action "getPage")}}

ember-simple-pagination's People

Contributors

twbrandt avatar ember-tomster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.