最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

日期時(shí)間范圍選擇插件:daterangepicker使用總結(jié)(必看篇)

 更新時(shí)間:2017年09月14日 09:08:43   投稿:jingxian  
下面小編就為大家?guī)硪黄掌跁r(shí)間范圍選擇插件:daterangepicker使用總結(jié)(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

分享說明:

項(xiàng)目中要使用日期時(shí)間范圍選擇對(duì)數(shù)據(jù)進(jìn)行篩選;精確到年月日 時(shí)分秒;起初,使用了layui的時(shí)間日期選擇插件;但是在IIE8第一次點(diǎn)擊會(huì)報(bào)設(shè)置格式錯(cuò)誤;研究了很久沒解決,但能確定不是layui的問題;因?yàn)樽约簩懙膁emo可以在IE8運(yùn)行;只是在我的項(xiàng)目環(huán)境下某些代碼沖突了;所以換用了bootstrap插件daterangepicker;看了很多資料;結(jié)合官網(wǎng)了文檔;基本算是搞定了;把我的總結(jié)代碼分享給大家;希望對(duì)使用daterangepicker插件的初學(xué)者有幫助。

總結(jié)分為四個(gè)部分:日期范圍選擇實(shí)現(xiàn),日期時(shí)間選擇,使用兩個(gè)單日歷實(shí)現(xiàn)范圍選擇,使用div代替input實(shí)現(xiàn)日期時(shí)間選擇;下面是代碼

css 代碼

<style type="text/css">
 body,
 ul,
 p,
 h3,
 img,
 input {
  margin: 0;
  padding: 0;
 }

 .box {
  display: block;
  text-align: center;
  margin: 20px auto;
 }

 input {
  width: 400px;
  height: 40px;
 }

 label {
  display: inline-block;
  width: 90px;
  line-height: 40px;
  height: 40px;
  margin: 0;
  font-weight: normal;
  font-family: "宋體";
  background-color: #ddd;
 }
 .divDateSelect{
  width: 185px;
  height: 50px;
  line-height: 50px;
  margin:10px auto;
  border:2px solid #ddd;
  border-radius: 5px;
 }
 </style>

html代碼:

<!-- 日期時(shí)間范圍選擇代碼 -->
 <div class="box">
  <label for="datePicker">雙日歷</label>
  <input type="text" name="datePicker" class="datePicker" id="datePicker">
 </div>
 <!-- 日期時(shí)間選擇代碼 -->
 <div class="box">
  <label for="singledatePicker">單日歷</label>
  <input type="text" name="singledatePicker" class="singledatePicker" id="singledatePicker">
 </div>
 <!-- 兩個(gè)單日歷實(shí)現(xiàn)日期時(shí)間范圍選擇代碼 -->
 <div class="box">
  <label for="from">從</label>
  <input type="text" name="from" class="from" id="from">
  <label for="to">到</label>
  <input type="text" name="to" class="to" id="to">
 </div>
 <!-- 不使用input,用div實(shí)現(xiàn)代碼 -->
 <div class="divDateSelect" id="divDateSelect">
  &nbsp;<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
  <span></span> <b class="caret"></b>
 </div>

js 代碼,按照上下順序?qū)?yīng)html四部分

$('input[name="datePicker"]').daterangepicker({
  timePicker: true, //顯示時(shí)間
  timePicker24Hour: true, //時(shí)間制
  timePickerSeconds: true, //時(shí)間顯示到秒
  startDate: moment().hours(0).minutes(0).seconds(0), //設(shè)置開始日期
  endDate: moment(new Date()), //設(shè)置結(jié)束器日期
  maxDate: moment(new Date()), //設(shè)置最大日期
  "opens": "center",
  ranges: {
   // '今天': [moment(), moment()],
   '昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
   '上周': [moment().subtract(6, 'days'), moment()],
   '前30天': [moment().subtract(29, 'days'), moment()],
   '本月': [moment().startOf('month'), moment().endOf('month')],
   '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  },
  showWeekNumbers: true,
  locale: {
   format: "YYYY-MM-DD HH:mm:ss", //設(shè)置顯示格式
   applyLabel: '確定', //確定按鈕文本
   cancelLabel: '取消', //取消按鈕文本
   customRangeLabel: '自定義',
   daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
   monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
    '七月', '八月', '九月', '十月', '十一月', '十二月'
   ],
   firstDay: 1
  },
 }, function(start, end, label) {
  timeRangeChange = [start.format('YYYY-MM-DD HH:mm:ss'), end.format('YYYY-MM-DD HH:mm:ss')];
  console.log(timeRangeChange);
 });
$('input[name="singledatePicker"]').daterangepicker({
  "autoApply": true, //選擇日期后自動(dòng)提交;只有在不顯示時(shí)間的時(shí)候起作用timePicker:false
  singleDatePicker: true, //單日歷
  showDropdowns: true, //年月份下拉框
  timePicker: true, //顯示時(shí)間
  timePicker24Hour: true, //時(shí)間制
  timePickerSeconds: true, //時(shí)間顯示到秒
  startDate: moment().hours(0).minutes(0).seconds(0), //設(shè)置開始日期
  maxDate: moment(new Date()), //設(shè)置最大日期
  "opens": "center",
  showWeekNumbers: true,
  locale: {
   format: "YYYY-MM-DD HH:mm:ss", //設(shè)置顯示格式
   applyLabel: '確定', //確定按鈕文本
   cancelLabel: '取消', //取消按鈕文本
   daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
   monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
    '七月', '八月', '九月', '十月', '十一月', '十二月'
   ],
   firstDay: 1
  },
 }, function(start) {
  console.log(start.format('YYYY-MM-DD HH:mm:ss'));
 });
