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

three-state-checkbox

v1.3.0

Published

jQuery plugin, which helps selecting and de-selecting group of checkboxes and enabling the action button based on the selection

Downloads

6

Readme

#jQuery Three State Checkbox Plugin

Demo

jQuery Three State Checkbox Plugin Demo

Usage instructions

List / table structure

To start with create an html structure of you list with one column (in case of the table) containing checkbox for the given record:

<form>

    <table>

        <thead>

            <tr>
                <th class="col1">
                    <input
                        type="checkbox"
                        class="checkboxMaster"
                        data-group-master="checkboxRemove"
                        >
                </th>
                <th>
                    Name
                </th>
            </tr>

        </thead>

        <tbody>

            <tr>
                <td>
                    <input
                        type="checkbox"
                        class="checkboxRecord"
                        data-group-record="checkboxRemove"
                        data-id="1"
                        >
                </td>
                <td>
                    Name 1
                </td>
            </tr>
            <tr>
                <td>
                    <input
                        type="checkbox"
                        class="checkboxRecord"
                        data-group-record="checkboxRemove"
                        data-id="2"
                        >
                </td>
                <td>
                    Name 2
                </td>
            </tr>
            <tr>
                <td>
                    <input
                        type="checkbox"
                        class="checkboxRecord"
                        data-group-record="checkboxRemove"
                        data-id="3"
                        >
                </td>
                <td>
                    Name 3
                </td>
            </tr>

        </tbody>

    </table>

</form>

The important things here are:

  • The main checkbox in the heading of the table, which selects / de-selects all of the child checkboxes, needs to have some class, that we either pass as an argument when we call the plugin or use the default one, which is 'checkboxMaster'. It also needs to have a data-* attribute, the default one is 'data-group-master', but again, you can overwrite it with the argument when calling the plugin.

  • Each child checkbox also needs to have the class and data-* attribute. The default class is 'checkboxRecord' and default data-* attribute is 'data-group-record'.

  • The 'data-group-master' and 'data-group-record' have to contain the same value, which is used to identify the group (corresponding items). If there is more than one group of checkboxes i.e. more than one table with master checkbox and its children - make sure that the group name is different for each group. (The above example only shows one table, with one group)

Button

We can also add a button, which will be disabled by default and becomes enabled when at least one checkbox is checked. I'm using Zurb Foundation css, hence the classes associated with the button, but you can use any styling and element - including input or button. The important thing here is to give it a 'data-group-buttton' attribute (or your custom defined one) and set is value to the corresponding group - with the above example it would be 'checkboxRemove'.

We also add the class 'checkboxButton', which will be used as a trigger identifier (this class name can be overwritten - see customisations below).

<a
    href="#"
    class="tiny alert button disabled checkboxButton"
    data-group-button="checkboxRemove"
    >REMOVE</a>

Plugin initiation

To initiate the plugin use the following method:

$(document).ssdThreeStateCheckbox();

Make sure that you have the latest version of jQuery and plugin file included:

<script src="js/jquery-2.1.1.min.js"></script>
<script src="js/jquery.ssdThreeStateCheckbox.min.js"></script>
<script>
    $(function() {
        $(document).ssdThreeStateCheckbox();
    });
</script>

Button click callback

When you click the button after making your selection, plugin will collect the ids matching selected checkboxes and will pass them as an array to the callback method clickCallback() together with the instance of the button. You can specify what you wish to do with that data by overwriting this method. In the following example I've added data-url to our Remove button with the url to which I wish the selected ids to be posted using $.getJSON call.

<a
    href="#"
    class="tiny alert button disabled checkboxButton"
    data-group-button="checkboxRemove"
    data-url="remove.php"
    >REMOVE</a>
<script>
$(document).ssdThreeStateCheckbox({

    clickCallback : function(ids, trigger) {

        $.getJSON(trigger.data('url'), { collection : ids }, function(data) {

            // do something with your returned data

        });

    }

});

Custom class and data-* attributes

If you've used different than default data-* or class attributes you can pass them to the plugin using the following arguments:

$(document).ssdThreeStateCheckbox({

    classButton         : 'myCustomButtonClass',
    classMaster         : 'myCustomMasterClass',
    classRecord         : 'myCustomRecordClass',

    dataGroupButton     : 'group-custom-button',
    dataGroupMaster     : 'group-custom-master',
    dataGroupRecord     : 'group-custom-record',
    dataGroupRecordId   : 'row'

});

The above would result in our master input to be called as:

<input
    type="checkbox"
    class="myCustomMasterClass"
    data-group-custom-master="checkboxRemove"
    >

The child input as:

<input
    type="checkbox"
    class="myCustomRecordClass"
    data-group-custom-record="checkboxRemove"
    data-row="1"
    >

And the button as:

<a
    href="#"
    class="tiny alert button disabled myCustomButtonClass"
    data-group-custom-button="checkboxRemove"
    >REMOVE</a>