基于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实现浏览器窗口传递参数的方法
Sep 03 Javascript
AngularJS中取消对HTML片段转义的方法例子
Jan 04 Javascript
Jsonp 关键字详解及json和jsonp的区别,ajax和jsonp的区别
Dec 30 Javascript
AngularJS入门教程之AngularJS模型
Apr 18 Javascript
基于jQuery实现一个marquee无缝滚动的插件
Mar 09 Javascript
JavaScript设计模式之策略模式详解
Jun 09 Javascript
js学习总结_轮播图之渐隐渐现版(实例讲解)
Jul 17 Javascript
详解Webpack如何引入CDN链接来优化编译后的体积
Jun 21 Javascript
使用VUE实现在table中文字信息超过5个隐藏鼠标移到时弹窗显示全部
Sep 16 Javascript
在vue中根据光标的显示与消失实现下拉列表
Sep 29 Javascript
js实现简单掷骰子小游戏
Oct 24 Javascript
微信小程序 wx.getUserInfo引导用户授权问题实例分析
Mar 09 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
神族 Protoss 历史背景
2020/03/14 星际争霸
2014最热门的24个php类库汇总
2014/12/18 PHP
php实现的debug log日志操作类实例
2016/07/12 PHP
Div自动滚动到末尾的代码
2008/10/26 Javascript
js面向对象设计用{}好还是function(){}好(构造函数)
2011/10/23 Javascript
基于jQuery的获取标签名的代码
2012/07/16 Javascript
THREE.JS入门教程(3)着色器-下
2013/01/24 Javascript
js中parseInt函数浅谈
2013/07/31 Javascript
javascript闭包传参和事件的循环绑定示例探讨
2014/04/17 Javascript
使用百度地图api实现根据地址查询经纬度
2014/12/11 Javascript
jQuery实现多级下拉菜单jDropMenu的方法
2015/08/28 Javascript
js判断某个字符出现的次数的简单实例
2016/06/03 Javascript
浅谈js构造函数的方法与原型prototype
2016/07/04 Javascript
Bootstrap零基础入门教程(三)
2016/07/18 Javascript
jQuery中$.ajax()方法参数解析
2016/10/22 Javascript
解决BootStrap Fileinput手机图片上传显示旋转问题
2017/06/01 Javascript
详解如何从零开始搭建Express+Vue开发环境
2018/07/17 Javascript
mpvue小程序仿qq左滑置顶删除组件
2018/08/03 Javascript
node express使用HTML模板的方法示例
2019/08/22 Javascript
微信小程序 轮播图实现原理及优化详解
2019/09/29 Javascript
[02:12]2015国际邀请赛 SHOWOPEN
2015/08/05 DOTA
Python类的动态修改的实例方法
2017/03/24 Python
利用python打开摄像头及颜色检测方法
2018/08/03 Python
python实现IOU计算案例
2020/04/12 Python
利用OpenCV中对图像数据进行64F和8U转换的方式
2020/06/03 Python
keras读取h5文件load_weights、load代码操作
2020/06/12 Python
Matplotlib 折线图plot()所有用法详解
2020/07/28 Python
python 8种必备的gui库
2020/08/27 Python
银行会计财务工作个人的自我评价
2013/10/29 职场文书
党员一句话承诺大全
2014/03/28 职场文书
政治表现评语
2014/05/04 职场文书
会员卡清退活动总结
2014/08/27 职场文书
夫妻房产协议书的格式
2014/10/11 职场文书
敲诈同学钱财检讨书范文
2014/11/18 职场文书
升职感谢信
2015/01/22 职场文书
2015年打非治违工作总结
2015/04/02 职场文书