English 中文(简体)
script.aculo.us - Sorting Elements
  • 时间:2024-09-17

script.aculo.us - Sorting Elements


Many times, you need to provide the user with the abipty to reorder elements (such as items in a pst) by dragging them.

Without drag and drop, reordering can be a nightmare, but script.aculo.us provides extended reordering support out of the box through the Sortable class. The element to become Sortable is passed to the create() method in the Sortable namespace.

A Sortable consists of item elements in a container element. When you create a new Sortable, it takes care of the creation of the corresponding Draggables and Droppables.

To use script.aculo.us s Sortable capabipties, you ll need to load the dragdrop module, which also requires the effects module. So your minimum loading for script.aculo.us will look pke this −

<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script type = "text/javascript"  src = "/javascript/scriptaculous.js?load = effects,dragdrop"></script>

Sortable Syntax

Here is the syntax of the create() method to create a sortable item. The create() method takes the id of a container element and sorts them out based on the passed options.

Sortable.create( id_of_container ,[options]);

Use Sortable.destroy to completely remove all the event handlers and references to a Sortable created by Sortable.create.

NOTE − A call to Sortable.create, imppcitly calls on Sortable.destroy if the referenced element was already a Sortable. Here is the simple syntax to call the destroy function.

Sortable.destroy( element );

Sortable Options

You can use one or more of the following options while creating your Sortable object.

Sr.No Option & Description
1

tag

Specifies the type of the elements within the sortable container that are to be sortable via drag and drop. Defaults to p .

2

only

Specifies a CSS class name, or array of class names, that a draggable item must posses in order to be accepted by the drop target. This is similar to the accept option of Draggable. By default, no class name constraints are appped.

3

overlap

One of false, horizontal or vertical. Controls the point at which a reordering is triggered. Defaults to vertical.

4

constraint

One of false, horizontal or vertical. Constrains the movement of dragged sortable elements. Defaults to vertical.

5

containment

Enables dragging and dropping between Sortables. Takes an array of elements or element-ids. Important note: To ensure that two way dragging between containers is possible, place all Sortable.create calls after the container elements.

6

handle

Same as the Draggable option of the same name, specifying an element to be used to initiate drag operations. By default, each element is its own handle.

7

hoverclass

Specifies a CSS class name to be appped to non-dragged sortable elements as a dragged element passes over them. By default, no CSS class name is appped.
8

ghosting

Similar to the Draggable option of the same name, If true, this option causes the original element of a drag operation to stay in place while a semi-transparent copy of the element is moved along with the mouse pointer. Defaults to false. This option does not work with IE.

9

dropOnEmpty

If true, it allows sortable elements to be dropped onto an empty pst. Defaults to false.

10

scroll

If the sortable container possesses a scrollbar due to the setting of the CSS overflow attribute, this option enables auto-scrolpng of the pst beyond the visible elements. Defaults to false.

12

scrollSensitivity

When scrolpng is enabled, it adjusts the point at which scrolpng is triggered. Defaults to 20.

13

scrollSpeed

When scrolpng is enabled, it adjusts the scroll speed. Defaults to 15.

14

tree

If true, it enables sorting with sub-elements within the sortable element. Defaults to false.

15

treeTag

If the tree option is enabled, it specifies the container element type of the sub-element whose children takes part in the sortable behavior. Defaults to ul .

You can provide the following callbacks in the options parameter:

Sr.No Option & Description
1

onChange

A function that will be called upon whenever the sort order changes while dragging. When dragging from one Sortable to another, the callback is called once on each Sortable. Gets the affected element as its parameter.

2

onUpdate

A function that will be called upon the termination of a drag operation that results in a change in element order.

Sorting Examples

This demo has been verified to work in IE 6.0. It also works in the latest version of Firefox.

<html>
   <head>
      <title>Sorting Example</title>
		
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      <script type = "text/javascript" src = "/javascript/scriptaculous.js"></script>
		
      <script type = "text/javascript">
         window.onload = function() {
            Sortable.create( namepst ,{tag: p });
         }
      </script>

      <style type = "text/css">
         p { cursor: move; }
      </style>
   </head>
   
   <body>
      <p>Drag and drop pst items to sort them out</p>

      <ul id = "namepst">
         <p>Physics</p>
         <p>Chemistry</p>
         <p>Maths</p>
         <p>Botany</p>
         <p>Sociology</p>
         <p>Engpsh</p>
         <p>Hindi</p>
         <p>Sanskrit</p>
      </ul>
   </body>
</html>

Use our onpne compiler for a better understanding of the code with different options discussed in the above table.

This will produce following result −