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

catcade

v1.0.1

Published

Dynamic cascading category selection for jquery and select2

Downloads

5

Readme

Catcade - Cascading Category Selection

npm version

Installation

This is a jQuery plugin, so be sure to include jQuery as your project dependency module or include in the pages using this plugin. This plugin also only uses select2, for now. support for pure HTML and vanilla javascript is in the roadmap. For now, to use this plugin, simply execute npm i catcade against your project or search for catcade in jsDelvr and use:

<script src="////cdn.jsdelivr.net/npm/[email protected]/dist/catcade.min.js"></script>

to include catcade to your project.

Usage

This is a jQuery plugin. Like any other jQuery plugin, you can initialize a cascading category selection by invoking catcade against a jQuery object.

$ = jQuery;
$('.selector-for-an-empty-div-or-span-as-container').catcade(options);
// or
$('.selector-for-an-empty-div-or-span-as-container').catcade('a-switch');

where options and switches may include any of the properties discussed later. It returns the jQuery object it has fired upon for chaining.

Options

Almost all options are (no pun intended) optional. Property | Type | Description | Default ---------|-------|-------------|-------- container|JQuery | A jquery container that overrides the object which catcade has fired upon. | The object which catcade has fired upon. label |string | The label which is used for each selection. Can be overriden by template. | "Category" live |boolean| Whether use select2's ajax option for firing live queries against url. | false maxLevel |number | The maximum level that cascading continues; the depth which is allowed to be delved in. 0 means unlimited depth | 0 name |string | The name used for the "LAST" level of selection which you can grab value for form submition | "category_id" url |string | PropertyFunction<string> | The URL used for fetching options in each level. You can provide dynamic URLs for each level by providing a function that returns the url for each specific level. | (parent, base = '') => base + defaultTo(parent,$(``<input value=""/>``)).val(), template |string | TemplateFunction | A template which the input selection will be created with. To modify each level's template, you can use a function that returns the HTML code to be inserted at each level. | a function

The template's default is as follows:

(self, parent) => {
    let result = ``;
    if (parent)
    {
        let current = parseInt(parent.data(`level`)) + 1;
        result = `
<div class="form-group">
<label class="col-md-3">${self.label} - ${current}</label>
<select class="form-control col-md-6" data-level="${current}" name="${self.name}"><option></option></select>
</div>
`;
    }
    else
    {
        result = `
<div class="form-group">
<label class="col-md-3">${self.label}</label>
<select class="form-control col-md-6" data-level="0" name="${self.name}"><option></option></select>
</div>
`;
    }
    return result;
}

Switches

There is currently two switches, but through course of time and per need as this package grows, they will grow through community. Switches are called like this:

$('container-selector').catcade('switch')

Switch | Returns | Action -------|---------|------- destroy | jQuery object that it fired upon (for chaining) | cleans the catcade on container object | The internal DynamicCategory object for manipulation per instance | nothing

Contributions

Feel free to open issues, pull-requests and engage in discussions. I'll appreciate it and do my best to keep adding and fixing.