Fork me on GitHub

ui-choices

angularjs binding for bootstrap-style button group

Abstract

This angular module 'ui-choices' helps you build up button groups in simple direcitves. It follows bootstrap 3.0 button css class naming, so you can directly use bootstrap.css to style your buttons. It is semantic-ui-friendly, too. ui-choices also provides a css file ( compiled from a sass file ) that can be used independently to any other frameworks. Source code can be found at http://github.com/zbryikt/ui-choices/. Here shows some examples about how to use ui-choices in various ways.



0. Preparation

Include angular.js, ui-choices.js and bootstrap.css ( or ui-choices.css as an alternative ):

<head>
  ...
  <link rel="stylesheet" type="text/css" href="your-path-to/ui-choices.css"/>
  <script type="text/javascript" src="your-path-to/angular.js">60;script>
  <script type="text/javascript" src="your-path-to/ui-choices.js">60;script>
  ...
<head>


1. Single Toggle Button

I love Fruit
Button Toggled?:
{{fruit1}}
Source Code
<toggle class="btn-success" ng-model="fruit1"/>


2. Basic Button Group - Single Choice

Banana Graphfruit Pitaya
Your Choices:
{{fruit2}}
Source Code
<choices ng-model="fruit2">
    <choice value="banana"> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


3. Multiple Choice

Banana Graphfruit Pitaya
Your Choice:
{{fruit3}}
Source Code
<choices multi ng-model="fruit3">
    <choice value="banana"> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


4. Multiple Choice with Default Values

Banana Graphfruit Pitaya
Your Choice:
{{fruit4}}
Source Code
<choices multi ng-model="fruit4">
    <choice value="banana" active> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


5. Multiple Choice with Fallback Value

Banana Graphfruit Pitaya
Your Choice:
{{fruit5}}
Source Code
<choices multi ng-model="fruit5">
    <choice value="banana" fallback active> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


6. Change Button Style

Banana Graphfruit Pitaya
Your Choice:
{{fruit6}}
Source Code
<choices multi btn-type="btn-danger" ng-model="fruit6">
    <choice value="banana" fallback active btn-type="btn-success"> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


7. Use Array Instead of Hash

Banana Graphfruit Pitaya
Your Choice:
{{fruit7}}
Source Code
<choices multi data-type="array" btn-type="btn-danger" ng-model="fruit7">
    <choice value="banana" fallback active btn-type="btn-success"> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


7.1 Use Single Value in Single Choice

Banana Graphfruit Pitaya
Your Choice:
{{fruit71}}
Source Code
<choices data-type="single" btn-type="btn-danger" ng-model="fruit71">
    <choice value="banana" fallback active btn-type="btn-success"> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


8. Customize Button Content

Banana Graphfruit Pitaya
Your Choice:
{{fruit8}}
Source Code
<choices ng-model="fruit8">
    <choice value="banana" btn-type="btn-success">
      <i class="glyphicon glyphicon-ok">60;i> Banana
    <choice>
    <choice value="graphfruit" btn-type="btn-warning">
      <i class="glyphicon glyphicon-exclamation-sign">60;i> Graphfruit
    <choice>
    <choice value="pitaya" btn-type="btn-danger">
      <i class="glyphicon glyphicon-remove">60;i> Pitaya
    <choice>
<choices>


9. Binding on Single Button

Banana Graphfruit Pitaya
Your Choice:
{{fruit9}}
Is Banana Chosen?
{{isBananaChosen}}
Source Code
<choices ng-model="fruit9">
    <choice value="banana" btn-type="btn-success" ng-model="isBananaChosen">
      <i class="glyphicon glyphicon-ok">60;i> Banana
    <choice>
    <choice value="graphfruit" btn-type="btn-warning">
      <i class="glyphicon glyphicon-exclamation-sign">60;i> Graphfruit
    <choice>
    <choice value="pitaya" btn-type="btn-danger">
      <i class="glyphicon glyphicon-remove">60;i> Pitaya
    <choice>
<choices>


10. Styling: Questionnaire

1. Which one is your favorite fruit?
Banana Graphfruit Pitaya
2. Which one is your favorite food?
Sandwich Steak Hamburger
3. Are you a vegetarian?
Yes No
Your Choice:
{{fruit10}}
Source Code
<choices ng-model="fruit10.q1" btn-type="btn-list">
    <choice value="banana" fallback> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>
... (ignored)
Stylesheet
.btn-list {
  display: block;
  clear: both; background: none;
  border: none; color: #000;
  box-shadow: none!important;
}
.btn-list:before {
  display: inline-block;
  width: 13px; height: 13px;
  border-radius: 7px;
  border: 1px solid rgba(0,0,0,0.5);
  box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
  margin: 2px 3px 0 0;
  veretical-align: text-top;
  content: " ";
}
.btn-list.active { box-shadow: none; }
.btn-list.active:before { background: #092; }
( * the 'btn-list' class is included in ui-choices.css )


11. With 'ng-repeat'

{{d.name}}
Your Choices:
{{fruit11.value}}
Source Code
<choices ng-model="fruit11.value" btn-type="btn-default" multi="fruit11.multi">
  <choice ng-repeat="d in fruit11.data" ng-data="d"> {{d.name}} <choice>
<choices>
Content of fruit11.data
... (ignored)
$scope.fruit11 = {
  multi: true,
  value: {},
  data: [
    { name: "Banana", value: 1, btntype: 'btn-primary' },
    { name: "Graphfruit", value: 2 },
    { name: "Pitaya", value: 3, active: true }
]}


12. With 'ng-repeat', both choices and choice

{{ c.name }}
{{d.name}}

Your Choices:
{{fruit12[0].value}}
{{fruit12[1].value}}
{{fruit12[2].value}}
Source Code
<choices ng-repeat="c in fruit12" ng-model="c.value" btn-type="btn-list" multi="c.multi">
  <div style="clear: both"> {{ c.name }}
  <choice ng-repeat="d in c.data" ng-data="d"> {{d.name}} <choice>
<choices>
Content of fruit12
$scope.fruit12 = [
  { name: "breakfast?", multi: true, value: {}, data: [
    { name: "Banana", value: 1, fallback: true },
    { name: "Graphfruit", value: 2 },
    { name: "Pitaya", value: 3, active: true }
  ]},
  ... (ignored)
]


13. Single Toggle Button ( with Semantic-ui )

I love Fruit
Button Toggled?:
{{fruit13}}
Source Code
<toggle class="ui button" ng-model="fruit13"/>


14. Basic Button Group - Single Choice with Default Value ( with Semantic-ui )

Banana Graphfruit Pitaya
Your Choices:
{{fruit14}}
Source Code
<choices class="ui buttons" ng-model="fruit14" btn-type="ui button">
    <choice value="banana" active> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>


15. Multiple Choice with Fallback Value and Styling ( with Semantic-ui )

Banana Graphfruit Pitaya
Your Choice:
{{fruit15}}
Source Code
<choices class="ui buttons" multi ng-model="fruit15" btn-type="ui button">
    <choice class="green" value="banana" fallback active> Banana <choice>
    <choice value="graphfruit"> Graphfruit <choice>
    <choice value="pitaya"> Pitaya <choice>
<choices>