vue日历组件的封装方法

本文实例为大家分享了vue日历组件的封装代码,供大家参考,具体内容如下

图示

封装的组件的代码如下

<template>
  <div class="calendar">
    <!-- 选择日历的弹出层 -->
    <div class="model_mask" v-show="showtimemask" @click="showmask1()">
    </div>
    <div class="bouncedBox" v-show="showtimemask">
      <div class="mobile-top">
        <div class="sel-time">
          <p>开始时间</p>
          <p class="start-date">{{starttime.substring(0,4)+'-'+starttime.substring(4,6)+'-'+starttime.substring(6,8)}}
          </p>
        </div>
        <div class="unsel-time">
          <p>结束时间</p>
          <p class="end-date">
            {{endtime==''?'请选择结束日期':endtime.substring(0,4)+'-'+endtime.substring(4,6)+'-'+endtime.substring(6,8)}}</p>
        </div>
      </div>
 
      <div class="title">
        <div class="btn" @click.stop="last()" :class="(month<=nowmonth)&&(Year<=nowYear)?'noclick':'' ">上一月</div>
        <div class="text">{{Year}}年{{month}}月</div>
        <div class="btn" @click.stop="next()">下一月</div>
      </div>
 
      <div class="head">
        <div class="days" v-for="(item,index) in ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']" :key="index">
          {{item}}
        </div>
      </div>
 
      <div class="wrap">
        <div class="span" v-for="(item,index) in calendarList" :key="index" @click.stop="click(item.count)" :class="item==''?'kong'
      :item.count<nowtime?'noclick'
      :(item.count>=starttime&&item.count<=endtime)||item.count==starttime?'active':''">
          {{item.value}}
        </div>
      </div>
 
      <div class="bottombtn">
        <button class="cancle-btn" @click.stop='cancle()'>取消</button>
        <button class="sure-btn" @click.stop='firm()'>确定</button>
      </div>
 
    </div>
  </div>
</template>
 
