element UI-日期选择器(el-date-picker)-起止日期相差30天

15 篇文章 0 订阅
12 篇文章 0 订阅

1.需求:结束日期选择范围:开始日期-开始日期+30天

2.实现:

使用el-date-picker组件的picker-options属性,及picker-options中的disabledDate方法

代码(style也在,可忽略):

<template>
  <div class="medicalRecord">
    <h3 class="title"><span class="left-border"></span>Demo</h3>
    <div class="main-content">
      <div class="content-top">
        <el-form style="width: 100%" :inline="true" :model="formResult" ref="formResult" hide-required-asterisk class="form-box">
          <ul class="flex-space-between" style="width: 100%">
            <li style="width:340px;">
              <el-form-item label="任务起期:" prop="beginTaskDate" class="errorFixCss timePicker" label-width="96px">
                <el-date-picker
                  v-model="formResult.beginTaskDate"
                  type="date"
                  :clearable="true"
                  placeholder="选择日期"
                  format="yyyy-MM-dd"
                  @focus="resetTaskDate"
                  class="startTimeBox"
                ></el-date-picker>
              </el-form-item>
              <el-form-item label="任务止期:" prop="endTaskDate" class="errorFixCss timePicker" label-width="96px">
                <el-date-picker
                  v-model="formResult.endTaskDate"
                  :picker-options="pickerOptions"
                  type="date"
                  :clearable="true"
                  placeholder="选择日期"
                  format="yyyy-MM-dd"
                  class="startTimeBox"
                ></el-date-picker>
              </el-form-item>
            </li>
          </ul>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'taskSearch',
  data () {
    return {
      formResult: {
        beginTaskDate: '', // 任务日期起
        endTaskDate: '' // 任务日期终
      },
      pickerOptions: {
        disabledDate: (time) => {
          if (this.formResult.beginTaskDate) {
            let date1 = this.formResult.beginTaskDate
            let date2 = new Date(date1)
            date2.setDate(date1.getDate() + 30)
            return time.getTime() < this.formResult.beginTaskDate || time.getTime() > date2
          } else {
            return time.getTime() < this.formResult.beginTaskDate
          }
        }
      }
    }
  },
  methods: {
    // 任务起期获取焦点时触发
    resetTaskDate () {
      // 清空任务止期
      this.formResult.endTaskDate = ''
    },
    // 获取数据
    getData () {
      let obj = this.formResult
      console.log('obj;;;', obj)
      this.$axios({
        url: 'XX/query',
        method: 'post',
        data: {
          'tradeMap': {
            'beginTaskDate': obj.beginTaskDate === '' ? '' : this.$moment(obj.beginTaskDate).format('YYYY-MM-DD'), // '任务日期起',
            'endTaskDate': obj.endTaskDate === '' ? '' : this.$moment(obj.endTaskDate).format('YYYY-MM-DD') // '任务日期终',
          }
        }
      }).then(res => {
        console.log(res)
      }).catch(error => {
        console.log(error)
      })
    }
  }
}
</script>
<style lang="less">
  .setClass {
    color: red;
  }
  .el-range__icon {
    background: url('../../../static/img/disabled_date.png') center no-repeat;
  }
  .el-range__icon:before{
    content: "";
    font-size: 16px;
    visibility: hidden;
  }
