Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="facet-list">
  <div class="facets-wrapper">
    <h1>Facets</h1>
    <div id="facets"></div>
  </div>
</div>

<div class="hits-wrapper" id=container>
  <h1>Results</h1>
  <div id="hits"></div>
</div>
              
            
!

CSS

              
                * {
  font-family: 'helvetica', sans-serif;
}

#search-box {
  margin: 1em 1em 3em;
  width: calc(100% - 2em);
  padding: 1em;
  border: 1px solid #ccc;
  border-radius: 2px;
  box-sizing: border-box;
}

#facet-list,
#container {
  max-width: 150px;
  width: 100%;
  float: left;
  padding: 0 1em;
  margin: 0;
  list-style: none;
}

#facet-list ul {
  padding-left: 0;
}

#facet-list li {
  padding: 6px 6px;
  border-bottom: 1px solid #ccc;
  list-style-type:none;
  margin-left: 0;
}

#facet-list a.refined {
  color: white;
  background: #ccc;
  display:block;
}

#facet-list li input {
  margin-right: 6px;
}

#container {
  max-width: calc(100% - 150px - 4em);
}

#container .hit {
  padding: 8px 0;
}

#container em {
  background: yellow;
  font-style: normal;
}
              
            
!

JS

              
                // Algolia client. Mandatory to instantiate the Helper.
var algolia = algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76');

// Algolia Helper
var helper = algoliasearchHelper(algolia, 'instant_search', {
  // Disjunctive facets need to be explicitly defined here
  // It's not the same attribute that is used to configure conjunctive and excluding facets
  disjunctiveFacets: [ 'manufacturer'],
  hierarchicalFacets: [{
    name: 'categories',
    attributes: ['hierarchicalCategories.lvl0', 'hierarchicalCategories.lvl1']
  }],
  // Misc. configuration for the demo purpose 
  hitsPerPage: 5,
  maxValuesPerFacet: 5
});

// Bind the result event to a function that will update the results
helper.on("result", searchCallback);

// The different parts of the UI that we want to use in this example
var $hits = $('#hits');
var $facets = $('#facets');

$facets.on('click', handleFacetClick);

// Trigger a first search, so that we have a page with results
// from the start.
helper.search();

// Result event callback
function searchCallback(results) {
  if (results.hits.length === 0) {
    // If there is no result we display a friendly message
    // instead of an empty page.
    $hits.empty().html("No results :(");
    return;
  }

	// Hits/results rendering
  renderHits($hits, results);
  renderFacets($facets, results);
}


function renderFacets($facets, results) {
  console.log(results); 

  var facets = $.map(results.hierarchicalFacets, function(f) {
    var name = f.name;
    var header = '<h4>' + name + '</h4>';
    var facetValues = results.getFacetValues(name);
    // console.log(facetValues);
    console.log(facetValues.data[0]);
    
    var facetsValuesList = renderFacetValues(name, facetValues.data);
    return header + facetsValuesList;
  });
  $facets.html(facets.join(''));
}

function renderFacetValues(name, facetValues) {
  if(!facetValues) return '';
  
  var facetsValuesList = facetValues.map(function(facetValue) {
    var facetValueClass = facetValue.isRefined ? 'refined'  : '';
    var valueAndCount = '<a data-attribute="' +name + '" data-value="' + facetValue.path + '" href="#" class=' + facetValueClass + '>' + facetValue.name + ' (' + facetValue.count + ')' + '</a>';
    var subList = renderFacetValues(name, facetValue.data);
    return '<li>' + valueAndCount + subList + '</li>';
  });

  return '<ul>' + facetsValuesList.join('') + '</ul>';
}

function handleFacetClick(e) {
  e.preventDefault();
  var target = e.target;
  var attribute = target.dataset.attribute;
  var value = target.dataset.value;
  // Because we are listening in the parent, the user might click where there is no data
  if(!attribute || !value) return;
  // The toggleRefine method works for disjunctive facets as well
  helper.toggleRefine(attribute, value).search();
}

function renderHits($hits, results) {
  var hits = results.hits.map(function renderHit(hit) {
    var highlighted = hit._highlightResult;
    var attributes = $.map(highlighted, function renderAttributes(attribute, name) {
      return (
        '<div class="attribute">' +
        '<strong>' + name + ': </strong>' + attribute.value +
        '</div>');
    }).join('');
    return '<div class="hit panel">' + attributes + '</div>';
  });
  $hits.html(hits);
}
              
            
!
999px

Console