<script>
  export default {
    name: "Calendar",
    data() {
      return {
        showtimemask:false,
        Puton_time: '', //投放日期  默认今日 展示
        Puton_Start:'',  //为了保存投放开始结束的日期  用来点击取消按钮时初始化选中的值
        Puton_End:'',
        nowtime: '', //当前日期的时间-----20190203格式  用于比较
        clickitem: '', //保存每次点击的时间-----20190203格式  用于比较
        clickcount: 0, //点击次数-------判断开始时间还是结束时间
        starttime: '', //开始时间  数字   默认当天日期
        endtime: '', //结束时间  数字   默认当天日期
        Year: new Date().getFullYear(), //日历上的年份   ----动态改变的
        month: new Date().getMonth() + 1, //日历上的月份 ----  动态改变的
        Day: new Date().getDate(), //日历上的天份         ----- 动态改变的
 
        nowYear: new Date().getFullYear(),
        nowmonth: new Date().getMonth() + 1,
        nowDay: new Date().getDate(),
        calendarList: [],
      };
    },
 
    created() {
      //关于日历的操作开始
      this.Draw(this.nowYear, this.nowmonth);
 
      let time_month = this.nowmonth; //现在的月份
      let time_day = this.nowDay; //现在的天数
      if (this.nowmonth < 10) {
        time_month = 0 + '' + this.nowmonth;
      }
      if (this.nowDay < 10) {
        time_day = 0 + '' + this.nowDay;
      }
 
      this.nowtime = this.nowYear + '' + time_month + '' + time_day;
      this.starttime = this.nowtime;
      this.endtime = this.nowtime;
 
      this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime
        .substring(6, 8) + '至今';
 
        this.Puton_Start = this.nowtime,
        this.Puton_End = this.nowtime,
 
 
        this.$emit('str',this.Puton_time)
 
      //关于日历的操作结束
    },
    mounted() {
 
    },
    methods: {
      showmask1() {
        if (this.showtimemask == true) {
          // this.showtimemask=false;   //隐藏弹框
          this.cancle();
        } else {
          this.showtimemask = true;  //显示弹框
        }
      },
 
      Draw: function (Year, Month) {
        //日期列表
        var calendar = [];
 
        //用当月第一天在一周中的日期值作为当月离第一天的天数(获取当月第一天是周几)
        for (var i = 1, firstDay = new Date(Year, Month - 1, 1).getDay(); i <= firstDay; i++) {
          calendar.push("");
        }
 
        //用当月最后一天在一个月中的日期值作为当月的天数
        for (var i = 1, monthDay = new Date(Year, Month, 0).getDate(); i <= monthDay; i++) {
 
          let time_month = Month;
          let time_day = i;
          if (Month < 10) {
            time_month = 0 + '' + Month;
          }
          if (i < 10) {
            time_day = 0 + '' + i;
          }
 
          calendar.push({
            value: i,
            count: Year + '' + time_month + '' + time_day
          })
        }
        this.calendarList = calendar;
        console.log(calendar)
      },
 
      last() {
        this.month--;
        if (this.month == 0) {
          this.month = 12;
          this.Year--;
        }
 
        this.Draw(this.Year, this.month);
      },
 
      next() {
        this.month++;
        if (this.month == 13) {
          this.month = 1;
          this.Year++;
        }
 
        this.Draw(this.Year, this.month);
      },
 
      click(item) {
        this.clickcount++;
        this.clickitem = item;
        //开始日期
        if (this.clickcount % 2 == 1) {
          this.starttime = this.clickitem;
          this.endtime = ''
        } else {
          this.endtime = this.clickitem;
          if (this.starttime > this.endtime) {
            this.endtime = this.starttime;
            this.starttime = this.clickitem;
          }
        }
      },
 
      firm() {
        this.showtimemask = false;
        //当选择的开始时间与结束时间相同时   显示为2019-07-19当天
        if (this.starttime == this.endtime) {
          this.Puton_Start = this.starttime,
          this.Puton_End = this.endtime,
 
          this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime
            .substring(6, 8) + '当天';
 
            this.$emit('str',this.Puton_time);
 
            //否则显示xxx 至   xxx
        } else {
 
          this.Puton_Start = this.starttime,
          this.Puton_End = this.endtime,
          this.Puton_time =
            this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime.substring(6,
            8) +
            '至' + this.endtime.substring(0, 4) + '-' + this.endtime.substring(4, 6) + '-' + this.endtime.substring(6, 8);
 
             
 
            this.$emit('str',this.Puton_time)
        }
 
      },
      // 取消按钮
      cancle() {
        this.showtimemask = false; 
        //当按取消按钮时   弹框中选中的区域等于上一次选中的区域
        this.starttime = this.Puton_Start;
        this.endtime = this.Puton_End;
        // this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime
        //   .substring(6, 8) + '至今';
 
        //   this.$emit('str',this.Puton_time)
      }
    }
  };
 
</script>
 
