npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

geta-product-filter

v0.1.6

Published

A JavaScript plugin that connects to your API and renders a product filter with custom HTML.

Downloads

11

Readme

Geta product filter

A JavaScript plugin that connects to your API and renders a product filter with custom HTML.

Use this plugin if you have a server-side api that returns search facets and products. Works with "any" json-api; the plugin itself doesn't expect the response to be in any particular format.

Don't use this plugin if you want't to use client-side filtering without a server-side api.



Getting started

Install from NPM

npm install geta-product-filter --save

Include CSS and JavaScript

<link rel="stylesheet" href="node_modules/geta-product-filter/dist/index.css">
<script src="node_modules/geta-product-filter/dist/index.js"></script>

Write a HTML template

Use Handlebars to render data from an api

<div data-filter class="filter">
    <script data-filter-template type="text/x-handlebars-template">
        <div class="filter-form-counter">
			{{response.products.length}} products
		</div>
    </script>
</div>

See an example of a template that renders html from a json response.

See a complete list of html data-attributes that should be included in the template.

Initialize the JavaScript

var element = document.querySelector('[data-filter]');
var options = {};
var filter = new window.Filter(element, options);

Options

url

string, required

An api endpoint that returns json with form facets and a list of products

ajaxOptions

object, optional

Options to be passed to jQuery Ajax

initialResponse

object, optional

An object with a response from the api. Use this to avoid having to call the api asynchronously after the JavaScript has initialized. Improves performance and SEO.

Alternatively, use server-side-rendering.

transformRequest

function, optional

A function that converts parameters before they are sent back to the api.

transformUrl

function, optional

A function that converts parameters before they are displayed in the url

transformTemplateData

function, optional

Transform the json from the api before rendering the template.

rendered

function, optional

A callback that runs every time the html template has rendered.

Use this for accessing the dom.

new window.Filter(element, {
    ...
    rendered: function(element){
        console.log(element); // An element that contains the rendered template html
    }
});

loaderClass

string, optional

A css class that will be added to the container element whenever an ajax request is in progress.

Default: filter--loading

Methods

navigate(Object)

Performs a new search and updates the url an ui.

Useful for updating the url and ui manually, e.g. when clicking an external button.

Example 1: request all red products

var filter = new window.Filter(element, options);
filter.navigate({color: 'red'});

Example 2: request all red and blue products that belong to My Brand

var filter = new window.Filter(element, options);
filter.navigate({color: ['red', 'blue'], brand: 'My Brand'})