• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🧐 A grid that allows you to sort all items via drag & drop. Items can be sorted in all directions (↕️ and ↔️). You can also select and sort multiple items at the same time by pressing ctrl and click on the item.

Ng-sortgrid

Travis build badge codecov angular10

Logo

Grid demo

Download

npm i -s ng-sortgrid

Import the NgsgModule in your AppModule.

  import {NgsgModule} from 'ng-sortgrid'
  ...
  @NgModule({
    imports: [BrowserModule, NgsgModule],
    //...
  })  
  ...

Apply the directive

Loop over your elements with *ngFor. 🛎️ the items needs to be an array. Alternate you can also use the async pipe to pass in your items.

Grid demo

Apply the ngSortgridItem directive

Grid demo

React on changes

In most cases you are interested in the new sort order. Often you want to store them in local storage or even send them to the backend. To do so the following two steps are needed in addition to the "Getting started" step.

Pass your items to the directive via the ngSortGridItems input.

Grid demo React on the 'sorted' output event. The sorted output event emits a NgsgOrderChange which contains the previousOrder and the currentOrder

Grid demo

Group sortgrids

In case you have more than one sortgriditem on the page you need to group the sortgriditems to avoid dropping drags from one group in another group. Pass in a unique name to the ngSortGridGroup input

Grid demo

Use the async pipe

You can also use the async pipe to display items

Grid demo

Style your items on different events

The ng-sortgrid adds different classes on different events to your items. You can either use those classes to style the appereance of your items on certain events or you can include the build in CSS from the ng-sortgrid library.

Integrate the build in CSS

To integrate the built in Stylesheet just import in in your angular.json.

    "styles": [
              "node_modules/ng-sortgrid/styles/ngsg.css",
            ],

Alternative you can provide custom styles for the different classes listed bellow

Class Description
ng-sg-placeholder This class is added to the placeholder item which previews where the item is inserted
ng-sg-dropped This class is added as soon after you drop an item. The class will be on the item for 500 milliseconds before it gets removed
ng-sg-selected This class is added when you press the CMD or the Ctrl Key and Click on an item. It indicates which items are selected for the multi drag&drop
ng-sg-active This class is added when dragging item

Scrolling

The ng-sortgrid has a autoScroll flag which you can use to enable autoScroll. If you enable autoScroll the screen will start to scroll in the following scenario.

Grid demo

  • If you drag an element in the top 50px of the screen
  • If you drag an element in the bottom 50px of the screen

Custom scroll points

Sometimes its not enough to only scroll once you drag over the top view port border. Imagine that you have a fixed navbar at the top of your page. In this case you need to scroll once you drag an element over the navbar.

Scroll speed (default 50)

The scrollSpeed property accepts a number and allows you to specify the scrolling speed.

Check out the scroll demo

API

Inputs

Value Description Default
ngSortGridGroup: string Groups a grid - avoids that items from one grid can be dragged to another grid undefined
ngSortGridItems: any[] Sort grid items. Pass down a list of all your items. This list is needed to enable the sorting feature. undefined
autoScroll: boolean Flag to enable autoscrolling false
scrollPointTop: number Custom top scrollpoint in pixels if autoscroll is applied we start scrolling if we pass the top border
scrollPointBottom: number Custom bottom scrollpoint in pixels if autoscroll is applied we start scrolling if we pass the bottom border
scrollSpeed: number Scrollspeed, the higher the value, the higher we scroll. 50 - only applies if autoscrolling is on

Outputs

Value Description Default
sorted: EventEmitter<NgsgOrderChange Emits an event after we sorted the items, each event is of type NgsgOrderChange. The NgsgOrderChange contains the previousOrder and the currentOrder. Both are freshly created arrays. undefined

Mobile usage

If you want to use those events on mobile you probably have to use some polyfills in order to emit all the needed events. Including this polyfill in your app should do the trick. https://github.com/timruffles/mobile-drag-drop.

More Repositories