var minDate = null;
 var max = null;
 function fromDate(maxDate) {
  if(!maxDate){
   max = moment(new Date())
  }else{
   max = maxDate;
  }
  $('input[name="from"]').daterangepicker({
   "autoApply": true, //選擇日期后自動(dòng)提交;只有在不顯示時(shí)間的時(shí)候起作用timePicker:false
   singleDatePicker: true, //單日歷
   showDropdowns: true, //年月份下拉框
   timePicker: true, //顯示時(shí)間
   timePicker24Hour: true, //時(shí)間制
   timePickerSeconds: true, //時(shí)間顯示到秒
   // startDate: moment().hours(0).minutes(0).seconds(0), //設(shè)置開始日期
   maxDate: max , //設(shè)置最大日期
   "opens": "center",
   showWeekNumbers: true,
   locale: {
    format: "YYYY-MM-DD HH:mm:ss", //設(shè)置顯示格式
    applyLabel: '確定', //確定按鈕文本
    cancelLabel: '取消', //取消按鈕文本
    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
     '七月', '八月', '九月', '十月', '十一月', '十二月'
    ],
    firstDay: 1
   },
  }, function(s) {
   toDate(s);
  });
 }
 fromDate()
 function toDate(minDate) {
  $('input[name="to"]').daterangepicker({
   "autoApply": true, //選擇日期后自動(dòng)提交;只有在不顯示時(shí)間的時(shí)候起作用timePicker:false
   singleDatePicker: true, //單日歷
   showDropdowns: true, //年月份下拉框
   timePicker: true, //顯示時(shí)間
   timePicker24Hour: true, //時(shí)間制
   timePickerSeconds: true, //時(shí)間顯示到秒
   // startDate: moment().hours(0).minutes(0).seconds(0), //設(shè)置開始日期
   maxDate: moment(new Date()), //設(shè)置最大日期
   minDate: minDate,
   "opens": "center",
   showWeekNumbers: true,
   locale: {
    format: "YYYY-MM-DD HH:mm:ss", //設(shè)置顯示格式
    applyLabel: '確定', //確定按鈕文本
    cancelLabel: '取消', //取消按鈕文本
    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
     '七月', '八月', '九月', '十月', '十一月', '十二月'
    ],
    firstDay: 1
   },
  }, function(s) {
   fromDate(s)
  });
 }
 toDate();
var start = moment(new Date());
 function cb(start) {
  $('#divDateSelect span').html(start.format('YYYY-MM-DD HH:mm:ss'));
 }
 $('#divDateSelect').daterangepicker({
  "autoApply": true, //選擇日期后自動(dòng)提交;只有在不顯示時(shí)間的時(shí)候起作用timePicker:false
  singleDatePicker: true, //單日歷
  showDropdowns: true, //年月份下拉框
  // timePicker: true, //顯示時(shí)間
  timePicker24Hour: true, //時(shí)間制
  timePickerSeconds: true, //時(shí)間顯示到秒
  startDate: moment().hours(0).minutes(0).seconds(0), //設(shè)置開始日期
  maxDate: moment(new Date()), //設(shè)置最大日期
  "opens": "center",
  showWeekNumbers: true,
  locale: {
   format: "YYYY-MM-DD HH:mm:ss", //設(shè)置顯示格式
   applyLabel: '確定', //確定按鈕文本
   cancelLabel: '取消', //取消按鈕文本
   daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
   monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
    '七月', '八月', '九月', '十月', '十一月', '十二月'
   ],
   firstDay: 1
  },
 }, cb);
 cb(start);

效果圖:

第一部分:

第二部分:

第三部分就是兩個(gè)第二部分組實(shí)現(xiàn)第一部分的效果;原理為在確定好開始日期后;設(shè)置選擇結(jié)束日期日歷的最小選擇日期;在結(jié)束日期選中后;設(shè)置開始日期的最大選擇日期;

第四部分:

關(guān)鍵選項(xiàng)的含義已經(jīng)在代碼中注釋了;引入文件css包括bootstrap的css文件;daterangepicker的css文件;js包括jquery的js;bootstrap的js;daterangepicker的js以及moment.js;

備注:

1 moment.js使用了數(shù)組的indexOf()方法;但I(xiàn)E8不支持;需要引入兼容代碼;代碼地址https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf  在polyfill下;

2 在IE8下;雙日歷的范圍選擇出現(xiàn)連個(gè)日歷豎直排列問題;解決方法為給存放兩個(gè)日歷的盒子設(shè)置固定的寬度,足以放下兩個(gè)日歷的div;再把兩個(gè)日歷的div設(shè)置float:left即可;

3 官網(wǎng)地址;選項(xiàng)設(shè)置: http://www.daterangepicker.com/#options  

例子: http://www.daterangepicker.com/#examples

4 希望本文能夠幫助初識(shí)daterangepicker的朋友.

以上這篇日期時(shí)間范圍選擇插件:daterangepicker使用總結(jié)(必看篇)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

桦甸市| 高尔夫| 商水县| 公主岭市| 九龙城区| 南昌县| 潼关县| 都昌县| 隆德县| 泗阳县| 临泉县| 嘉禾县| 桑日县| 阿拉善盟| 云霄县| 乃东县| 定边县| 洛隆县| 庆阳市| 通渭县| 防城港市| 阳江市| 商城县| 宁德市| 汉川市| 平原县| 靖西县| 安岳县| 黔江区| 时尚| 道孚县| 博白县| 彭州市| 乐清市| 塘沽区| 梅州市| 内江市| 长兴县| 平塘县| 安国市| 威远县|