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

ng-checkbox

v1.0.2

Published

The component provides selection data and its statistics based on checkboxes selection.

Downloads

2

Readme

NPM

What is ng-checkbox?

A checkbox Angular directive provides user selected data and its statistics. Any contributions are welcomed!

Demo

Installation

npm install --save ng-checkbox

Include the file in your html

<script src="node_modules/ng-checkbox/dist/ngCheckbox.js"></script>

Add ngCheckbox to your module's dependencies

angular.module('MyApp', ['ngCheckbox']);

Sample Usage

Inject the ngCheckboxStatistics factory into your controllers. Add callback(s) to ngCheckboxStatistics

angular.module('MyApp')
.controller('myController', function($scope, ngCheckboxStatistics) {
    var onCheckboxClick = function(stats){
        console.log('Stats object contain all the information everytime you select/unselect a checkbox: ',stats);
    }
    ngCheckboxStatistics.addListener(onCheckboxClick);
});

Use it in your templates, there are many variations depending on your needs (also check the Documentation, and Demo, when use right, it can be powerful), here is one small workable simple example:

        <ng-checkbox groups="['luxury','fast']" count="2" sum="200000" unit="USD">Car A</ng-checkbox>
        <ng-checkbox groups="['cheap','fast']" count="2" sum="200" unit="USD">Car B</ng-checkbox>
  ...

By default, if ng-model attribute is not defined, checkbox will have a value of true (the checkbox is checked). When the controller is first instantiated, onCheckboxClick will be invoked by ngCheckboxStatistics, and stats object will have a value as following:

{
    count: 4 // total count. if unset, default count is 1
    groups: {
        cheap:{
            count:2
            sumByUnits: [
                {
                    sum:400,
                    unit:'USD'
                }
            ]
        },
        fast:{
            count:4
            sumByUnits: [
                {
                    sum:400400,
                    unit:'USD'
                }
            ]
        },
        luxury:{
            count:2
            sumByUnits: [
                {
                    sum:400000,
                    unit:'USD'
                }
            ]
        },
    },
    selectedId: undefined,
    selectedIds: ['ngCb_luxury_fast_19','ngCb_cheap_fast_20'] // selected checkbox ids. The ids are autogenerated if id attribute is not defined
    sumByUnits: [
        {
            sum:400400,
            unit:'USD'
        }
    ],
    superGroup: "default", // 'default' if super-group attribute is not set
    unselectedId: undefined
}

If however you decided to later have a superGroup for each checkbox, e.g something like this:

        <ng-checkbox super-group='Car' groups="['luxury','fast']" count="2" sum="200000" unit="USD">Car A</ng-checkbox>
        <ng-checkbox super-group='Car' groups="['cheap','fast']" count="2" sum="200" unit="USD">Car B</ng-checkbox>
        
        <ng-checkbox super-group='Bike' groups="['luxury','fast']" count="2" sum="1000" unit="USD">Bike A</ng-checkbox>
        <ng-checkbox super-group='Bike' groups="['cheap','fast']" count="2" sum="10" unit="USD">Bike B</ng-checkbox>
  ...

Then you have to add listeners for each superGroup to be able to receive the statistics back (see more documentation for ngCheckboxStatistics.addListener)

ngCheckboxStatistics.addListener(onCheckboxClick, null,'Car'); // This will only be invoked whenever the user clicks on 'Car' checkbox(es)
ngCheckboxStatistics.addListener(this.onCheckboxClick2, this, 'Bike'); // if you pass in this.onCheckboxClick2 instead of just onCheckboxClick2, you must pass in the 'this' context object in the second parameter as well

Documentation

ng-checkbox directive attributes (All are optional)

  • id (string, default: autogenerated id) - unique id for checkbox. Shown in 'selectedId' if checkbox is selected and 'unselectedId' if checkbox is unselected.
  • super-group (string, default: 'defaut') - super-group that a checkbox belongs to
  • groups (array, default: []) - group(s) that a checkbox belongs to
  • head (boolean, default: false) - if checkbox has a head="true" then whenever it is clicked, other checkboxes under the same group(s) will have the same value with it
  • ngModel (boolean, default: true) - value of checkbox. True if checkbox is checked and vice versa.
  • sum (array/number, default: []) - checkbox sum value (the final sum, however, will be count*sum)
  • count (number, default: 1) - checkbox count value
  • unit (string, default: '') - unit for checkbox sum value

ngCheckboxStatistics

  • ngCheckboxStatistics.addListener(callback,[thisArg,superGroupName]) - add a callback to listen to checkboxes selection in a super-group. So if you ngCheckboxStatistics.addListener(myCallback,null,"mySuperGroup"), myCallback will be invoked whenever a checkbox with super-group="mySuperGroup" is selected/unselected.

callback _(function) thisArg _(object,optional,default: null) _ callback will use thisArg as a reference for its context superGroupName _(string,optional,default: 'default')

  • ngCheckboxStatistics.reset() - reset all checkboxes' states but not the callbacks which has been added by addListener.

  • ngCheckboxStatistics.resetHard() - reset all checkboxes' states,and callbacks which has been added by addListener, usually called in controller 's $destroy event.

Want to contribute?

Anyone can help make this project better, fork it, do your changes and make a pull request! Also, see TODO file for some of my notes on what could be improved.