Giter Site home page Giter Site logo

vishalbiradar / angular-sorting Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 118 KB

Sorting list of objects using ng2-order-pipe

Home Page: https://stackblitz.com/edit/angular-sorting

License: MIT License

TypeScript 67.76% JavaScript 15.04% CSS 2.35% HTML 14.84%

angular-sorting's Introduction

Angular Sorting

Steps to achieve sorting in angular project are as below:

  1. Install the required packages

    npm install ng2-order-pipe --save
  2. Import Ng2OrderModule

    app.module.ts

    import { Ng2OrderModule } from 'ng2-order-pipe';
    @NgModule({
      ...
      ...
      imports: [
        Ng2OrderModule
      ]
    })
    export class AppModule { }
  3. Create a mock list which one we want to sort

    app.component.ts

    heroes = [
        { id: 11, name: 'Mr. Nice', country: 'India' },
        ...
        ...
      ];
  4. Show/iterate this list in our html page

    <div class="container">
      <div class="row">
        <table class="table table-striped">
          <thead>
          <tr>
            <th>Id</th>
            <th>Hero Name</th>
            <th>Country</th>
          </tr>
          </thead>
          <tbody>
          <tr *ngFor="let hero of heroes>
            <td>{{hero.id}}</td>
            <td>{{hero.name}}</td>
            <td>{{hero.country}}</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
    
  5. Create key and reverse varibales key = default field based on which we want to sort our list reverse = sorting in ascending or descending order

    app.component.ts

    key = 'name';  // means i want to apply sorting on name as default
    reverse = false;
  6. Write the sorting logic in a function will send the key from UI to this function, based on which we want to sort our list

    sortList(key) {
        this.key = key;
        this.reverse = !this.reverse;
      }
  7. Lets create onclick functions which will send the key to our sorting function

    <span (click)="sortList('name')" class="by-name">Name</span>
    <span (click)="sortList('country')" class="by-country">Country</span>
  8. Actual plugin sorting logic to be applied on the list iteration

    *ngFor="let hero of heroes | orderBy: key : reverse"

Demo

https://stackblitz.com/edit/angular-sorting

angular-sorting's People

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.