Giter Club home page Giter Club logo

ng-pickadate's Introduction

ng-pickadate

bower version npm version HuBoard

AngularJS directives for pickadate.js.

http://toilal.github.io/ng-pickadate/

Requirements

Install

  1. Install dependency using bower

     bower install ng-pickadate --save
    
  2. Set overrides property in bower.json to register pickadate CSS files

  • Classic theme

        "overrides": {
          "pickadate": {
            "main": [
              "lib/picker.js",
              "lib/picker.date.js",
              "lib/picker.time.js",
              "lib/themes/classic.css",
              "lib/themes/classic.date.css",
              "lib/themes/classic.time.css"
            ]
          }
        }
    
  • Default theme

        "overrides": {
          "pickadate": {
            "main": [
              "lib/picker.js",
              "lib/picker.date.js",
              "lib/picker.time.js",
              "lib/themes/default.css",
              "lib/themes/default.date.css",
              "lib/themes/default.time.css"
            ]
          }
        }
    

Usage

  1. Declare the dependency

     angular.module('yourApp', ['pickadate']);
    
  2. Use pick-a-date and pick-a-time directives.

    <input type="text" pick-a-date="curDate"/>
    <input type="text" pick-a-time="curTime"/>
    $scope.curDate = new Date(); // Only the date part of curDate
                                 // is synced to pick-a-date directive
                                 
    $scope.curTime = new Date(); // Only the time part of timeDate
                                 // is synced to pick-a-time directive
  3. You can also provide additional max-date and min-date values.

     <input type="text" pick-a-date="startDate" max-date="endDate"/>
     <input type="text" pick-a-date="endDate" min-date="startDate"/>
    

Options

You can define pickadate.js options through pick-a-date-options and pick-a-time-options directives as well.

<input type="text" pick-a-date="curDate" pick-a-date-options="{ format: 'dd/mm/yy', selectYears: true }" />

If you find yourself setting the same options for multiple date pickers, you can set them as the default options for all date pickers by configuring pickADateProvider and pickATimeProvider.

angular.module('yourApp', ['pickadate'])
  .config(['pickADateProvider', 'pickATimeProvider', function (pickADateProvider, pickATimeProvider) {
    pickADateProvider.setOptions({
      format: 'dd/mm/yy',
      selectYears: true
    });

    pickATimeProvider.setOptions({
      today: ''
    });
  }]);

Form Validation

AngularJS form validation can be used using ngModel. Keep in mind that ngModel keeps the string value of the raw input.

<form name="dateForm">
  <input type="text" name="dateInput" ng-model="curDateText" pick-a-date="curDate"/>
  <div ng-show="dateForm.dateInput.$error.required" style="color: red;">
    <strong>Date is required.</strong>
  </div>
</form>

In order to correct how the pickadate.js affects ngModel states of its assigned input, ng-pickadate uses ngModelController to manually restore expected form validation states: $pristine, $dirty, $untouched, and $touched. The unexpected angular validation states caused by the pickadate.js jQuery plugin, and how they've been corrected, are as follows:

  • When pickadate is initialized on an input, this triggers the input's ngModel to be marked as $dirty. To correct this, the ng-pickadate directives each set ngModel to $pristine at the end of the postlink function.
  • When pickadate calendar opens, the input itself loses focus and its ngModel is marked as $touched. To correct this, the directives set ngModel to $untouched the first time the calendar opens, and set it to $touched whenever the calendar closes, via pickadate's onOpen and onClose methods, respectively.

Credits

This project is initially based on a blog post from Coding Insight

ng-pickadate's People

Contributors

toilal avatar jayellsbrg avatar djsmentya avatar gufo avatar wannymiarelli avatar soleno-n avatar

Watchers

James Cloos avatar Michał Gurbiel avatar

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.