README.md 9.8 KB
Newer Older
R
RubaXa 已提交
1
# Sortable
D
Dan Dascalescu 已提交
2
Sortable is a minimalist JavaScript library for reorderable drag-and-drop lists.
R
RubaXa 已提交
3

D
Dan Dascalescu 已提交
4 5
Demo: http://rubaxa.github.io/Sortable/

R
RubaXa 已提交
6

R
RubaXa 已提交
7
## Features
R
RubaXa 已提交
8

R
RubaXa 已提交
9 10
 * Supports touch devices and [modern](http://caniuse.com/#search=drag) browsers
 * Can drag from one list to another or within the same list
11
 * CSS animation when moving items
R
RubaXa 已提交
12
 * Supports drag handles *and selectable text* (better than voidberg's html5sortable)
13
 * Built using native HTML5 drag and drop API
R
RubaXa 已提交
14
 * Supports [AngularJS](#ng) and and any CSS library, e.g. [Bootstrap](#bs)
15 16
 * Simple API
 * No jQuery
R
RubaXa 已提交
17 18 19 20 21 22 23 24 25 26 27 28


### Usage
```html
<ul id="items">
	<li>item 1</li>
	<li>item 2</li>
	<li>item 3</li>
</ul>
```

```js
R
* upd  
RubaXa 已提交
29
var el = document.getElementById('items');
R
RubaXa 已提交
30
var sortable = Sortable.create(el);
R
RubaXa 已提交
31 32
```

33 34
You can use any element for the list and its elements, not just `ul`/`li`. Here is an [example with `div`s](http://jsbin.com/luxero/2/edit?html,js,output).

R
RubaXa 已提交
35

R
* upd  
RubaXa 已提交
36 37 38
---


R
RubaXa 已提交
39 40
### Options
```js
D
Dano Alexander 已提交
41
var sortable = new Sortable(el, {
42
	group: "name",  // or { name: "...", pull: [true, false, clone], put: [true, false, array] }
43
	sort: true,  // sorting inside list
R
RubaXa 已提交
44
	disabled: false, // Disables the sortable if set to true.
45 46 47
	store: null,  // @see Store
	animation: 150,  // ms, animation speed moving items when sorting, `0` — without animation
	handle: ".my-handle",  // Drag handle selector within list items
D
Dan Dascalescu 已提交
48
	filter: ".ignore-elements",  // Selectors that do not lead to dragging (String or Function)
49
	draggable: ".item",  // Specifies which items inside the element should be sortable
D
Dan Dascalescu 已提交
50
	ghostClass: "sortable-ghost",  // Class name for the drop placeholder - jsbin.com/luxero/3
R
RubaXa 已提交
51 52 53
	setData: function (dataTransfer, dragEl) {
		dataTransfer.setData('Text', dragEl.textContent);
	},
54

55 56 57 58 59 60 61 62 63 64
	// dragging started
	onStart: function (/**Event*/evt) {
		evt.oldIndex;  // element index within parent
	},
	
	// dragging ended
	onEnd: function (/**Event*/evt) {
		evt.oldIndex;  // element's old index within parent
		evt.newIndex;  // element's new index within parent
	},
R
RubaXa 已提交
65

66
	// Element is dropped into the list from another list
67 68 69 70
	onAdd: function (/**Event*/evt) {
		var itemEl = evt.item;  // dragged HTMLElement
		itemEl.from;  // previous list
		// + indexes from onEnd
R
RubaXa 已提交
71 72
	},

73
	// Changed sorting within list
74 75 76
	onUpdate: function (/**Event*/evt) {
		var itemEl = evt.item;  // dragged HTMLElement
		// + indexes from onEnd
R
RubaXa 已提交
77 78
	},

79
	// Called by any change to the list (add / update / remove)
80 81
	onSort: function (/**Event*/evt) {
		// same properties as onUpdate
82 83
	},

84
	// Element is removed from the list into another list
85 86
	onRemove: function (/**Event*/evt) {
		// same properties as onUpdate
87 88
	},

89
	// Attempt to drag a filtered element
90 91
	onFilter: function (/**Event*/evt) {
		var itemEl = evt.item;  // HTMLElement receiving the `mousedown|tapstart` event.
R
RubaXa 已提交
92 93 94
	}
});
```
R
RubaXa 已提交
95

R
RubaXa 已提交
96 97 98 99
---


#### `handle` option
100 101 102
To make list items draggable, Sortable disables text selection by the user.
That's not always desirable. To allow text selection, define a drag handler,
which is an area of every list element that allows it to be dragged around.
103

R
RubaXa 已提交
104 105
Demo: http://jsbin.com/newize/1/edit?html,js,output

106
```js
R
RubaXa 已提交
107 108
Sortable.create(el, {
	handle: ".my-handle"
109 110 111 112 113
});
```

```html
<ul>
R
RubaXa 已提交
114 115
	<li><span class="my-handle">::</span> list item text one
	<li><span class="my-handle">::</span> list item text two
116 117 118
</ul>
```

D
Dan Dascalescu 已提交
119 120
```css
.my-handle {
R
RubaXa 已提交
121 122
	cursor: move;
	cursor: -webkit-grabbing;
D
Dan Dascalescu 已提交
123 124 125
}
```

R
RubaXa 已提交
126

R
* upd  
RubaXa 已提交
127 128
---

R
RubaXa 已提交
129

130 131 132
#### `group` option
To drag elements from one list into another, both lists must have the same `group` value.
You can also define whether lists can give away, give and keep a copy (`clone`), and receive elements.
R
RubaXa 已提交
133

134 135
 * name: `String` — group name
 * pull: `true|false|'clone'` — ability to move from the list. `clone` — copy the item, rather than move.
L
Lebedev Konstantin 已提交
136
 * put: `true|false|["foo", "bar"]` — whether elements can be added from other lists, or an array of group names from which elements can be taken. Demo: http://jsbin.com/naduvo/2/edit?html,js,output
R
RubaXa 已提交
137 138 139 140


---

141

R
RubaXa 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#### `sort` option
Sorting inside list

Demo: http://jsbin.com/xizeh/2/edit?html,js,output


---


#### `disabled` options
Disables the sortable if set to `true`.

Demo: http://jsbin.com/xiloqu/1/edit?html,js,output

```js
var sortable = Sortable.create(list);

document.getElementById("switcher").onclick = function () {
	var state = sortable.option("disabled"); // get

	sortable.option("disabled", !state); // set
};
```


---


R
RubaXa 已提交
170 171 172 173 174 175 176
#### `filter` option


```js
Sortable.create(list, {
	filter: ".js-remove, .js-edit",
	onFilter: function (evt) {
R
RubaXa 已提交
177 178
		var item = el.item,
			ctrl = evt.target;
R
RubaXa 已提交
179

R
RubaXa 已提交
180 181
		if (Sortable.utils.is(ctrl, ".js-remove")) {  // Click on remove button
			item.parentNode.removeChild(item); // remove sortable item
R
RubaXa 已提交
182
		}
R
RubaXa 已提交
183
		else if (Sortable.utils.is(ctrl, ".js-edit")) {  // Click on edit link
R
RubaXa 已提交
184 185 186 187 188 189 190 191 192 193
			// ...
		}
	}
})
```


---


194 195 196 197
<a name="ng"></a>
### Support AngularJS
Include [ng-sortable.js](ng-sortable.js)

R
RubaXa 已提交
198 199
Demo: http://jsbin.com/naduvo/1/edit?html,js,output

200
```html
R
RubaXa 已提交
201
<div ng-app="myApp" ng-controller="demo">
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	<ul ng-sortable>
		<li ng-repeat="item in items">{{item}}</li>
	</ul>

	<ul ng-sortable="{ group: 'foobar' }">
		<li ng-repeat="item in foo">{{item}}</li>
	</ul>

	<ul ng-sortable="barConfig">
		<li ng-repeat="item in bar">{{item}}</li>
	</ul>
</div>
```


```js
angular.module('myApp', ['ng-sortable'])
R
RubaXa 已提交
219 220 221 222 223 224
	.controller('demo', ['$scope', function ($scope) {
		$scope.items = ['item 1', 'item 2'];
		$scope.foo = ['foo 1', '..'];
		$scope.bar = ['bar 1', '..'];
		$scope.barConfig = { group: 'foobar', animation: 150 };
	}]);
225 226
```

R
RubaXa 已提交
227

R
* upd  
RubaXa 已提交
228 229
---

R
RubaXa 已提交
230

231 232
### Method

233 234 235 236 237

##### option(name:`String`[, value:`*`]):`*`
Get or set the option.


238

239 240 241 242
##### closest(el:`String`[, selector:`HTMLElement`]):`HTMLElement|null`
For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.


243
##### toArray():`String[]`
244
Serializes the sortable's item `data-id`'s into an array of string.
245 246


R
* jsdoc  
RubaXa 已提交
247
##### sort(order:`String[]`)
248
Sorts the elements according to the array.
249

250
```js
R
* upd  
RubaXa 已提交
251 252
var order = sortable.toArray();
sortable.sort(order.reverse()); // apply
253 254 255 256
```


##### destroy()
257
Removes the sortable functionality completely.
258 259 260 261 262 263 264 265


---


### Store
Saving and restoring of the sort.

266 267 268 269 270 271 272 273
```html
<ul>
	<li data-id="1">order</li>
	<li data-id="2">save</li>
	<li data-id="3">restore</li>
</ul>
```

274
```js
275
Sortable.create(el, {
276 277
	group: "localStorage-example",
	store: {
R
* JSDoc  
RubaXa 已提交
278 279 280
		/**
		 * Get the order of elements. Called once during initialization.
		 * @param   {Sortable}  sortable
D
Dan Dascalescu 已提交
281
		 * @returns {Array}
R
* JSDoc  
RubaXa 已提交
282
		 */
283 284 285 286 287
		get: function (sortable) {
			var order = localStorage.getItem(sortable.options.group);
			return order ? order.split('|') : [];
		},

R
* JSDoc  
RubaXa 已提交
288
		/**
D
Dan Dascalescu 已提交
289
		 * Save the order of elements. Called onEnd (when the item is dropped).
R
* JSDoc  
RubaXa 已提交
290 291
		 * @param {Sortable}  sortable
		 */
292 293 294 295 296 297 298 299 300
		set: function (sortable) {
			var order = sortable.toArray();
			localStorage.setItem(sortable.options.group, order.join('|'));
		}
	}
})
```


R
RubaXa 已提交
301 302 303 304 305
---


<a name="bs"></a>
### Bootstrap
306
Demo: http://jsbin.com/luxero/2/edit?html,js,output
R
RubaXa 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

```html
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>


<!-- Latest Sortable -->
<script src="http://rubaxa.github.io/Sortable/Sortable.js"></script>


<!-- Simple List -->
<ul id="simpleList" class="list-group">
	<li class="list-group-item">This is <a href="http://rubaxa.github.io/Sortable/">Sortable</a></li>
	<li class="list-group-item">It works with Bootstrap...</li>
	<li class="list-group-item">...out of the box.</li>
	<li class="list-group-item">It has support for touch devices.</li>
	<li class="list-group-item">Just drag some elements around.</li>
</ul>

<script>
    // Simple list
    Sortable.create(simpleList, { /* options */ });
</script>
D
Dano Alexander 已提交
330
```
331

R
RubaXa 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
---



### Sortable.utils
* on(el`:HTMLElement`, event`:String`, fn`:Function`) — attach an event handler function
* off(el`:HTMLElement`, event`:String`, fn`:Function`) — remove an event handler
* css(el`:HTMLElement`)`:Object` — get the values of all the CSS properties
* css(el`:HTMLElement`, prop`:String`)`:Mixed` — get the value of style properties
* css(el`:HTMLElement`, prop`:String`, value`:String`) — set one CSS properties
* css(el`:HTMLElement`, props`:Object`) — set more CSS properties
* find(ctx`:HTMLElement`, tagName`:String`[, iterator`:Function`])`:Array` — get elements by tag name
* bind(ctx`:Mixed`, fn`:Function`)`:Function` — Takes a function and returns a new one that will always have a particular context
* closest(el`:HTMLElement`, selector`:String`[, ctx`:HTMLElement`])`:HTMLElement|Null` — for each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree
* toggleClass(el`:HTMLElement`, name`:String`, state`:Boolean`) — add or remove one classes from each element
B
Bitdeli Chef 已提交
347

L
Lebedev Konstantin 已提交
348

R
RubaXa 已提交
349

L
Lebedev Konstantin 已提交
350 351 352 353 354
---



## MIT LICENSE
355
Copyright 2013-2014 Lebedev Konstantin <ibnRubaXa@gmail.com>
L
Lebedev Konstantin 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
http://rubaxa.github.io/Sortable/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.