基于angular实现三级联动的生日插件


Posted in Javascript onMay 12, 2017

写了一个生日联动插件具体的效果是这样的:

具体的数据

我取得数据是今年的数据,如果是想要做三级联动的日期插件,改一下时间就好了

var app=angular.module("dataPicker",[])

app.factory('dataPicker', ['$http', '$q', function ($http, $q) {
  return {
   query: function () {
    var lengthYear=100;
    var dataPicker={
     month:[],
     year:[],
     day:[]
    };
    var data = new Date();
    var nowyear = data.getFullYear();
    for(var i=nowyear,j=0; i>nowyear-lengthYear;i--,j++){
     dataPicker.year[j]=i;
    }
    for(var i=0;i<=11;i++){
     if(i<9){
      dataPicker.month[i]='0'+(i+1);
     }else{
      dataPicker.month[i]=String(i+1);
     }
    }
    return dataPicker;
   }
  }
 }])

directive插件的主要内容

app.directive('selectDatepicker', function ($http,dataPicker) {
  return {
   restrict: 'EAMC',
   replace: false,
   scope: {
    birthday: '=birthday'
   },
   transclude: true,
   template: '<span>生日</span>'+
    '<select class="sel_year" ng-model="birY" ng-change="changeYear()"><option ng-repeat="x in yearAll">{{x}}</option></select>'+
    '<select class="sel_month" ng-model="birM" ng-change="changeMonth()" ng-disabled="birY==\'\'"><option ng-repeat="x in MonthAll">{{x}}</option> </select>'+
    '<select class="sel_day" ng-model="birD" ng-disabled="birM==\'\'" ng-change="changeDay()"><option ng-repeat="x in DayAll">{{x}}</option></select>',
   link: function (scope, element){
    var arr=[];
    scope.birthday=scope.birthday=='0000-00-00'?"":scope.birthday
    var shuju=dataPicker.query()
    scope.yearAll=shuju.year;
    scope.MonthAll=shuju.month;
    if(scope.birthday){
     scope.birY=scope.birthday.birthday.split('-')[0];
     scope.birM=String(scope.birthday.birthday.split('-')[1])
    }else{
     scope.birY="";
     scope.birM="";
    }
    scope.getDaysInOneMonth=function(year, month){
     var month1 = Number(month);
     month1=parseInt(month1,10)
     var d= new Date(Number(year),month1,0);
     return d.getDate();
    }
    scope.getDayArr=function(day){
     shuju.day=[];
     for(var i=0; i<day;i++){
      if(i<9){
       shuju.day[i]='0'+(i+1)
      }else{
       shuju.day[i]=String((i+1));
      }
     }
    }
    if(scope.birthday){
     var day=scope.getDaysInOneMonth(scope.birthday.birthday.split('-')[0],scope.birthday.birthday.split('-')[1]);
     scope.getDayArr(day)
     scope.DayAll=shuju.day;
     scope.birD=scope.birthday.birthday.split('-')[2]
    }
    scope.changeYear=function(){
     scope.birD="";
     scope.birM="";
    }
    scope.changeMonth=function(){
     var day=scope.getDaysInOneMonth(scope.birY,scope.birM);
     console.log(day)
     scope.getDayArr(day);
     scope.DayAll=shuju.day;
     scope.birD="";
    }
    scope.changeDay=function(){
     scope.returnDate();
    }
    scope.returnDate=function(){
     if(!scope.birD||!scope.birY||!scope.birM){
      scope.birthday.returnValue="";
     }else{
      arr[0]=scope.birY;
      arr[1]=scope.birM;
      arr[2]=scope.birD;
      scope.birthday.returnValue=arr.join("-");
     }
    }
   }
  }
 })
});

 html

<div select-datepicker birthday="birthday"> 

js 传入的数据

