README.md 9.3 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 90
	onFilter: function (/**Event*/evt) {
		var itemEl = evt.item;  // HTMLElement receiving the `mousedown|tapstart` event.
R
RubaXa 已提交
91 92 93
	}
});
```
R
RubaXa 已提交
94

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


#### `handle` option
99 100 101
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.
102 103

```js
R
RubaXa 已提交
104
var sortable = new Sortable(el, {
D
Dano Alexander 已提交
105
        handle: ".my-handle"
106 107 108 109 110
});
```

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

D
Dan Dascalescu 已提交
116 117 118 119 120 121
```css
.my-handle {
	cursor: move
}
```

R
RubaXa 已提交
122

R
* upd  
RubaXa 已提交
123 124
---

R
RubaXa 已提交
125

126 127 128
#### `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 已提交
129

130 131
 * name: `String` — group name
 * pull: `true|false|'clone'` — ability to move from the list. `clone` — copy the item, rather than move.
L
Lebedev Konstantin 已提交
132
 * 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 已提交
133 134 135 136


---

137

138 139 140 141
<a name="ng"></a>
### Support AngularJS
Include [ng-sortable.js](ng-sortable.js)

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

144
```html
R
RubaXa 已提交
145
<div ng-app="myApp" ng-controller="demo">
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	<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 已提交
163 164 165 166 167 168
	.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 };
	}]);
169 170
```

R
RubaXa 已提交
171

R
* upd  
RubaXa 已提交
172 173
---

R
RubaXa 已提交
174

175 176
### Method

177 178 179 180 181

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


182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
##### 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.

```js
var editableList = new Sortable(list, {
	filter: ".js-remove, .js-edit",
	onFilter: function (evt) {
		var el = editableList.closest(evt.item); // list item

		if (editableList.closest(evt.item, ".js-remove")) { // Click on remove button
			el.parentNode.removeChild(el); // remove sortable item
		}
		else if (editableList.closest(evt.item, ".js-edit")) { // Click on edit link
			// ...
		}
	}
})
```


203
##### toArray():`String[]`
204
Serializes the sortable's item `data-id`'s into an array of string.
205 206


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

210
```js
R
* upd  
RubaXa 已提交
211 212
var order = sortable.toArray();
sortable.sort(order.reverse()); // apply
213 214 215 216
```


##### destroy()
217
Removes the sortable functionality completely.
218 219 220 221 222 223 224 225


---


### Store
Saving and restoring of the sort.

226 227 228 229 230 231 232 233
```html
<ul>
	<li data-id="1">order</li>
	<li data-id="2">save</li>
	<li data-id="3">restore</li>
</ul>
```

234
```js
235
Sortable.create(el, {
236 237
	group: "localStorage-example",
	store: {
R
* JSDoc  
RubaXa 已提交
238 239 240
		/**
		 * Get the order of elements. Called once during initialization.
		 * @param   {Sortable}  sortable
D
Dan Dascalescu 已提交
241
		 * @returns {Array}
R
* JSDoc  
RubaXa 已提交
242
		 */
243 244 245 246 247
		get: function (sortable) {
			var order = localStorage.getItem(sortable.options.group);
			return order ? order.split('|') : [];
		},

R
* JSDoc  
RubaXa 已提交
248
		/**
D
Dan Dascalescu 已提交
249
		 * Save the order of elements. Called onEnd (when the item is dropped).
R
* JSDoc  
RubaXa 已提交
250 251
		 * @param {Sortable}  sortable
		 */
252 253 254 255 256 257 258 259 260
		set: function (sortable) {
			var order = sortable.toArray();
			localStorage.setItem(sortable.options.group, order.join('|'));
		}
	}
})
```


R
RubaXa 已提交
261 262 263 264 265
---


<a name="bs"></a>
### Bootstrap
266
Demo: http://jsbin.com/luxero/2/edit?html,js,output
R
RubaXa 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

```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 已提交
290
```
291

R
RubaXa 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
---



### 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 已提交
307

L
Lebedev Konstantin 已提交
308

R
RubaXa 已提交
309

L
Lebedev Konstantin 已提交
310 311 312 313 314
---



## MIT LICENSE
315
Copyright 2013-2014 Lebedev Konstantin <ibnRubaXa@gmail.com>
L
Lebedev Konstantin 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
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.