enmi12/glossaire/_posts/docs/2010-12-07-sorting.mdown
changeset 0 d970ebf37754
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 ---
       
     2 
       
     3 title: Sorting
       
     4 category: docs
       
     5 layout: default
       
     6 toc:
       
     7   - { title: Markup, anchor: markup }
       
     8   - { title: getSortData option, anchor: getsortdata_option }
       
     9   - { title: sortBy option, anchor: sortby_option }
       
    10   - { title: sortAscending option, anchor: sortascending_option }
       
    11   - { title: Creating interactive buttons, anchor: creating_interactive_buttons }
       
    12 
       
    13 ---
       
    14 
       
    15 Collect data from item element and rearrange their order in the layout with sorting.
       
    16 
       
    17 [**See Demo: Sorting**](../demos/sorting.html)
       
    18 
       
    19 ## Markup
       
    20 
       
    21 Any group of similar items have their own data. It could be a text value, like a title or tag, or a numerical value, like a measurement or grade. For our example, each item element has several data points that can be used for sorting. There's the elemental symbol, number, name of the element, weight, and category.
       
    22 
       
    23 {% highlight html %}
       
    24 
       
    25 <div id="container">
       
    26   <div class="element transition metal" data-category="transition"> 
       
    27     <p class="number">79</p> 
       
    28     <h3 class="symbol">Au</h3> 
       
    29     <h2 class="name">Gold</h2> 
       
    30     <p class="weight">196.966569</p> 
       
    31   </div> 
       
    32     
       
    33   <div class="element metalloid" data-category="metalloid"> 
       
    34     <p class="number">51</p> 
       
    35     <h3 class="symbol">Sb</h3> 
       
    36     <h2 class="name">Antimony</h2> 
       
    37     <p class="weight">121.76</p> 
       
    38   </div>
       
    39 </div>
       
    40 
       
    41 {% endhighlight %}
       
    42 
       
    43 
       
    44 ## getSortData option
       
    45 
       
    46 In order to extract this data from the element, we need to pass in a function to get it via the [`getSortData`](options.html#getsortdata) option.  This option accepts an object, whose values are the functions to extract the data.
       
    47 
       
    48 Each function receives one argument, which represents a jQuery object for each item element. With that argument, the function needs to return the data point.
       
    49 
       
    50 In the example above, to get element name, we would need to get the text from the `.name` element. The same works for symbol.
       
    51 
       
    52 {% highlight javascript %}
       
    53 
       
    54 $('#container').isotope({
       
    55   getSortData : {
       
    56     name : function ( $elem ) {
       
    57       return $elem.find('.name').text();
       
    58     },
       
    59     symbol : function ( $elem ) {
       
    60       return $elem.find('.symbol').text();
       
    61     }
       
    62   }
       
    63 });
       
    64 
       
    65 {% endhighlight %}
       
    66 
       
    67 
       
    68 For numerical data, we can convert a text value into a number with `parseInt()` or `parseFloat()`.
       
    69 
       
    70 {% highlight javascript %}
       
    71 
       
    72 getSortData : {
       
    73   // ...
       
    74   number : function ( $elem ) {
       
    75     return parseInt( $elem.find('.number').text(), 10 );
       
    76   },
       
    77   weight : function ( $elem ) {
       
    78     return parseFloat( $elem.find('.weight').text() );
       
    79   }
       
    80 }
       
    81 
       
    82 {% endhighlight %}
       
    83 
       
    84 The data extracted can be anything accessible in the item element via jQuery. To extract the category data held within the `data-category` attribute, we can use the `.attr()`.
       
    85 
       
    86 {% highlight javascript %}
       
    87 
       
    88 getSortData : {
       
    89   // ...
       
    90   category : function ( $elem ) {
       
    91     return $elem.attr('data-category');
       
    92   }
       
    93 }
       
    94 
       
    95 {% endhighlight %}
       
    96 
       
    97 Get creative! You could sort a list by the width of each item element.
       
    98 
       
    99 {% highlight javascript %}
       
   100 
       
   101 getSortData : {
       
   102   // ...
       
   103   width : function( $elem ) {
       
   104     return $elem.width();
       
   105   }
       
   106 }
       
   107 
       
   108 {% endhighlight %}
       
   109 
       
   110 ## sortBy option
       
   111 
       
   112 For every method set in `getSortData`, Isotope uses that method to build the data for sorting. The data cache is built on initialization so it can be quickly accessed when sorting. With the methods above, we have built data for an item elements name, symbol, number, weight and category.
       
   113 
       
   114 Sorting elements is done with the [`sortBy` option](options.html#sortby). The value needs to match the property name used in the `getSortData` object.
       
   115 
       
   116 With our example, we can use `'name'`, `'symbol'`, `'number'`, `'weight'` and `'category'`.
       
   117 
       
   118 {% highlight javascript %}
       
   119 
       
   120 $('#container').isotope({ sortBy : 'symbol' });
       
   121 
       
   122 {% endhighlight %}
       
   123 
       
   124 There are two additional sorting data methods built in to Isotope. 
       
   125 
       
   126 + `'original-order'` will use the original order of the item elements to arrange them in the layout.
       
   127 + `'random'` is a random order.
       
   128 
       
   129 ## sortAscending option
       
   130 
       
   131 By default, Isotope sorts data in ascension. If our data for name is "Gold, Antimony, Lead, Iron, Silver", when sorted by name, the elements will be ordered ABC.. : "Antimony, Gold, Iron, Lead, Silver."  To reverse the order and sort data in descension, set [`sortAscending`](options.html#sortascending) to <code><span class="kc">false</span></code>.
       
   132 
       
   133 {% highlight javascript %}
       
   134 
       
   135 $('#container').isotope({ 
       
   136   sortBy : 'name',
       
   137   sortAscending : false
       
   138 });
       
   139 
       
   140 {% endhighlight %}
       
   141 
       
   142 ## Creating interactive buttons
       
   143 
       
   144 We can use a simple list for our buttons.
       
   145 
       
   146 {% highlight html %}
       
   147 
       
   148 <ul id="sort-by">
       
   149   <li><a href="#name">name</a></li>
       
   150   <li><a href="#symbol">symbol</a></li>
       
   151   <li><a href="#number">number</a></li>
       
   152   <li><a href="#weight">weight</a></li>
       
   153   <li><a href="#category">category</a></li>
       
   154 </ul>
       
   155 
       
   156 {% endhighlight %}
       
   157 
       
   158 When one of these links is clicked, we can use the `href` attribute as the value for `sortBy` in the Isotope script.
       
   159 
       
   160 {% highlight javascript %}
       
   161 
       
   162 $('#sort-by a').click(function(){
       
   163   // get href attribute, minus the '#'
       
   164   var sortName = $(this).attr('href').slice(1);
       
   165   $('#container').isotope({ sortBy : sortName });
       
   166   return false;
       
   167 });
       
   168 
       
   169 {% endhighlight %}