vue-week-picker实现支持按周切换的日历

本文实例为大家分享了vue-week-picker实现按周切换的日历的具体代码,供大家参考,具体内容如下

vue-week-picker

安装

npm install vue-week-picker --save-dev

DEMO

功能

  • 自适应式按周切换
  • 与DatePicker日期选择器使用

结合Element-ui使用

效果

与vue-element结合组件,请转到链接

使用

<VueWeekPicker @dateValue="dateValue" />

Or

<vue-week-picker @dateValue="dateValue" />

import VueWeekPicker from 'vue-week-picker';

export default {
 components: {
 VueWeekPicker
 }
}

Or

export default {
 components: {
 'vue-week-picker': VueWeekPicker
 }
}

代码

<template>
 <div class="date">
 <el-row>
 <el-col :span="24">
 <div class="weeks">
  <!-- 日期 -->
  <ul class="days">
  <li @click="weekPre" class="prev-btn">
  <i class="fa fa-angle-left fa-icon" aria-hidden="true"></i>
  <span class="hidden-sm-and-down" style="margin-left: 5px;">上一周</span>
  </li>
  <li
  @click="pick(day, index)"
  v-for="(day, index) in days"
  :key="index"
  :class="{selected:index == tabIndex}"
  >
  <!--本月-->
  <span v-if="day.getMonth()+1 != currentMonth" class="other-month item-wrapper">
  <p>{{day | getWeekFormat}}</p>
  <span class="hidden-sm-and-down">{{ day | dateFormat }}</span>
  </span>
  <span v-else>
  <!--今天-->
  <span
   v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()"
   class="today-item"
  >今天</span>
  <span class="item-wrapper" v-else>
   <p>{{day | getWeekFormat}}</p>
   <span class="hidden-sm-and-down">{{ day | dateFormat }}</span>
  </span>
  </span>
  </li>
  <li @click="weekNext" class="next-btn">
  <span class="hidden-sm-and-down" style="margin-right: 5px;">下一周</span>
  <i class="fa fa-angle-right fa-icon" aria-hidden="true"></i>
  </li>
  <li>
  <span>
  <el-date-picker
   class="right-pick-btn"
   style="width: 100%"
   @change="pickDate"
   v-model="value1"
   type="date"
   placeholder="按日期查询"
  ></el-date-picker>
  </span>
  </li>
  </ul>
 </div>
 </el-col>
 </el-row>
 <el-row>
 <el-col :span="20" :offset="2" class="time-range">
 <span
  @click="pickTime(time, index)"
  v-for="(time, index) in times"
  :key="index"
  :class="{active:index == tabTimeIndex}"
 >{{time}}</span>
 </el-col>
 </el-row>
 </div>
</template>
<script>
/* eslint-disable */
import moment from "moment";
export default {
 props: {
 dateValue: {
 type: String,
 default: moment(new Date()).format("YYYY-MM-DD")
 },
 timeValue: {
 type: String,
 default: "00:00"
 }
 },
 data() {
 return {
 currentYear: 1970, // 年份
 currentMonth: 1, // 月份
 currentDay: 1, // 日期
 currentWeek: 1, // 星期
 days: [],
 value1: "",
 tabIndex: null,
 tabTimeIndex: 0,
 times: [
 "00:00~06:00",
 "06:00~12:00",
 "12:00~18:00",
 "18:00~24:00",
 "今日节目"
 ]
 };
 },
 filters: {
 dateFormat(date) {
 return moment(date).format("YYYY-MM-DD");
 },
 getWeekFormat(date) {
 const weeksObj = {
 1: "周一",
 2: "周二",
 3: "周三",
 4: "周四",
 5: "周五",
 6: "周六",
 7: "周日"
 };
 let weekNumber = moment(date).isoWeekday();
 return weeksObj[weekNumber];
 }
 },

 mounted() {
 const index = _.findIndex(this.days, function(o) {
 // console.log('o: ', o.getDate());
 // console.log('new Date().getDate(): ', new Date().getDate());
 return o.getDate() === new Date().getDate();
 });
 console.log("index: ", index);
 this.tabIndex = index;
 },

 created() {
 this.initData(null);
 },

 methods: {
 formatDate(year, month, day) {
 const y = year;
 let m = month;
 if (m < 10) m = `0${m}`;
 let d = day;
 if (d < 10) d = `0${d}`;
 return `${y}-${m}-${d}`;
 },
 pickDate(date) {
 let newDate = moment(date).format("YYYY-MM-DD");
 this.$emit("dateValue", newDate);
 },
 initData(cur) {
 let date = "";
 if (cur) {
 date = new Date(cur);
 } else {
 date = new Date();
 }
 this.currentDay = date.getDate(); // 今日日期 几号
 this.currentYear = date.getFullYear(); // 当前年份
 this.currentMonth = date.getMonth() + 1; // 当前月份
 this.currentWeek = date.getDay(); // 1...6,0 // 星期几
 if (this.currentWeek === 0) {
 this.currentWeek = 7;
 }
 const str = this.formatDate(
 this.currentYear,
 this.currentMonth,
 this.currentDay
 ); // 今日日期 年-月-日
 this.days.length = 0;
 // 今天是周日,放在第一行第7个位置,前面6个 这里默认显示一周,如果需要显示一个月,则第二个循环为 i<= 35- this.currentWeek
 /* eslint-disabled */
 for (let i = this.currentWeek - 1; i >= 0; i -= 1) {
 const d = new Date(str);
 d.setDate(d.getDate() - i);
 // console.log(y:" + d.getDate())
 this.days.push(d);
 }
 for (let i = 1; i <= 7 - this.currentWeek; i += 1) {
 const d = new Date(str);
 d.setDate(d.getDate() + i);
 this.days.push(d);
 }
 },

 // 上个星期
 weekPre() {
 const d = this.days[0]; // 如果当期日期是7号或者小于7号
 d.setDate(d.getDate() - 7);
 this.initData(d);
 },

 // 下个星期
 weekNext() {
 const d = this.days[6]; // 如果当期日期是7号或者小于7号
 d.setDate(d.getDate() + 7);
 this.initData(d);
 },

 // 上一個月 传入当前年份和月份
 pickPre(year, month) {
 const d = new Date(this.formatDate(year, month, 1));
 d.setDate(0);
 this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
 },

 // 下一個月 传入当前年份和月份
 pickNext(year, month) {
 const d = new Date(this.formatDate(year, month, 1));
 d.setDate(35);
 this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
 },

 // 当前选择日期
 pick(date, index) {
 let newDate = moment(date).format("YYYY-MM-DD");
 this.$emit("dateValue", newDate);
 // console.log("index: ", index);
 this.tabIndex = index;
 // alert(
 // this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate())
 // );
 },
 pickTime(time, index) {
 // console.log('time: ', time);
 let timeArr = [];
 timeArr.push(_.head(_.split(time, "~")));
 console.log("timeArr: ", timeArr);
 this.$emit("timeValue", _.join(timeArr), "");
 // console.log("index: ", index);
 this.tabTimeIndex = index;
 // alert(
 // this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate())
 // );
 }
 }
};
</script>

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

