ng2bln-sort-by-attribute-pipe

1.1.9 • Public • Published

ng2bln-sort-by-attribute-pipe

N|Solid

ng2bln-sort-by-attribute-pipe is a simple to use ng2 pipe.
It sorts an array of objects by a provided property value

Tech

ng2bln-sort-by-attribute-pipe uses a number of open source projects to work properly:

  • [Angular.io] - HTML enhanced for web apps!

Installation

ng2bln-sort-by-attribute-pipe requires Angular.io v2.x to run.

How to use the pipe

    ...
    import { SortByAttributePipe } from 'ng2bln-sort-by-attribute-pipe'
    ...
    
    @NgModule ( 
      {
        imports: [
        ...
        ],
        declarations: [
          ...
          SortByAttributePipe,
          ...
        ],
      } 
    )
    export class SharedModule {
    }

The pipe takes an parameter object with this (ts) interface:

interface Sorting {
    sortByCol: string,
    sortDir: string,
    sortType: string
}

Allowed instances of Sorting.sortDir are "asc", and "desc".
Allowed instances of Sorting.sortTye are "Number", "String", and "Date".

For more details see the test spec shipped with this package.

Examples

    ...
    let items: Array = [
        {
            label: 'Spoiler',
            category: 'cars',
            priceCategory: 2
        },
        {
            label: 'Bumber',
            category: 'cars',
            priceCategory: 15
        },
        {
            label: 'Steeling wheel',
            category: 'cars',
            priceCategory: 4
        },
        {
            label: 'Sail',
            category: 'wessels',
            priceCategory: 81
        },
    ]
    ...
    let sorting: Sorting = {
        sortByCol: 'label', // object attribute's name to sort by
        sortDir: 'asc',     // 'asc' or 'desc'
        sortType: 'String'  // 'String', 'Number', or 'Date'
    }
    ...
    <ul>
        <li *ngFor="let item of items | sortByAttr : sorting"></li>
    </ul>

License

MIT

Free Software, Hell Yeah!

https://www.npmjs.com/~gdrahota

Readme

Keywords

Package Sidebar

Install

npm i ng2bln-sort-by-attribute-pipe

Weekly Downloads

0

Version

1.1.9

License

MIT

Last publish

Collaborators

  • gdrahota