Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rniemeyer/knockout-sortable
A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality
https://github.com/rniemeyer/knockout-sortable
Last synced: 3 months ago
JSON representation
A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality
- Host: GitHub
- URL: https://github.com/rniemeyer/knockout-sortable
- Owner: rniemeyer
- License: mit
- Created: 2012-02-04T04:59:42.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2021-08-18T13:01:48.000Z (about 3 years ago)
- Last Synced: 2024-04-17T05:15:59.037Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 819 KB
- Stars: 548
- Watchers: 35
- Forks: 128
- Open Issues: 56
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-knockout - Sortable - A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality (Plugins and libraries)
README
**knockout-sortable** is a binding for [Knockout.js](http://knockoutjs.com/) designed to connect observableArrays with jQuery UI's sortable functionality. This allows a user to drag and drop items within a list or between lists and have the corresponding observableArrays updated appropriately.
**Basic Usage**
* using anonymous templates:
```html
```
* using named templates:
```html
<li data-bind="text: name"></li>
```
Note: The sortable binding assumes that the child "templates" have a single container element. You cannot use containerless bindings (comment-based) bindings at the top-level of your template, as the jQuery draggable/sortable functionality needs an element to operate on.
Note2: (*Update: 0.9.0 adds code to automatically strip leading/trailing whitespace*) When using named templates, you will have the best results across browsers, if you ensure that there is only a single top-level node inside your template with no surrounding text nodes. Inside of the top-level nodes, you can freely use whitespace/text nodes. So, you will want:
```html
<li data-bind="text: name">
<span data-bind="text: name"></span>
</li>
<li>
<span data-bind="text: name"></span>
</li>
```
**Additional Options**
* **connectClass** - specify the class that should be used to indicate a droppable target. The default class is "ko_container". This value can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.connectClass`.
* **allowDrop** - specify whether this container should be a target for drops. This can be a static value, observable, or a function that is passed the observableArray as its first argument. If a function is specified, then it will be executed in a computed observable, so it will run again whenever any dependencies are updated. This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.allowDrop`.
* **beforeMove** - specify a function to execute prior to an item being moved from its original position to its new position in the data. This function receives an object for its first argument that contains the following information:
* `arg.item` - the actual item being moved
* `arg.sourceIndex` - the position of the item in the original observableArray
* `arg.sourceParent` - the original observableArray
* `arg.sourceParentNode` - the container node of the original list
* `arg.targetIndex` - the position of the item in the destination observableArray
* `arg.targetParent` - the destination observableArray
* `arg.cancelDrop` - this defaults to false and can be set to true to indicate that the drop should be cancelled.
This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.beforeMove`. This callback also receives the `event` and `ui` objects as the second and third arguments.
* **afterMove** - specify a function to execute after an item has been moved to its new destination. This function receives an object for its first argument that contains the following information:
* `arg.item` - the actual item being moved
* `arg.sourceIndex` - the position of the item in the original observableArray
* `arg.sourceParent` - the original observableArray
* `arg.sourceParentNode` - the container node of the original list. Useful if moving items between lists, but within a single array. The value of `this` in the callback will be the target container node.
* `arg.targetIndex` - the position of the item in the destination observableArray
* `arg.targetParent` - the destination observableArray
This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.afterMove`. This callback also receives the `event` and `ui` objects as the second and third arguments.
* **dragged** - specify a function to execute after a draggable item has been dropped into a sortable. This callback receives the drag item as the first argument, the `event` as the second argument, and the `ui` object as the third argument. If the function returns a value, then it will be used as item that is dropped into the sortable. This can be used as an alternative to the original item including a `clone` function.
* **isEnabled** - specify whether the sortable widget should be enabled. If this is an observable, then it will enable/disable the widget when the observable's value changes. This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.isEnabled`.
* **strategyMove** - specify whether dropping an item within the same list should move the same item to the new index rather than removing and re-adding the item in the new location (which is the default and causes the item to be re-rendered). This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.strategyMove`. The default value is `false`.
* **options** - specify any additional options to pass on to the `.sortable` jQuery UI call. These options can be specified in the binding or specified globally by setting `ko.bindingHandlers.sortable.options`.
* **afterAdd, beforeRemove, afterRender, includeDestroyed, templateEngine, as** - this binding will pass these options on to the template binding.
**Draggable binding**
This library also includes a `draggable` binding that you can place on single items that can be moved into a `sortable` collection. When the item is dropped into a sortable, the plugin will attempt to call a `clone` function on the item to make a suitable copy of it, otherwise it will use the item directly. Additionally, the `dragged` callback can be used to provide a copy of the object, as described above.
* using anonymous templates:
```html
```
* using named templates:
```html
<span data-bind="text: name"></span>
```
**Additional Options**
* **connectClass** - specify a class used to indicate which sortables that this draggable should be allowed to drop into. The default class is "ko_container". This value can be passed in the binding or configured globally by setting `ko.bindingHandlers.draggable.connectClass`.
* **isEnabled** - specify whether the draggable widget should be enabled. If this is an observable, then it will enable/disable the widget when the observable's value changes. This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.draggable.isEnabled`.
* **options** - specify any additional options to pass on to the `.draggable` jQuery UI call. These options can be specified in the binding or specified globally by setting `ko.bindingHandlers.draggable.options`.
**Droppable binding**
This library also includes a `droppable` binding that you can place on items which are targets for any `draggable` item. The binding can update an `observable` or a simple `function` on your viewmodel.
```html
Drop Items Here
```
**Additional options**
* **isEnabled** - specify whether the droppable widget should be enabled. If this is an observable, then it will enable/disable the widget when the observable's value changes. This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.droppable.isEnabled`.
* **options** - specify any additional option to pass to the `.droppable` jQuery UI call. When using options, your method or observable should be provided on the `data` property.
```html
Drop Items Here
**Touch Support** - for touch support take a look at: http://touchpunch.furf.com/
**Build:** This project uses [grunt](http://gruntjs.com/) for building/minifying.
**Examples** The `examples` directory contains samples that include a simple sortable list, connected lists, and a seating chart that takes advantage of many of the additional options.
**Fiddles**
* simple: http://jsfiddle.net/rniemeyer/hw9B2/
* connected: http://jsfiddle.net/rniemeyer/Jr2rE/
* draggable: http://jsfiddle.net/rniemeyer/AC49j/
* seating chart: http://jsfiddle.net/rniemeyer/UdXr4/
**License**: MIT [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)