YUI 3.x Home -

YUI Library Examples: Sortable List Utility: Simple Sortable List

Sortable List Utility: Simple Sortable List

Making a simple sortable list.

  • Item #1
  • Item #2
  • Item #3
  • Item #4
  • Item #5
  • Item #6
  • Item #7
  • Item #8
  • Item #9
  • Item #10

Setting up the list

First we need to create the HTML structure for the list. Since Sortable uses Y.DD.Delegate, we need to set up a delegation container (#demo) and the list items (li).

  1. <div id="demo">
  2. <ul>
  3. <li>Item #1</li>
  4. <li>Item #2</li>
  5. <li>Item #3</li>
  6. <li>Item #4</li>
  7. <li>Item #5</li>
  8. <li>Item #6</li>
  9. <li>Item #7</li>
  10. <li>Item #8</li>
  11. <li>Item #9</li>
  12. <li>Item #10</li>
  13. </ul>
  14. </div>
<div id="demo">
    <ul>
        <li>Item #1</li>
        <li>Item #2</li>
        <li>Item #3</li>
        <li>Item #4</li>
        <li>Item #5</li>
        <li>Item #6</li>
        <li>Item #7</li>
        <li>Item #8</li>
        <li>Item #9</li>
        <li>Item #10</li>
    </ul>
</div>

Now we give the list some CSS to make it visible.

  1. #demo li {
  2. list-style-type: none;
  3. padding: 3px;
  4. width: 150px;
  5. border: 1px solid black;
  6. margin: 3px;
  7. background-color: #8DD5E7;
  8. }
    #demo li {
        list-style-type: none;
        padding: 3px;
        width: 150px;
        border: 1px solid black;
        margin: 3px;
        background-color: #8DD5E7;
    }

Setting up the YUI Instance

Now we need to create our YUI instance and tell it to load the sortable module.

  1. YUI().use('sortable'
YUI().use('sortable'

Making the list draggable

Now that we have a YUI instance with the sortable module, we need to instantiate the Sortable instance on the list.

  1. YUI().use('sortable', function(Y) {
  2. var sortable = new Y.Sortable({
  3. //The container of the list
  4. container: '#demo',
  5. //The items to make sortable
  6. nodes: 'li',
  7. //Set the opacity of the sorted item to make it
  8. //almost invisible
  9. opacity: '.1'
  10. });
  11.  
  12. });
YUI().use('sortable', function(Y) {
    var sortable = new Y.Sortable({
        //The container of the list
        container: '#demo',
        //The items to make sortable
        nodes: 'li',
        //Set the opacity of the sorted item to make it
        //almost invisible
        opacity: '.1'
    });
 
});

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings