Giter Site home page Giter Site logo

singular_sortable's Introduction

singular_sortable

singluar_sortable is a jQuery UI plugin for enhancing the behavior of the jQuery UI sortable Interaction.

The Problem

Sortable is very nice, but it tends to treat the sorted elements as one lump group. The recommended way of dealing with the sorting event is to serialize the entire list and send it back to the server for processing.

But, if you’re using the Rails acts_as_list plugin (or something similar) to handle position info, then you’ve already got a powerful way of handling sorting on the backend. All you really need to know is the new integer position value for the moved item.

The Solution

singular_sortable will automatically do all the position calculation on the client side and determine the new position value to be sent to the server for only the singular item that was moved.

Requirements

  • jQuery
  • jQuery UI
  • HTML5 – singular_sortable uses the new HTML5 data- attributes, but only as a way to store the data. There’s no assumption of the underlying support for retrieving the values. Instead, jQuery is used for that. In other words, it will work in all browsers, but if your DOCTYPE isn’t HTML5, then validators will complain.

Usage

You will need to add an attribute to each draggable sort item containing the object’s position value. We recommend using the new HTML5 data- attributes, as this is a way to do it with valid HTML. Otherwise, it will be technically invalid, but should still work.

Example HTML

<ul id='my-sortable'>
  <li data-position='1'>Your first item.</li>
  <li data-position='2'>Your second item.</li>
  <li data-position='3'>And so on...</li></ul>

The javascript

jQuery('#my-sortable').sortable({
  }).singular_sortable({
    positionAttribute : 'data-position',
    update : function(e, ui) {
      var newPositionValue = ui.numericalPosition;
      // Now, you can use an AJAX call to send the new position back to the server, no messing with toArray or serialize
    }
  });

What it’s doing

singluar_sortable borrows heavily from acts_as_list for inspiration. It maintains the position information in an attribute on the sort items. When an item is moved, the position values are recalculated and updated with the new sort ordering.

Sorting paginated lists

The inspiration for this came from trying to sort a paginated list. By using the actual position values from the database, you will be able to easily sort items within a single page of a paginated list, since you’re using their absolute position values in the list. There’s currently no support for sorting an item onto a different page, but I’m trying to figure out a good way to do this. Still, that’s probably outside the scope of singular_sortable

singular_sortable's People

Stargazers

John Rees avatar Thomas avatar Chris Lee avatar brownman avatar Erick Schmitt avatar Bence Nagy avatar Micah Wedemeyer avatar  avatar

Watchers

James Cloos avatar

Forkers

bitsushi tbuyle

singular_sortable's Issues

Incompatibility with Latest jQuery UI?

Hi,

Thanks for the great plugin! Unfortunately I couldn't get it to work with the latest version of jQuery UI (1.8.14). My knowledge of jQuery UI plugins is extremely limited, but I got it to work by changing the following line from this:

...
_onSortUpdate : function(e, ui) {
  var me = $(this).singular_sortable("option", "me");
...

to this:

...
_onSortUpdate : function(e, ui) {
  var me = $(this).data('singular_sortable').options.me;
...

I'm afraid I don't know if that's the right way to fix this, but that's what I found worked for me. Any insights would be greatly appreciated!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.