$scope.birthday={
   birthday:1993-01-20,
   returnValue:'',
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript RegExp方法获取地址栏参数(面向对象)
Mar 10 Javascript
利用jQuery插件扩展识别浏览器内核与外壳的类型和版本的实现代码
Oct 22 Javascript
jquery 定位input元素的几种方法小结
Jul 28 Javascript
JS操作Cookies的小例子
Oct 15 Javascript
jQuery中each()方法用法实例
Dec 27 Javascript
JS实现自动变换的菜单效果代码
Sep 09 Javascript
js实现可键盘控制的简单抽奖程序
Jul 13 Javascript
JS实现的DIV块来回滚动效果示例
Feb 07 Javascript
vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
Nov 28 Javascript
引入外部js脚本加载慢与页面白屏问题的解决
Dec 10 Javascript
超简单的微信小程序轮播图
Nov 22 Javascript
在项目vue中使用echarts的操作步骤
Sep 07 Javascript
vue.js中mint-ui框架的使用方法
May 12 #Javascript
js图片加载效果实例代码(延迟加载+瀑布流加载)
May 12 #Javascript
微信小程序之数据双向绑定与数据操作
May 12 #Javascript
Flask中获取小程序Request数据的两种方法
May 12 #Javascript
关于bootstrap日期转化,bootstrap-editable的简单使用,bootstrap-fileinput的使用详解
May 12 #Javascript
微信小程序 支付功能实现PHP实例详解
May 12 #Javascript
深入理解JavaScript继承的多种方式和优缺点
May 12 #Javascript
You might like
APMServ使用说明
2006/10/23 PHP
php sprintf()函数让你的sql操作更安全
2008/07/23 PHP
封装一个PDO数据库操作类代码
2009/09/09 PHP
Apache中php.ini的设置方法
2013/02/28 PHP
老生常谈PHP 文件写入和读取(必看篇)
2017/05/22 PHP
PHP实时统计中文字数和区别
2019/02/28 PHP
PHP生成指定范围内的N个不重复的随机数
2019/03/18 PHP
js中几种去掉字串左右空格的方法
2006/12/25 Javascript
键盘 keycode的值 javascript时触发事件时很有用的要素
2009/11/02 Javascript
国外大牛IE版本检测!现在IE都到9了,IE检测代码
2012/01/04 Javascript
一样的table?不一样的table(可编辑状态table)
2012/09/19 Javascript
常见的jQuery选择器汇总
2014/11/24 Javascript
NodeJS Web应用监听sock文件实例
2015/02/18 NodeJs
WordPress中利用AJAX技术进行评论提交的实现示例
2016/01/12 Javascript
node.js发送邮件email的方法详解
2017/01/06 Javascript
基于canvas的二维码邀请函生成插件
2017/02/14 Javascript
JavaScript制作简单的框选图表
2017/05/15 Javascript
Vue2.0父子组件传递函数的教程详解
2017/10/16 Javascript
详解Vue文档中几个易忽视部分的剖析
2018/03/24 Javascript
axios如何取消重复无用的请求详解
2019/12/15 Javascript
js实现菜单跳转效果
2020/12/11 Javascript
[14:00]DOTA2国际邀请赛史上最长大战 赛后专访B神
2013/08/10 DOTA
[50:48]LGD vs CHAOS 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
利用python获得时间的实例说明
2013/03/25 Python
Python实现新浪博客备份的方法
2016/04/27 Python
pytorch1.0中torch.nn.Conv2d用法详解
2020/01/10 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
2020/02/07 Python
python实现人机五子棋
2020/03/25 Python
公务员年总结的自我评价
2013/10/25 职场文书
新闻专业大学生找工作的自我评价
2013/10/30 职场文书
影视艺术学院毕业生自荐信
2013/11/13 职场文书
广告传媒专业应届生求职信
2014/03/01 职场文书
音乐教育感言
2014/03/05 职场文书
家长会演讲稿
2014/04/26 职场文书
教师党员岗位承诺书
2014/05/29 职场文书
MySQL 隔离数据列和前缀索引的使用总结
2021/05/14 MySQL