基于vue-mobile-calendar插件,写的一个移动端日期弹窗插件

vue2.0  pc端适配移动端,在element-ui的基础上使用vue-mobile-calendar来进行适配。

效果:

引入vue-mobile-calendar

 npm install vue-mobile-calendar

在main.js进行全局引入

// 移动端日期插件
import Calendar from "vue-mobile-calendar";
Vue.use(Calendar);

组件代码:

<template>
  <div>
    <div @click="datePopup">
      <el-date-picker
        v-model="newDates"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :editable="false"
        popper-class="popperClass1"
      >
      </el-date-picker>
    </div>
    <el-dialog
      title="日期选择"
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      width="98%"
      style="margin-top: 10vh"
      custom-class="date-picker-dialog"
    >
      <div class="dialog-left">
        <el-button
          v-for="item in pickerOptions"
          :key="item.id"
          @click="onClick(item.id)"
          size="small"
          type="text"
          >{{ item.text }}</el-button
        >
      </div>
      <div class="dialog-right">
        <inlineCalendar
          :defaultDate="datePicker"
          mode="during"
          :enableTouch="false"
          @change="onChange"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  model: {
    prop: "value",
    event: "click",
  },
  data() {
    return {
      newDates: [], //日期选择
      datePicker: [], //日期默认值回显
      dialogVisible: false, // 弹窗显示状态
      //日期快捷键选择
      pickerOptions: [
        {
          text: "今    天",
          id: 1,
        },
        {
          text: "昨    天",
          id: 2,
        },
        {
          text: "最近三天",
          id: 3,
        },
        {
          text: "最近一周",
          id: 4,
        },
        {
          text: "最近30天",
          id: 5,
        },
        {
          text: "前一个月",
          id: 6,
        },
        {
          text: "前两个月",
          id: 7,
        },
        {
          text: "前三个月",
          id: 8,
        },
        {
          text: "最近一年",
          id: 9,
        },
      ],
    };
  },
  props: {
    value: {
      type: String | Array,
      default: () => {
        return [];
      },
    },
  },
  watch: {
    // 监听弹窗,若弹窗打开 true ,将父组件value值赋值给 newDates 和datePicker 进行回显
    dialogVisible(val) {
      if (val) {
        this.newDates = this.value;
        this.datePicker = this.value;
      }
    },
    // 监听日期选择,并且传值到父组件
    newDates(data) {
      let timeData = data || [];
      if (timeData.length > 0) {
        timeData = [timeData[0] + " 00:00:00", timeData[1] + " 23:59:59"];
      }
      this.$emit("click", timeData);
    },
  },
  created() {
    // 首次加载将父组件值赋值给newDates
    this.newDates = this.value;
  },
  methods: {
    //日期弹窗
    datePopup() {
      this.dialogVisible = true;
    },

    /**
     * @function 日期列表选择
     * @param {Array} date -日期范围选择框选中的值
     */
    onChange(date) {
      if (date.length == 2) {
        this.newDates = [];
        //将 date进行格式化 得到 "YYYY-MM-DD"格式日期
        date.forEach((element) => {
          this.newDates.push(element.format("YYYY-MM-DD"));
        });
        // 判断数组值是否不相同,在进行数组去重
        if (this.newDates[0] != this.newDates[1]) {
          this.newDates = [...new Set(this.newDates)];
        }
        this.dialogVisible = false;
      }
    },

    /**
     *快捷日期选择方法
     * @param item {*} item - ID用于switch判断执行
     */
    onClick(item) {
      let end = new Date();
      let start = new Date();
      let now = new Date();
      let year = now.getFullYear();
      let month = now.getMonth();
      const myDate = new Date(year, month, 0);
      switch (item) {
        // 当天
        case 1:
          this.newDates = [
            this.msToDate(new Date()),
            this.msToDate(new Date()),
          ];
          this.dialogVisible = false;
          break;
        // 昨天
        case 2:
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24);
          this.newDates = [this.msToDate(date), this.msToDate(date)];
          this.dialogVisible = false;
          break;
        // 最近三天
        case 3:
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 2);
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
          break;
        // 最近一周
        case 4:
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
          break;
        // 最近30天
        case 5:
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
          break;
        // 前一个月
        case 6:
          if (month <= 0) {
            month = month + 12;
            year = year - 1;
          }
          if (parseInt(month) < 10) {
            month = "0" + month;
          }
          start = new Date(year + "-" + month + "-01");
          end = new Date(year + "-" + month + "-" + myDate.getDate());
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
          break;
        // 前两个月
        case 7:
          month = month - 1;
          if (month <= 0) {
            month = month + 12;
            year = year - 1;
          }
          if (parseInt(month) < 10) {
            month = "0" + month;
          }
          start = new Date(year + "-" + month + "-01");
          end = new Date(year + "-" + month + "-" + myDate.getDate());
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
          break;
        // 前三个月
        case 8:
          month = month - 2;
          if (month <= 0) {
            month = month + 12;
            year = year - 1;
          }
          if (parseInt(month) < 10) {
            month = "0" + month;
          }
          start = new Date(year + "-" + month + "-01");
          end = new Date(year + "-" + month + "-" + myDate.getDate());
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
          break;
        // 最近一年
        default:
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
          this.newDates = [this.msToDate(start), this.msToDate(end)];
          this.dialogVisible = false;
      }
    },
    /**
     * 用于将中国标准时间转换成 YYYY-MM-DD
     * @param {*} msec -日期参数
     */
    msToDate(msec) {
      let datetime = new Date(msec);
      let year = datetime.getFullYear();
      let month = datetime.getMonth();
      let date = datetime.getDate();
      let result =
        year +
        "-" +
        (month + 1 >= 10 ? month + 1 : "0" + (month + 1)) +
        "-" +
        (date + 1 < 10 ? "0" + date : date);
      return result;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-button + .el-button,
.el-checkbox.is-bordered + .el-checkbox.is-bordered {
  margin-left: 0px;
}
.el-button--text {
  border-color: transparent;
  color: #606266;
  background: 0 0;
  padding-left: 0;
  padding-right: 0;
}
.dialog-left {
  float: left;
  width: 15%;
  height: 100%;
  padding: 5px;
}
.dialog-right {
  float: right;
  width: 80%;
  text-align: center;
  border-left: 2px solid #f5f5f5;
  padding: 0 5px;
}
</style>
<style lang="scss">
body {
  padding-right: 0 !important;
}

.popperClass1 {
  border: none;
}
.popperClass1 .el-picker-panel__body {
  display: none !important;
}

.el-range-editor.el-input__inner {
  width: 100%;
}

.date-picker-dialog .el-dialog__body {
  padding: 0 5px;
}

.m-calendar {
  background: #fff;

  .m-toolbar {
    background: #ffffff;
    height: 44px;
    font-size: 17px;
    .m-year-selector {
      width: 50%;
      padding: 0 10px;
    }
    .m-month-selector {
      width: 50%;
      padding: 0 10px;
    }
    .m-toolbar {
      background: #ffffff;
    }
  }
  .m-months-container {
    .m-months-wrapper {
      .m-months {
        .m-row {
          .m-day {
            .m-during {
              background: #409eff;
              color: #fff;
              width: 30px;
              height: 30px;
              line-height: 30px;
              display: inline-block;
              border-radius: 100%;
            }
          }
        }
      }
    }
  }
  .m-week-header {
    background: #ffffff;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法提供具体的代码示例。但是,以下是一些可以帮助您开始使用Vue和Uni-app创建移动端界面的步骤: 1. 安装 Vue 和 Uni-app 首先,您需要在本地安装Vue和Uni-app。您可以使用npm或yarn来安装它们。在命令行中输入以下命令: ``` npm install -g vue npm install -g @vue/cli npm install -g @vue/cli-init npm install -g @dcloudio/uni-cli ``` 2. 创建一个新的Uni-app项目 在命令行中输入以下命令: ``` vue init dcloudio/uni-preset-vue my-project ``` 这将创建一个名为my-project的新Uni-app项目。 3. 编Vue组件 现在,您可以开始编Vue组件来构建您的界面。您可以将组件放在src/components目录中。 例如,以下是一个简单的Vue组件示例: ``` <template> <div class="my-component"> <h1>{{ title }}</h1> <p>{{ description }}</p> </div> </template> <script> export default { name: 'MyComponent', props: { title: String, description: String } } </script> <style scoped> .my-component { background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } h1 { font-size: 24px; margin-bottom: 10px; } p { font-size: 16px; } </style> ``` 4. 添加组件到页面 现在,您可以将组件添加到页面中。您可以在pages目录下创建一个新的页面,并在该页面中引用您的组件。 例如,以下是一个简单的页面示例: ``` <template> <div class="my-page"> <my-component title="Hello World" description="This is a test component"></my-component> </div> </template> <script> import MyComponent from '@/components/MyComponent' export default { name: 'MyPage', components: { MyComponent } } </script> <style scoped> .my-page { padding: 20px; } </style> ``` 5. 运行应用程序 现在,您可以在命令行中输入以下命令来启动您的应用程序: ``` npm run dev:mp-weixin ``` 这将启动一个本地开发服务器,并在微信小程序中显示您的应用程序。您可以使用微信开发者工具来查看您的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值