Giter VIP home page Giter VIP logo

angular-custom-select's Introduction

What is angular-custom-select?

It's an AngularJS directive that builds easily styleable, functional dropdowns using divs, rather than the not-so-customizable select element. It supports ng-model and ng-options, as well as options to customize placeholder text, control disabled states, and specify the classes applied to dropdown elements. You can check out a demo here.

How can I get it?

Use Bower

bower install --save angular-custom-select

Then include the script in your app (after including Angular, of course).

Or download it from GitHub

In the /dist directory, you'll find angular-custom-select.min.js. Download that file and include it in your app (after Angular).

How do I use it?

Add angular-custom-select as a dependency for your app, like so:

angular.module('your-amazing-app', ['angular-custom-select'])

Then you can incorporate the <custom-select> element to your markup, using ng-model and ng-options as you normally would for a select element. At the moment, angular-custom-select can handle arrays of strings or objects, but it does not yet support iteration over objects. Below are some examples.

Keep in mind that the angular-custom-select directive imposes no styling, so the dropdown won't look much like a dropdown out of the box. You can take a peek at src/scss/example.scss to get inspiration for styling your own.

Array of Strings

# Controller
$scope.selectedValues = {}
$scope.strings = ['Option A', 'Option B', 'Option C', 'Option D', 'Option E'] 
<!-- View -->
<custom-select
	ng-model="selectedValues.string"
	ng-options="string for string in strings"
	placeholder="Select one of these string values...">
</custom-select>

...will produce the following markup and update the selectedValues.string model as expected:

<div class="custom-select" ng-class="{ 'expanded': expanded }">
  <div class="placeholder option">
    <span class="value">
      Select one of these string values...
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option A
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option B
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option C
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option D
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option E
    </span>
  </div>
</div>	

Array of Objects

# Controller
$scope.selectedValues = {}
$scope.objects = [
 	{ name: 'Option A' }
 	, { name: 'Option B' }
 	, { name: 'Option C' }
 	, { name: 'Option D' }
	, { name: 'Option E' }
]
<!-- View -->
<custom-select
	ng-model="selectedValues.object"
	ng-options="object as object.name for object in objects"
	placeholder="Select one of these object values...">
</custom-select> 

...will produce the following markup and update the selectedValues.object model as expected:

<div class="custom-select" ng-class="{ 'expanded': expanded }">
  <div class="placeholder option">
    <span class="value">
      Select one of these string values...
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option A
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option B
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option C
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option D
    </span>
  </div>
  <div class="option">
    <span class="value">
      Option E
    </span>
  </div>
</div>

Options

You can also configure a dropdown by adding attributes to the custom-select element:

  • select-class specifies the class applied to the outermost custom dropdown element, defaults to custom-select.
  • option-class specifies the class applied to each option element, defaults to option.
  • option-value-wrapper-class specifies the class applied to the value text element for each option element, defaults to value.
  • expanded-class specifies the class applied to the outermost custom dropdown element when the menu is open, defaults to expanded.
  • placeholder-class specifies the class applied to the default/placeholder option element, defaults to placeholder.
  • placeholder-label specifies the text of the default/placeholder option, defaults to Select a value....
  • disabled-attribute specifies the attribute to trigger a disabled state for options, only works for objects, defaults to null.
  • disabled-class specifies the class applied to disabled options, defaults to disabled.

Working with the Source

Installing Dependencies

After installing Ruby and Bundler, run bundle install from the project root to install the necessary Ruby gems.

After installing Node.js, NPM, and Gulp, run npm install from the project root to install the necessary NPM package dependencies.

After installing Bower, run bower install from the project root to install the necessary Bower component dependencies.

Compiling for Development

Run gulp to observe changes made to any .coffee files in the project, then respond by compiling/concatenating/copying the appropriate assets. The resulting files can be found in the build directory.

Compiling for Production

Run 'gulp dist' to compile angular-custom-select.min.js for production.

angular-custom-select's People

Contributors

nickcherry avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

angular-custom-select's Issues

Not accessible

Users cannot navigate the control using the keyboard.

Tracking by object id is not possible

With something like:

$scope.valueTypes = [ { id: 1, value: "Value 1" }, { id: 2, value: "Value 2" } ];

I can't do something like:

<custom-select ng-model="myModel" ng-options="type.id as type.value for type in valueTypes"></custom-select>

The model will have the right value but the select will stay blank or with the placeholder.

Is there any way to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.