tutorials

QuickSand tutorial – Create a filterable portfolio

Its been a while since I wrote my last tutorial in 2007 ” play youtube videos in flash tutorial “ and now im back with a different topic… jQuery *tsk tsk* now let’s get started!

What we are trying to achieve is a filterable portfolio based on its categories ( all, web & print ) using quicksand and jQuery…

Demo | Download

Download required files first:

  1. jQuery 1.3+
  2. QuickSand Plugin
  3. jQuery easing ( yes Robert Penners  )
  4. Rotation and Scaling Plugin

The HTML:

i omitted the head section with the includes, you can find it in the source code downloadable above!

[cc lang="html" tab_size="2" lines="60"]

[/cc]

A closer look at the filter menu:

[cc lang="html" tab_size="2" lines="2"]

  • web
  • [/cc]

    Notice that i added a title attribute for each, in our case here the title attribute is “web”, which will be pulled by jQuery later in a conditional statement…

    A closer look to the list items of the portfolio:

    [cc lang="html" tab_size="2" lines="2"]

  • [/cc]

    notes the [data-id="id-1"] which is used for sorting and the [data-type="web"] used for filtering

    The CSS:

    Nothing fancy below just few lines of basic CSS styling

    [cc lang="css" tab_size="2" lines="10"]
    #content {width:960px; border: 1px solid #ddd; margin-left: auto ; margin-right: auto ; overflow:hidden; height:auto;}
    #content ul { float:left;}
    #content li { float:left; padding:3px; list-style:none; overflow:hidden;}
    .box { width: 950px;}
    [/cc]

    And finally The jQuery “sortable.js”:

    [cc lang="jquery" tab_size="2" lines="60"]
    $(document).ready(function() {
    // Custom sorting plugin // — No Need to edit this —\\
    (function($) {
    $.fn.sorted = function(customOptions) {
    var options = {
    reversed: false,
    by: function(a) { return a.text(); }
    };
    $.extend(options, customOptions);
    $data = $(this);
    arr = $data.get();
    arr.sort(function(a, b) {
    var valA = options.by($(a));
    var valB = options.by($(b));
    if (options.reversed) {
    return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
    } else {
    return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
    }
    });
    return $(arr);
    };
    })(jQuery);

    //—————————————- EDIT BELOW ——————————\\

    // Filter Handler
    var $filter = $(‘#menu a’);

    // define portfolio “#div” with the portfolio list ( li ) elements in
    var $portfolio = $(‘#content #items’);

    // clone the “#div”
    var $data = $portfolio.clone();

    // attempt to call Quicksand on every click event handler
    $filter.click(function(e) {

    //check if the title attr is set to “all”
    if ($($(this)).attr(“title”) == ‘all’) {

    //if all then show all list ( li )
    var $filteredData = $data.find(‘li’);

    } else {

    //get the title attribute dynamically using $(this)
    var $filteredData = $data.find(‘li[data-type=' + $($(this)).attr("title") + ']‘);

    }
    // finally, call quicksand and dump the $fileredData in
    $portfolio.quicksand($filteredData, {
    duration: 800,
    easing: ‘easeInOutQuad’
    });

    });
    });
    [/cc]

    A closer look at the jQuery code :

    the code below defines $filter, thats where the filter requests will come from based on clicks, now remember that each link element has a unique title attribute.

    [cc lang="jquery" tab_size="2" lines="5"]
    var $filter = $(‘#menu a’);
    [/cc]

    This one defines the path to the portfolio list and adds it to $portfolio

    [cc lang="jquery" tab_size="2" lines="5"]
    var $portfolio = $(‘#content #items’);
    [/cc]

    Clone the portfolio list

    [cc lang="jquery" tab_size="2" lines="5"]
    var $data = $portfolio.clone();
    [/cc]

    Add/bind a click event to the function where it first checks if the button/link which was clicked has the “all” attribute, if its true then it will locate all list elements and adds it to $filteredData, if it was false then it will grab the title attribute dynamically, it will then find lists with the similar title attributes and adds it to the $filteredData .

    [cc lang="jquery" tab_size="2" lines="6"]
    $filter.click(function(e) {
    if ($($(this)).attr(“title”) == ‘all’) {
    var $filteredData = $data.find(‘li’);
    } else {
    var $filteredData = $data.find(‘li[data-type=' + $($(this)).attr("title") + ']‘);
    }
    [/cc]

    time for some magic *tada* call the quicksand and pass it the $filteredData along with Duration & easing type.

    [cc lang="jquery" tab_size="2" lines="5"]
    $portfolio.quicksand($filteredData, {
    duration: 800,
    easing: ‘easeInOutQuad’
    });
    [/cc]

    Happy filtering :-)

    Related posts:

    1. Quick Flash “Prealoder” tutorial .

    Tags: , , , ,

    10 Responses to “QuickSand tutorial – Create a filterable portfolio”

    1. On August 29, 2010 at 12:54 pm BuZain responded with... #

      nice job bro. Can’t wait to use this in my next project.

    2. On August 29, 2010 at 4:26 pm Mohammed Zainal responded with... #

      Glad you like it ya buZain, i deserve a 25% share of your next project ;)

    3. On September 20, 2010 at 10:21 pm Saulo responded with... #

      Nice Job!

      Works in IE?

      Thanks

    4. On September 20, 2010 at 11:04 pm Mohammed Zainal responded with... #

      two words … screw IE ;)

    5. On September 20, 2010 at 11:39 pm Saulo responded with... #

      I agree! ;)

    6. On September 6, 2011 at 10:19 am Kiel responded with... #

      I’m a total noob when it comes to web. I recently setup up a website from a template I got….and it has this setup. I have a gallery of videos with different tags. When I go to my gallery page it currently displays all my videos. Is there a way to set it to display a different tag instead of all?

    7. On October 16, 2011 at 3:32 pm Seaweedgirl responded with... #

      Thanks heaps for writing this tutorial! Helped me out a bunch! :D

      I was just wondering if I would be able to get a little help with making a callback for the quicksand plugin? I’m a complete n00b when it comes to jquery and I’m having trouble figuring it out. I would be so veeeery very grateful if you could help me out! D: I’m using a simple jquery caption (http://thirdroute.com/projects/captify/) for my thumbnail images.

    8. On October 27, 2011 at 2:05 pm AJ responded with... #

      Hi there, great stuff. Is it possible to associate several data types to a single item as I’m looking to have portfolio items show up several different filters?

    9. On October 27, 2011 at 2:41 pm AJ responded with... #

      Just found this quick fix for multiple data types (thanks to: rozzy2049)
      Original post at: https://github.com/razorjack/quicksand/issues/21

      - – - – - – -

      Just in case anyone stumbles onto this issue. Look for the code in your main.js ( although I found it in the sortable.js ):

      else {
      // find all li elements that have our required $filterType
      // values for the data-type element
      var $filteredData = $data.find(‘li[data-type=' + $filterType + ']‘);
      }

      edit the line: var $filteredData = $data.find(‘li[data-type=' + $filterType + ']‘); to include ” ~ ” in the data type, so that the line should then read:

      var $filteredData = $data.find(‘li[data-type~=' + $filterType + ']‘);

      Once completed, you can go back to your itemized list and define multiple categories for a single data element, all you need to do is separate categories with a single blank space (i.e.):

      Test

      where “web” and “media” are two different categories that this data element will be sorted into…

      Took me forever to solve this issue for myself. Hopefully someone else finds it handy

    10. On November 12, 2011 at 9:32 pm Jason Todd responded with... #

      Thanks for the multiple data-types fix. Absolute life saver!

    Add your response