How to load entries via ajax when a filter is clicked? I have a page that lists all entries from the section 'work'. I would like the user to be able to filter these entries by category but via ajax so I don't have to go to a new page.

{% set entries = craft.entries.section('work').limit(null).search('categories:hotel') %}


This works but each category has to have its own page. Is it possibly to load the filtered entries by ajax? Something using {% if craft.request.isAjax %}?
Here is the relevant html:



    &lt;div class="filters"&gt;
        &lt;p class="categories"&gt;Categories&lt;/p&gt;
        &lt;p&gt;&lt;a href="{{ url('work') }}"&gt;All&lt;/a&gt;&lt;/p&gt;
        &lt;p&gt;&lt;a href="{{ url('work/hotel') }}"&gt;Hotel&lt;/a&gt;&lt;/p&gt;
        &lt;p&gt;&lt;a href="{{ url('work/bar') }}"&gt;Bar&lt;/a&gt;&lt;/p&gt;
        &lt;p&gt;&lt;a href="{{ url('work/club') }}"&gt;Club&lt;/a&gt;&lt;/p&gt;
        &lt;p&gt;&lt;a href="{{ url('work/restaurant') }}"&gt;Restaurant&lt;/a&gt;&lt;/p&gt;
        &lt;p&gt;&lt;a href="{{ url('work/retail') }}"&gt;Retail&lt;/a&gt;&lt;/p&gt;
    &lt;/div&gt;

    {% set entries = craft.entries.section('work').limit(null) %}
    &lt;div class="projects"&gt;
        &lt;ul&gt;
            {% for entry in entries %}
                &lt;li class="project"&gt;
                    &lt;a href="{{ entry.url }}"&gt;
                        {% for asset in entry.mainPhoto %}
                            &lt;div class="unveil-wrapper"&gt;
                                &lt;img class="unveil" src="{{ url('assets/images/placeholder.png') }}" alt="{{ entry.title }}"&gt;
                            &lt;/div&gt;
                        {% endfor %}
                        &lt;p class="name"&gt;{{ entry.title }}&lt;/p&gt;
                        &lt;p class="address"&gt;{{ entry.address }}&lt;/p&gt;
                    &lt;/a&gt;
                &lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;
    &lt;/div&gt;

&lt;/section&gt;
There's a ton of ways to pull this off, but in my opinion setting up pages/URLs for each category is probably a good idea, for SEO and sharing reasons. It's not something that requires a lot of work and would make implementing the AJAX part a breeze. Another win is that you're going to have a solution which will actually work without AJAX or JavaScript, too – which is always a plus :)

To start, make sure the template path for your category group points to the same template that you use for your work index listing page (e.g. work/index), and that the URL pattern is work/{slug}.

Then, amend the listing template like this – the idea is to add the category filter to the craft.entries query only when one of the category URLs are requested.

{# Your basic work entries query #}
{% set query = {
    section : 'work',
    limit : null
} %}

{% If a category URL is requested, Craft will have populated your template with a global `category` variable #}
{% if category is defined %}
    {% set query = query|merge({
        search : 'categories:'~category.slug
    }) %}
{% endif %}

{# Fetch entries #}
{% set entries = craft.entries(query) %}

&lt;div class="projects"&gt;
    &lt;ul&gt;
        {% for entry in entries %}
            &lt;li class="project"&gt;
                ...
            &lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
&lt;/div&gt;


For the AJAX part, here's a super simple implementation – this example uses jQuery, and the concept is basically that we'll have the category URLs load using AJAX, and then just swap out the contents of the .projects DOM element with the returned HTML – no need for a separate AJAX template, JSON or even a craft.request.isAjaxRequest test. It's probably also a good idea to use the HTML5 History API to update the browser's address bar:

// Check if the browser supports HTML5 history
var historySupport = !!(window.history &amp;&amp; history.pushState);

// Add an event listener for filter clicks
$(function () {
    $('body').on('click', '.filters a', onFilterClick);
});

// Event handler for the filter click
function onFilterClick (e)
{
    e.preventDefault();
    var $filter = $(e.currentTarget);
    var href = $filter.attr('href');
    if (historySupport) {
        // Update the browser's address bar
        history.pushState(null, null, href);  
    }
    $.ajax($filter.attr('href), {
        dataType : 'html',
        success : function (response) {
            refreshProjects(response);
        }
    });
}

function refreshProjects (html)
{

    // Update the &lt;title&gt; tag
    document.title = $(html).filter('title').text();

    // Update the .projects DOM element with new HTML
    var $html = $('&lt;div /&gt;').append(html);
    $('.projects').html($html.find('.projects').html());

}