1

svg-to-ts

Build performant SVG icon libraries by converting raw SVG files to TypeScript that is optimized for modern tree shaking mechanisms.
TypeScript
273
star
2

import-conductor

Automatically organize your TypeScript import statements to keep your code clean and readable.
TypeScript
59
star
3

ng-samurai

Angular schematics to improve tree shaking of Angular libraries
TypeScript
47
star
4

city-quiz

TypeScript
42
star
5

node-command-line-starter

Starter project that provides you with all the necessary setup to quickly create your node-command-line utility. Focus on your util and don't loose time with the project setup.
TypeScript
39
star
6

svg-icon-library-starter

Build an astonishing framework-agnostic SVG icon library with ease. Out of the box icon optimization, build process, and icon showcase. 🚀
CSS
38
star
7

primeNG-advanced-growl

The AdvGrowlModule is a wrapper around the growl module from PrimeNG. This wrapper was created because PrimeNG is missing some features.
TypeScript
18
star
8

nx-release

TypeScript
17
star
9

replace-json-property

CLI tool to replace a specific properties value with a new value in a JSON file.
JavaScript
17
star
10

ng-simple-alert

TypeScript
16
star
11

dinosaur-icons

TypeScript
13
star
12

pretty-html-log

pretty-html-log is a tool that allows you to highlight and pretty print your html string to console.
TypeScript
11
star
13

ng-if-responsive

A simple project that provides helpers to render or remove elements from the DOM based on screen size. Its like ngIf, just responsive.
TypeScript
5
star
14

icons-monorepo

This repository shows how Icons can be used inside an NX monorepo
TypeScript
5
star
15

my-awesome-lib-with-jest

HTML
5
star
16

monoleasa

TypeScript
4
star
17

ng-for-else

Render templates if Angulars ngFor returns an empty list or null
TypeScript
4
star
18

angular-download-tracker

TypeScript
4
star
19

HarryPotter-names

JavaScript
3
star
20

http-retry

TypeScript
3
star
21

mat-filter-table

TypeScript
3
star
22

ng-form

TypeScript
3
star
23

the-mighty-eleven

TypeScript
2
star
24

Footradamus-old

Footradamus is app that trys to predict Football Results based on game statistics
JavaScript
2
star
25

providerouter

TypeScript
2
star
26

kreuzerk

2
star
27

harness-testing-author-example

This repository shows how you can write custom harness tests for your UI components
TypeScript
2
star
28

inject-pilot

JavaScript
2
star
29

ng-web-console

SCSS
1
star
30

release-me-daddy

TypeScript
1
star
31

a-transform-transformer

JavaScript
1
star
32

monoleasa-is-single

TypeScript
1
star
33

ng-astro-todo

1
star
34

greek-mythology-wiki

TypeScript
1
star
35

ng-ui-magic

TypeScript
1
star
36

nestjs-showcase

TypeScript
1
star
37

a16-app

TypeScript
1
star
38

ng-cd-hooks

TypeScript
1
star
39

npmiall

JavaScript
1
star
40

ng-change-detection

TypeScript
1
star
41

rxjs-tips

Project that displays various RxJs tips & tricks
1
star
42

ng-parsel

TypeScript
1
star
43

qwikest-todo-app

TypeScript
1
star
44

ng14

TypeScript
1
star
45

resuable-subforms

TypeScript
1
star
46

ivy-playground

HTML
1
star
47

earthquakeVisualizer

A small app based on RXJS and Leaflet that localizes earthquakes in America. This app was build with the help of the book Reactive Programming with RxJS
JavaScript
1
star
48

ng-eggsplore

TypeScript
1
star
49

dynamic-nested-forms

TypeScript
1
star
50

signal-inputs

HTML
1
star
51

signal_queries

TypeScript
1
star
52

frontend-heroes

Collection of exercises for the Frontend Heroes Workshop.
TypeScript
1
star
53

cubic

TypeScript
1
star