</style>
<style scoped lang="less" rel="stylesheet/less">
  .medicalRecord{
    /deep/ .el-form--inline .el-form-item__label{
      color: #616467 !important;
      height: 32px !important;
      line-height: 32px !important;
    }
    /deep/ .content-top .form-box .el-form-item__content {
      width: 240px !important;
      height: 32px !important;
    }
    /deep/ .el-form--inline .el-form-item{
      margin-left: 0 !important;
    }
    /deep/ .el-form--inline .el-form-item__label{
      margin-right: 0;
      // text-align: left;
    }
    /deep/ .content-top .inputWrap input{
      width: 240px !important;
      padding:0 4px;
    }
    /deep/ .shortInputWrap {
      .shortInput {
        input{
          width: 94px !important;
        }
      }
    }
    /deep/ .content-top .form-box .shortInputLeft .el-form-item__content{
      width: 94px !important;
    }
    /deep/ .flex-space-between{
      width: 100%;
    }
    /deep/ .line{
      margin-top: 10px;
    }
    /deep/ .content-top{
      padding-bottom: 0 !important;
    }
    /deep/ .selectId .el-input__inner{
      width: 80px !important;
    }
    /deep/ .idInput .el-input__inner {
      font-size: 12px !important;
      width: 150px !important;
    }
    /deep/ .el-date-editor.el-input, .el-date-editor.el-input__inner {
      width: 240px !important;
    }
    /deep/ .el-input--small .el-input__icon {
      line-height: 32px !important;
    }
    /deep/ .el-form-item__content {
      line-height: 32px !important;
    }
    /deep/ .el-input__icon {
      line-height: 32px !important;
    }
    /deep/ .el-table__body .el-radio__inner {
      // display: none;
    }
    /deep/ .el-table .cell {
      padding-right: 0px !important;
    }
    /deep/ .el-radio__label {
      display: none;
      padding-left: 0px !important;
    }
    /deep/ .el-button+.el-button {
      margin-left: 6px !important;
    }
    .errorFixCss {
      /deep/.el-form-item__error {
        margin-top: -4px;
      }
    }
    .errorFixCss .el-date-editor.el-input {
      // margin-top: 2px !important;
    }
    .timePicker {
      /deep/.el-date-editor .el-icon-circle-close {
        position: absolute;
        right: 20px;
        top: 0px;
      }
    }
    .startTimeBox {
      width: 240px !important;
      border: 1px solid #DCDFE6;
      overflow: hidden;
      height: 32px;
      color:#606266;
      border-radius: 4px;
      /deep/ .el-input__inner {
        position: absolute;
        // top: 3px;
        // background: url('../../../static/img/disabled_date.png') 210px center no-repeat;
        background: url('../../../static/img/disabled_date.png') 210px 6px no-repeat;
      }
    }
    /deep/ .el-input {
      height: 32px !important;
    }
    .imgText{
      border: 1px solid #eee;
      border-radius: 4px;
      height: 36px;
      line-height: 36px;
      padding: 0 38px 0 15px;
      text-align: left;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    .title{
      color: #333333;
      font-size: 18px;
      font-weight: bold;
      text-align: left;
      display: flex;
      display: -webkit-flex;
      align-items: center;
      margin-bottom: 20px;
      margin-top: 20px;
      span{
        display: inline-block;
        width: 6px;
        height: 18px;
        background: #FF694E;
        margin-right: 10px;
      }
    }
    .main-content{
      background: #ffffff;
      margin-bottom: 20px;
      padding: 20px 0 0;
      min-width:1180px;
      .content-top{
        padding: 0 15px 20px;
        margin-bottom: 20px;
        border-bottom: 20px solid #F3F3F3;
        display: -webkit-flex;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .btn-box{
          display: flex;
          display: -webkit-flex;
          justify-content: space-around;
          width: 190px;
          height: 36px;
          border: 1px solid #D1D1D1;
          border-radius: 2px;
          overflow: hidden;
          li{
            text-align: center;
            height: 36px;
            line-height: 36px;
            flex: 1;
          }
          li:hover{
            cursor: pointer;
            background: #f2f2f2;
          }
          li:nth-of-type(1),li:nth-of-type(2){
            border-right: 1px solid #D1D1D1;
          }
        }
      }
      .form-box{
        display: -webkit-flex;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        text-align: left;
        .el-form-item{
          margin-bottom: 14px;
        }
      }
      .table-box{
        box-sizing: border-box;
        width: calc(100% - 40px);
        margin: 0 auto 20px;
        border: 1px solid #E4E4E4;
        text-align: center;
        color: #666666;
        font-size: 14px;
      }
      .search-btn{
        width: 64px;
        height: 32px;
        line-height: 7px;
        border-radius: 2px;
        border: none;
        background: -webkit-linear-gradient(#FF9C70, #FF694E); /* Safari 5.1 - 6.0 */
        background: -o-linear-gradient(#FF9C70, #FF694E); /* Opera 11.1 - 12.0 */
        background: -moz-linear-gradient(#FF9C70, #FF694E); /* Firefox 3.6 - 15 */
        background: linear-gradient(to right,#FF9C70, #FF694E); /* 标准的语法 */
        span{
          font-size: 16px;
        }
      }
    }
    /deep/ .contentBox {
      // min-width: 1400px !important;
      width: 100% !important;
      // width: calc(100% + 220px) !important;
    }
  }
</style>

 3.实现效果:

4.其他:

可以使用vue-moment处理日期,使用方法如下:

1)安装-npm install vue-moment --save

2)在main.js中引入

import Moment from 'vue-moment'

Vue.use(Moment)

 3)使用

this.$moment(obj.beginTaskDate).format('YYYY-MM-DD')

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值