# ngxs-mat-sort-plugin

> When using [Angular Material sort](https://material.angular.io/components/sort/overview), you need to bind the selected order from the store to the table so you can use it in a [NGXS Selector](https://www.ngxs.io/concepts/select). To make the sorting stat

Latest version **0.0.7** (published 2021-04-04) · 0 weekly downloads

## Install

```sh
npm install ngxs-mat-sort-plugin
pnpm add ngxs-mat-sort-plugin
yarn add ngxs-mat-sort-plugin
bun add ngxs-mat-sort-plugin
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2021-04-04 |
| First published | 2021-04-03 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 49.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Philipp Escher |
| Maintainers | philippesc |
| Keywords | ngxs, redux, state, rxjs, angular, cqrs, store, state-management, mat-sort, angular-material |

## Links

- npm: https://www.npmjs.com/package/ngxs-mat-sort-plugin
- Repository: https://github.com/philippesc/ngxs-mat-sort-plugin
- Homepage: https://github.com/philippesc/ngxs-mat-sort-plugin#readme
- Issues: https://github.com/philippesc/ngxs-mat-sort-plugin/issues
- npm.io page: https://npm.io/package/ngxs-mat-sort-plugin

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.0.0

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.0.7 (latest) — 2021-04-04
- 0.0.6 — 2021-04-04
- 0.0.5 — 2021-04-04
- 0.0.4 — 2021-04-04
- 0.0.3 — 2021-04-03
- 0.0.2 — 2021-04-03
- 0.0.1 — 2021-04-03

## README

# NgxsMatSortPlugin

When using [Angular Material sort](https://material.angular.io/components/sort/overview), you need to bind the selected order from the store to the table so you can use it in a [NGXS Selector](https://www.ngxs.io/concepts/select). To make the sorting state persistent beyond a component's lifetime (when the component is destroyed the users selection of sort is also destroyed), you can synchronize the selection to the state with this plugin. This plugin works analogously to [@ngxs/form-plugin](https://www.ngxs.io/plugins/form).

## Installation

```
npm install ngxs-mat-sort-plugin --save

# or if you are using yarn
yarn add ngxs-mat-sort-plugin
```

## Usage

In the root module of your application, import `NgxsMatSortPluginModule` and include it in the imports.

```
import { NgxsMatSortPluginModule } from 'ngxs-mat-sort-plugin';
import { NovelsState } from './novels.state';

@NgModule({
  imports: [
    NgxsModule.forRoot([NovelsState]),
    NgxsMatSortPluginModule.forRoot()
  ]
})
export class AppModule {}
```

If you are using sort in a submodule, it must be imported there as well:

```
import { NgxsMatSortPluginModule } from 'ngxs-mat-sort-plugin';

@NgModule({
  imports: [NgxsMatSortPluginModule]
})
export class SomeModule {}
```

### Sort State

Define your default sort state as part of your application state.

```
import { Injectable } from '@angular/core';
import { State } from '@ngxs/store';

@State<NovelsStateModel>({
  name: "novels",
  defaults: {
    sort: {
        active: "title",
        direction: "asc"
    },
    data: []
  }
})
@Injectable()
export class NovelsState {}
```

### Sort Setup

In your component, you would implement the table and decorate the table with the `ngxsMatSort` directive with the path of your state object where the sort should be located. We are passing the string path to `ngxsMatSort`. The directive uses this path to connect itself to the store and setup bindings.

```
<table matSort mat-table [dataSource]="novels" ngxsMatSort="novels.sort">
...
</table>
```

Now anytime the user changes the sort of this table, your state will also reflect the new sort. When the component is destroyed and initialized again, the sort is binded and instantly restored 🎉.

For making the sort apply you can just use the sort object in your selectors:

```
@Selector()
static novelsSorted(state: NovelsStateModel) {
    if (state.sort.active && state.sort.direction !== "") {
        const novels = Object.assign([], state.data);
        return novels.sort((a, b) => {
            const aValue = (a as any)[state.sort.active];
            const bValue = (b as any)[state.sort.active];
            return (aValue < bValue ? -1 : 1) * (state.sort.direction === "asc" ? 1 : -1);
        });
    } else {
        return state.data;
    }
}
```

---
_Source: https://npm.io/package/ngxs-mat-sort-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
