You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.9 KiB
HTML
48 lines
1.9 KiB
HTML
<h2>Sorting<a href="#sorting"></a></h2>
|
|
|
|
<p>You can order the elements based off a function you supply. In the demo above, each item has a <code>data-date-created</code> and <code>data-title</code> attribute. When the select option menu changes, a sort object is passed to shuffle.</p>
|
|
|
|
|
|
<div class="code-block">
|
|
<pre rel="HTML"><code class="language-markup"><select class="sort-options">
|
|
<option value="">Default</option>
|
|
<option value="title">Title</option>
|
|
<option value="date-created">Date Created</option>
|
|
</select></code></pre>
|
|
</div>
|
|
|
|
<div class="code-block">
|
|
<pre rel="HTML"><code class="language-markup"><figure class="picture-item" data-groups='["photography"]' data-date-created="2010-09-14" data-title="Baseball">…</figure></code></pre>
|
|
</div>
|
|
|
|
<div class="code-block">
|
|
<pre rel="JavaScript"><code class="language-javascript">// Sorting options
|
|
$('.sort-options').on('change', function() {
|
|
var sort = this.value,
|
|
opts = {};
|
|
|
|
// We're given the element wrapped in jQuery
|
|
if ( sort === 'date-created' ) {
|
|
opts = {
|
|
reverse: true,
|
|
by: function($el) {
|
|
return $el.data('date-created');
|
|
}
|
|
};
|
|
} else if ( sort === 'title' ) {
|
|
opts = {
|
|
by: function($el) {
|
|
return $el.data('title').toLowerCase();
|
|
}
|
|
};
|
|
}
|
|
|
|
// Filter elements
|
|
$grid.shuffle('sort', opts);
|
|
});</code></pre>
|
|
</div>
|
|
|
|
<p>The <code class="language-javascript">opts</code> parameter can contain two properties. <code class="language-javascript">reverse</code>, a boolean which will reverse the array. <code class="language-javascript">by</code> is a function that is passed the element wrapped in jQuery. In the case above, we’re returning the value of the data-date-created or data-title attributes.</p>
|
|
<p>Calling sort with an empty object will reset the elements to DOM order.</p>
|
|
<p class="demo-link-container">Check out the <a href="#demo">demo</a>.</p>
|