angular-sortablejs
This package is an Angular 2 binding for Sortable.js. Supports standard arrays and Angular FormArray
.
Installation
npm install --save angular-sortablejs
Webpack configuration
There is nothing to configure additionally. Enjoy!
SystemJS configuration
Adapt your systemjs.config.js
(or another place where you configure SystemJS) file with the following:
...
var map = {
...
'angular-sortablejs': 'node_modules/angular-sortablejs',
'sortablejs': 'node_modules/sortablejs/Sortable.js',
...
};
...
var packages = {
...
'angular-sortablejs': { main: 'index.js', defaultExtension: 'js' },
...
};
...
var config = {
map: map,
packages: packages
};
System.config(config);
This is important to let SystemJS know everything it needs about the dependencies it needs to load.
Usage
First, import SortablejsModule
into the angular module where you want to use it:
imports: [
...
SortablejsModule,
...
]
Then use sortablejs
property on a container HTML element to tell Angular that this is a sortable container; also pass the items
array to both *ngFor
and [sortablejs]
to register the changes automatically.
Simple sortable list
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h2>Drag / drop the item</h2>
<div [sortablejs]="items">
<div *ngFor="let item of items">{{ item }}</div>
</div>
<hr>
<h2>See the result</h2>
<div>
<div *ngFor="let item of items">{{ item }}</div>
</div>
`
})
export class AppComponent {
items = [1, 2, 3, 4, 5];
}
Passing the options
Pass the options with sortablejsOptions
property.
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h2>Drag / drop the item</h2>
<div [sortablejs]="items" [sortablejsOptions]="{ animation: 150 }">
<div *ngFor="let item of items">{{ item }}</div>
</div>
<hr>
<h2>See the result</h2>
<div>
<div *ngFor="let item of items">{{ item }}</div>
</div>
`
})
export class AppComponent {
items = [1, 2, 3, 4, 5];
}
Drag & drop between two lists
The only thing which should be done is assigning the group
option to the both list. Everything else is handled automatically.
import { Component } from '@angular/core';
import { SortablejsOptions } from 'angular-sortablejs';
@Component({
selector: 'my-app',
template: `
<h2>Drag / drop the item</h2>
<h3>list 1</h3>
<div class="items1" [sortablejs]="items1" [sortablejsOptions]="options">
<div *ngFor="let item of items1">{{ item }}</div>
</div>
<h3>list 2</h3>
<div class="items2" [sortablejs]="items2" [sortablejsOptions]="options">
<div *ngFor="let item of items2">{{ item }}</div>
</div>
<hr>
<h2>See the result</h2>
<div>
<h3>list 1</h3>
<div *ngFor="let item of items1">{{ item }}</div>
<h3>list 2</h3>
<div *ngFor="let item of items2">{{ item }}</div>
</div>
`
})
export class AppComponent {
items1 = [1, 2, 3, 4, 5];
items2 = ['a', 'b', 'c', 'd', 'e'];
options: SortablejsOptions = {
group: 'test'
};
}
Configure the options globally
If you want to use the same sortable options across different places of your application you might want to set up global configuration. Add the following to your main module to enable e.g. animation: 150
everywhere:
imports: [
...
SortablejsModule.forRoot({
animation: 150
}),
...
]
This value will be used as a default one, but it can be overwritten by a local sortablejsOptions
property.
How it works
The model is automatically updated because you pass the items
as <div [sortablejs]="items">
. The items
variable can be either an ordinary JavaScript array or a reactive forms FormArray
.
If you won't pass anything, e.g. <div sortablejs>
, the items won't be automatically updated, thus you should take care of updating the array on your own using standard Sortable.js
events.
Original events onAdd
, onRemove
, onUpdate
are intercepted by the library in order to reflect the sortable changes into the data. If you will add your own event handlers (inside of the options object) they will be called right after the data binding is done. If you don't pass the data, e.g. <div sortablejs>
the data binding is skipped and only your event handlers will be fired.