YUI 3.x Home -

YUI Library Examples: Sortable List Utility: Simple Sortable List - Floated Nodes

Sortable List Utility: Simple Sortable List - Floated Nodes

Making a simple sortable list with floated nodes.

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 (em).

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

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

  1. #demo {
  2. border: 1px solid black;
  3. height: 200px;
  4. }
  5. #demo em {
  6. display: block;
  7. float: left;
  8. padding: 3px;
  9. width: 150px;
  10. border: 1px solid black;
  11. margin: 3px;
  12. background-color: #8DD5E7;
  13. }
    #demo {
        border: 1px solid black;
        height: 200px;
    }
    #demo em {
        display: block;
        float: left;
        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: 'em',
  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: 'em',
        //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