(0)

相关推荐

  • Vue编写可显示周和月模式的日历 Vue自定义日历内容的显示

    之前写了一篇周和月日历,但感觉体验不是太好,所以有重新做了一遍,加上了动画.还可以自定义显示日历里的内容. 现在贴出项目源码,我现在是放在CSDN里的下载资源,这里哦 现在我上传带了GitHub上了,可以去这里下载哦,如果觉得好的话希望能给个star,谢谢支持 1.总共分为两个组件(父组件calendar.vue) <template> <div class="calendar-box"> <ul class="calendar-head&quo

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

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

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

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

  • 基于Vue实现支持按周切换的日历

    基于Vue的日历小功能,可根据实际开发情况按每年.每月.每周.进行切换,具体内容如下 <template> <div class="date"> <!-- 年份 月份 --> <div class="month"> <p>{{ currentYear }}年{{ currentMonth }}月</p> </div> <!-- 星期 --> <ul class=&q

  • Vue.js创建Calendar日历效果

    使用 Vue.js 进行数据与视图的绑定,数据更新会让视图自动进行更新,类似 Android 里面的 DataBinding. 实现一个HTML的日历效果. html 部分 <div id="calendar"> <!-- 年份 月份 --> <div class="month"> <ul> <li class="arrow" @click="pickPre(currentYear,

  • vue实现日历备忘录功能

    用vue写了个日历备忘录的功能,省略了备忘录的增删改查功能. 直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>备忘录</title> <style type="text/css"> #box{ width: 469px; } /*日历*/ *{ padding:

  • vue实现简单的日历效果

    最近在项目中遇到了一个需求,在vue中创建一个组件,这个组件显示的是当前的日期,以及在当前的日需要处理的事项,处理的事项的信息会以后端的接口的形式返回. 需求确认后,搭建了一下,在这里记录了一下,现在是简单的实现了这个需求,但是肯定的是后期需要进行修改. vue就不多说了,在vue中使用的是原生JS 效果图(基本没有样式,很low) 现在实现的都是最初级的版本,代码里面的容错,还有一些性能上的处理,并没有书写. 不多说,上代码: 首先是vue的html结构,很简单,里面添加了一些其他时间形式的显

  • 亲自动手实现vue日历控件

    之前项目中有用到日历控件,当时由于时间问题,是在网上找到一个demo,然后二次开发的,从那时就想着自己写一个日历控件.这篇文章说明日历数据的处理,去除月份天数判断以及是否闰年判断. 设计(以最常用的按月份的日历) 日历其实大家都很熟悉,一切的设计都是从功能出发,这是根本.日历的功能分为两大块. 日历头部:当前年份/月份. 日历主体:当前月份的具体的日期信息. 日历主体的行数:现在我们看到的日历基本上为6行,因为一个月最多为31天,假设当前月的第一天为上一月最后一周的最后一天.如果是五行数据的话则

  • Vue 组件(component)教程之实现精美的日历方法示例

    组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要,使用不同的组件来拼接页面.这种开发模式使得前端页面易于扩展,且灵活性高,而且组件之间也实现了解耦. 最近应公司的要求,需要开发一个精美的日历组件(IOS , 安卓, PC 的IE9+都能运行),写完后想把它分享出来,希望大家批评. 先来个截图 代码已经分享到 https://github.com/zhangKun

  • VUE实现日历组件功能

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

随机推荐