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

dropdown-selectbox

v0.0.12

Published

A simple dropdown/selectbox

Downloads

7

Readme

dropDown.js

install

npm install  dropdown-selectbox

use

var Dropdown = require('dropdown-selectbox');

if not using browerify/webpack simply copy dropdown.js and dropdown.css.

See the examples in example folder.

create a dropdown

new Dropdown({
  mainText: 'Select Item',
  field: $('.mydropdown1'),
  list: [
    {name: 'Item1', id: 1},
    {name: 'Item2', id: 2}
  ],
  onSelect: function($item, $dropdown) {
    var id =  $item.data('id');
    console.log(id, $dropdown);
  }
});

id are optional. If passed will be added as data-id to each list-item.

html generated

<div class="dropdown">
  <div class="dropdown__selected-wrapper">
    <div class="dropdown__selected">Item1</div>
    <div class="dropdown__arrow"></div>
  </div>
  
  <div class="dropdown__list-wrapper">
    <div class="dropdown__list">
      <div class="dropdown__item">Item1</div>
      <div class="dropdown__item">Item2</div>
    </div>
  </div>  
</div>  

Css

Copy the css from dropdown.css file .

Classes
  • is-active - applied to dropdown, if dropdown is open.
  • is-disabled - applied to dropdown, if the list provided is an empty array.
  • item-selected - applied to dropdown__item, if item is selected.
  • scrolled - applied to dropdown__item, if item s currently being navigated using keys.

load from a url

 new Dropdown({
    mainText: 'Select Item',
    field: $('.mydropdown1'),
    url: '/list',
    parse: function(data) {
      // use this function to specify data to be used
      // good for updating main text with selected item
      return   {list: data.items, selected: data.items[0]};
    },
    onSelect: function($item, $dropdown) {
      console.log($item, $dropdown);
    }
  });

Use it as SelectBox [Options]

  • type : 'selectBox', it works as select box.Supports key navigation and search.
  • scrollOn: 200, no of pixels to start scrolling for key down nav.

For custom selected and item html

selTpl: {tpl:  '<span class="name"> selected item </span>', _class: 'name'},
itmTpl: {tpl:  '<span class="name"> item </span>', _class: 'name'},

The selTpl will be inserted inside <div class="dropdown__selected"></div>

The itmTpl will be inserted inside <div class="dropdown__item"></div>

Add items dynamically

var dropdown = new Dropdown({
  mainText: 'Select Item',
  field: $('.mydropdown1')
});

dropdown.addItems(
    list: [
     {name: 'Item1', id: 1},
     {name: 'Item2': id: 2}
    ]
);

Update selected item text

dropdown.updateSelected({name: 'Item1', id: 1}, true);

pass true if the item is present in the list

Add items at the top

dropdown.prependItems(
  list[
    {name: 'Item1', id: 1}
  ]
});

Add custom elements

dropdown.addCustom('<div class="datepicker">My datepicker</div>');

Options:

  • noDataText: 'Not available', applies is-disabled class if list is empty.
  • selected: {name: 'Item1', 'id': 1}, replaces the mainText if id is found in the list.
  • nameAttr: 'display_name', use this if you have other than name in your json.
  • idAttr: '_id', use this if you have other than id in your json.

Api

  • dropdown.open() -- opens the dropdown/selectBox
  • dropdown.close() -- closes the dropdown/selectBox
  • dropdown.closeAll() -- closes all the dropdowns/ selectBox
  • dropdown.disabled() -- adds disabled class to the dropdowns
  • dropdown.removeItems() -- emptys the list