DateRange.vue 3.96 KB
<template>
	<div class="date-range">
		<el-date-picker
	      v-model="dateRange"
	      type="daterange"
	      size="small"
	      :clearable="false"
	      :editable="false"
	      :unlink-panels="true"
	      :picker-options="{
	      	disabledDate(time){
	      		return time.getTime() > new Date()
	      	}
	      }"
	     
	      range-separator="至"
	      start-placeholder="开始日期"
	      end-placeholder="结束日期">
	    </el-date-picker>
		<el-button-group >

		  	<el-button size="small" 
		  		:type="getButtonType('all')"
		  		v-if="showBtn('all')"
		  		plain
		  		@click.native="chooseType('all')"
		  	>全部</el-button>

			<el-button size="small" 
		  		:type="getButtonType('today')" 
		  		v-if="showBtn('today')"
		  		plain
		  		@click.native="chooseType('today')" 
		  	>今天</el-button>

			<el-button size="small" 
		  		:type="getButtonType('yesterday')" 
		  		v-if="showBtn('yesterday')"
		  		plain
		  		@click.native="chooseType('yesterday')" 
		  	>昨日</el-button>

			<el-button size="small" 
		  		:type="getButtonType('last3')" 
		  		v-if="showBtn('last3')"
		  		plain
		  		@click.native="chooseType('last3')"
		  	>近3天</el-button>

		  	<el-button size="small" 
		  		:type="getButtonType('last7')" 
		  		v-if="showBtn('last7')"
		  		plain
		  		@click.native="chooseType('last7')"
		  	>近7天</el-button>

		  	<el-button size="small" 
		  		:type="getButtonType('last15')" 
		  		v-if="showBtn('last15')"
		  		plain
		  		@click.native="chooseType('last15')"
		  	>近15天</el-button>

			<el-button size="small" 
		  		:type="getButtonType('thisMonth')" 
		  		v-if="showBtn('thisMonth')"
		  		plain
		  		@click.native="chooseType('thisMonth')"
		  	>本月</el-button>

		  	<el-button size="small" 
		  		:type="getButtonType('lastMonth')" 
		  		v-if="showBtn('lastMonth')"
		  		plain
		  		@click.native="chooseType('lastMonth')"
		  	>上月</el-button>

			<el-button size="small" 
		  		:type="getButtonType('last30')" 
		  		v-if="showBtn('last30')"
		  		plain
		  		@click.native="chooseType('last30')"
		  	>近30天</el-button>

		</el-button-group>
	</div>
</template>

<script>
export default {
	props: {
  		date: {
  			type: Array
  		},
  		options: {
  			type: Array
  		},
  		activeType: {
  			type: String,
  			default: ''
  		}
  	},
  	data(){
	    return {
	      	dateRange: this.date,
	      	type: this.activeType
	    }
	},
	watch: {
		dateRange(newVal){
			if(newVal.length){
				newVal[0] = moment(newVal[0])
				newVal[1] = moment(newVal[1])
			}
			this.$emit('onDateRangeChange', newVal)
		}
	},
  	created(){
  		this.getDateRange(this.activeType)
  	},
  	methods: {
  		getButtonType(type){
  			if(type==this.type){
  				return 'primary'
  			}
  		},
	  	showBtn(type){
			return !this.options || (this.options&&this.options.indexOf(type)>-1)
		},
		chooseType(type){
			this.type = type;
			this.getDateRange(type)
		},
		getDateRange(type){

			switch(type){
				case('today'):
					this.dateRange = [moment(),moment()]
					break;
				case('yesterday'):
					this.dateRange = [moment().subtract(1, 'days'),moment().subtract(1, 'days')]
					break;
				case('last3'):
					this.dateRange = [moment().subtract(2, 'days'),moment()]
					break;
				case('last7'):
					this.dateRange = [moment().subtract(6, 'days'),moment()]
					break;
				case('thisMonth'):
					this.dateRange = [moment().startOf('month'), moment().endOf('month')]
					break;
				case('lastMonth'):
					this.dateRange = [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
					break;
				case('last30'):
					this.dateRange = [moment().subtract(29, 'days'),moment()]
					break;
				case('last15'):
					this.dateRange = [moment().subtract(14, 'days'),moment()]
					break;
				default:
					this.dateRange = [];
			}
		},
		reset(){
			this.type = this.activeType;
			this.dateRange = []
		}
  	},
  	components: {
		
	}
}
</script>
<style scoped>
	.date-range, .el-date-editor{
		vertical-align: middle;
	}
</style>