<style scoped lang="scss">
  @import "../common/common.css";
 
  // 日历的样式
  .model_mask {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba($color: #000000, $alpha: 0.5);
  }
 
  .bouncedBox {
    position: fixed;
    background: #fff;
    bottom: 0;
    left: 0;
    right: 0;
 
    //开始结束日期的显示
    .mobile-top {
      display: flex;
      flex-wrap: nowrap;
      background: #fff;
      padding: 0.1rem 0;
 
      .sel-time {
        text-align: center;
        width: 50%;
 
        // border-bottom: solid 2px #2a81e8;
        .start-date {
          color: #b1b1b1;
          margin-top: 0.05rem;
        }
      }
 
      .unsel-time {
        text-align: center;
        width: 50%;
 
        .end-date {
          color: #b1b1b1;
          margin-top: 0.05rem;
        }
      }
    }
 
    // 左右选择月份  显示当前年月
    .title {
      width: 100%;
      height: 40px;
      background-color: #60a7e8;
      display: flex;
      flex-wrap: nowrap;
      text-align: center;
      color: #fff;
      font-weight: bold;
      line-height: 40px;
 
      .btn {
        width: 1.2rem;
 
        &.noclick {
          pointer-events: none;
          background: #ccc;
        }
      }
 
      .text {
        flex: 1;
      }
    }
 
    //表头  周1到周天的显示
    .head {
      display: flex;
      flex-wrap: nowrap;
      text-align: center;
      height: 40px;
      line-height: 40px;
 
      .days {
        flex: 1;
      }
    }
 
    //日历表区域
    .wrap {
      width: 7.5rem;
      height: auto;
      overflow: hidden;
      padding-bottom: 1rem;
 
      .span {
        width: 1.07142rem;
        height: 0.6rem;
        background: #fff;
        color: #337ab7;
        float: left;
        text-align: center;
        line-height: 0.6rem;
 
        &.active {
          background: #037ef5;
          color: #fff;
        }
 
        &.noclick {
          pointer-events: none;
          background: #ccc;
        }
 
        &.kong {
          background: #fff;
          pointer-events: none;
        }
      }
    }
 
    //底部按钮区域
    .bottombtn {
      height: 40px;
      width: 100%;
      display: flex;
      flex-wrap: nowrap;
 
      button {
        flex: 1;
      }
 
      .sure-btn {
        background: #037ef5;
 
        color: #fff;
      }
    }
 
  }
 
</style>

使用方法

main,js引入  全局注册组件

import Calendar from './components/fz_zujian/Calendar.vue'    //日历组件
Vue.component('Calendar',Calendar)

页面使用

<div class="" @click="showmodel()">{{str}}</div>
 
<Calendar ref="chi1" v-on:str="getChild"></Calendar>
 
 data() {
      return {
        str: '',
      }
  }
 
 showmodel(){
        this.$refs.chi1.showmask1()
      },
 
      getChild(val) {
        this.str = val
      },

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

(0)

相关推荐

  • vue实现一个炫酷的日历组件

    公司业务新开了一个商家管理微信H5移动端项目,日历控件是商家管理员查看通过日程来筛选获取某日用户的订单等数据. 如图: 假设今天为2018-09-02 90天前: 90天后; 产品需求: 展示当前日期(服务器时间)前后90天,一共181天的日期. 日历可以左右滑动切换月份. 当月份的如果不在181天区间的,需要置灰并且不可点击. 点击日历绑定的节点的外部,关闭弹窗. 涉及内容: 获取服务器时间,渲染日历数据 vue-touch监听手势滑动事件 ios日期兼容处理 clickOutSide自定义指

  • 基于Vue2-Calendar改进的日历组件(含中文使用说明)

    一,前言 我是刚学Vue的菜鸟,在使用过程中需要用到日历控件,由于项目中原来是用jQuery写的,因此用了bootstarp的日历控件,但是配合Vue实在有点蛋疼,不够优雅-- 于是网上搜了好久找到了Vue2-Calendar,不用说,挺好用的,但是同时也发现这个组件有些问题,有些功能挺不符合我们的要求,于是着手改了一版 二,改进的功能 在Vue2-Calendar v2.2.4 版基础上作了优化. 1.改进原控件无法切换语言的BUG,支持 lang='zh-CN'和'en'. 2.日历面板增加

  • vue自定义可选时间的日历组件

    本文实例为大家分享了vue自定义可选时间日历组件的具体代码,供大家参考,具体内容如下 日历功能: 1.过去时间不可选择 2.可自定义不可选时间 3.本月默认展示当天,其他月展示第一天,若为不可选时间,往后顺延 效果图: -------下面开始表演----------- **首先,画出日历页面布局,参照win10系统日历布局*6行7列,为何如此,请自行理解-*本人也是"偷窥"来的 beginDay是当前月第一天的周几,prevMdays是上个月总天数,nowMdays是当月总天数,这样就

  • VUE实现日历组件功能

    哈哈, 就在昨天笔者刚刚在Github 上发布了一个基于VUE的日历组件.过去做日历都是需要引用 jquery moment 引用 fullCalendar.js 的.几者加起来体积庞大不说,也并不是很好使用在vue这种数据驱动的项目里.所以笔者经过一周的拍脑袋,做了一个十分简陋的版本. 简介 目前只支持月视图,该组件是 .vue 文件的形式.所以,大家在使用的时候 是需要node的咯~~~ 安装 npm install vue-fullcalendar DEMO 针对这个组件, 本人做了一个十

  • 从零写vue日历组件

    目录 1.前言 2.vue日历制作 2.1制作月份选择器 2.2制作日历 2.2.1获取当前月所要显示的日期 2.2.2给不同的日期添加不同的样式 2.3将月份选择器和日历组件组合使用 2.4编辑功能 1. 前言 最近做项目遇到一个需求,需要制作一个定制化的日历组件(项目使用的UI框架不能满足需求,算了,我直说了吧,ant design vue的日历组件是真的丑,所以就自己写了一个),如下图所示,需求大致如下:(2)日历可以按照月份进行上下月的切换.(2)按照月份展示周一到周日的排班信息.(3)

  • vue中使用element日历组件的示例代码

    先看下效果图: 完整代码附上 <template> <div class="newSeeds" id="famerCalendar"> <div class="title-bottom"> <el-date-picker :clearable="false" prefix-icon="timeFilter" v-model="value2" ty

  • vue日历组件的封装方法

    本文实例为大家分享了vue日历组件的封装代码,供大家参考,具体内容如下 图示 封装的组件的代码如下 <template>   <div class="calendar">     <!-- 选择日历的弹出层 -->     <div class="model_mask" v-show="showtimemask" @click="showmask1()">     </di

  • Vue分页组件的封装方法

    前言 这个是基于vue2的分页封装,仿照elementUI而写的组件. 效果如图 话不多说,直接上代码 <template>   <div class="pagination">     <!-- 总页数 -->     <div class="total">共{{ total }}条</div>     <!-- 选择每页的条数 -->     <select name="&q

  • VUE 自定义组件模板的方法详解

    本文实例讲述了VUE 自定义组件模板的方法.分享给大家供大家参考,具体如下: 先说下需求吧,因为客户的用户群比较大,如果需求变动,频繁更新版本就需要重新开发和重新发布,影响用户的体验,考虑到这一层就想到,页面展示效果做动态可配,需求更新时,重新配置一份模板录入到数据库,然后根据用户选择的模板进行展示. 关于页面展示做的动态可配,我是参考vue的Component组件方式,开始时可能会遇到组件定义后不能加载的情况,并在控制台如下错误:You are using the runtime-only b

  • vue父子组件的通信方法(实例详解)

    一.父组件向子组件传递数据 1.首先形成父子组件关系 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/vue.js"></script> </head> <body>

  • vue面包屑组件的封装方法

    vue中自己封装面包屑组件,供大家参考,具体内容如下 要实现效果 前言 很多电商类产品都需要实现面包屑导航,用来优化用户体验 一.初级面包屑封装组件 1.封装基础结构组件 Bread.vue <template> <div class='xtx-bread'> <div class="xtx-bread-item"> <RouterLink to="/">首页</RouterLink> </div&g

  • vue版日历组件的实现方法

    开发背景 常用日历组件可能满足不了我们自定义的多种需求(比如样式),因此通常情况下我们可能需要自己手动开发款日历,先上图 开发流程 1. 根据常用日历样式,我们template部分可以分为三部分(上下月及当前月份展示:周日至周六展示:主体日期展示三部分) 1) template部分代码 <div class="date">     <div class="header">         <span class="pre_mo

  • Vue 兄弟组件通信的方法(不使用Vuex)

    项目中,我们经常会遇到兄弟组件通信的情况.在大型项目中我们可以通过引入vuex轻松管理各组件之间通信问题,但在一些小型的项目中,我们就没有必要去引入vuex.下面简单介绍一下使用传统方法,实现父子组件通信的方法. 简单实例:我们在a组件中点击按钮,将信息传给b组件,从而使b组件弹出. 主要的思路就是:先子传父,在父传子 首先我们在 a.vue 组件中 ,给按钮botton绑定一个handleClick事件,事件中我们通过 this.$emit() 方法去触发一个自定义事件,并传递我们的参数. 示

  • Vue弹窗组件的实现方法

    本文实例为大家分享了Vue弹窗组件的实现具体代码,供大家参考,具体内容如下 弹窗组件包含内容: 弹窗遮罩层 内容层的实现(涉及slot.props.$on.$emit) 实现步骤: 1.搭建组件UI样式,HTML.css实现遮罩层.内容区2.编写弹窗内容:通过组件slot插槽接收父组件传递过来的弹窗内容3.组件开关的实现:通过父组件传递进来的props控制组件的显示与隐藏,子组件关闭时通过事件 $emit 触发父组件改变状态值. 组件代码实现 <template>     <div cl

  • vue选项卡组件的实现方法

    本文实例为大家分享了vue选项卡组件的实现代码,供大家参考,具体内容如下 主要功能:点击不同的选项,显示不同的内容 html <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">         <title></title>         <link rel="stylesheet" type="te

随机推荐