You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
  1. <template>
  2. <el-date-picker :type="type" :style="styleObj" v-model="dateRange" :value-format="valueFormat" :format="format"
  3. unlink-panels
  4. :range-separator="rangeSepaSrator"
  5. :start-placeholder="startPlaceholder"
  6. :end-placeholder="endPlaceholder"
  7. :default-range="[-30,0]"
  8. @change="onDateRangeChange"
  9. :picker-options="pickerOptions"
  10. ></el-date-picker>
  11. </template>
  12. <script>
  13. import util from '@/common/js/util'
  14. export default {
  15. name: 'date-range',
  16. components: { },
  17. computed: {
  18. },
  19. data(){
  20. return {
  21. dateRange:[],
  22. }
  23. },
  24. watch:{
  25. dateRange(){
  26. },
  27. value:{
  28. deep:true,
  29. handler(){
  30. this.initData();
  31. }
  32. }
  33. },
  34. props: {
  35. value: {
  36. },
  37. type: {
  38. type: String,
  39. default: 'daterange'
  40. },
  41. startKey: {
  42. type: String,
  43. default: 'startTime'
  44. },
  45. styleObj:{
  46. typeof:Object,
  47. default:function(){return { maxWidth:'100%' }}
  48. },
  49. endKey: {
  50. type: String,
  51. default: 'endTime'
  52. },
  53. valueFormat: {
  54. type: String,
  55. default: 'yyyy-MM-dd HH:mm:ss'
  56. },
  57. format: {
  58. type: String,
  59. default: 'yyyy-MM-dd'
  60. },
  61. startPlaceholder: {
  62. type: String,
  63. default: '开始日期'
  64. },
  65. endPlaceholder: {
  66. type: String,
  67. default: '结束日期'
  68. },
  69. rangeSepaSrator:{
  70. type: String,
  71. default: '-'
  72. },
  73. pickerOptions:{
  74. typeof:Object,
  75. default:function(){return util.getPickerOptions('datarange')}
  76. },
  77. autoDefault:{
  78. type:Boolean,
  79. default:true,
  80. },
  81. defaultRange:{
  82. type:Array,
  83. default:function(){
  84. return [-15,15]
  85. }
  86. }
  87. },
  88. methods: {
  89. initData(){
  90. this.dateRange=[];
  91. if(this.value instanceof Array){
  92. this.dateRange=this.value
  93. }else if(this.value instanceof Object){
  94. if(this.value && this.value[this.startKey] && this.value[this.endKey]){
  95. this.dateRange=[this.value[this.startKey],this.value[this.endKey]]
  96. }
  97. }
  98. if( !this.dateRange || this.dateRange.length===0){
  99. if(this.autoDefault===true || (this.autoDefault!==false && this.defaultRange && this.defaultRange.length==2)){
  100. var now=new Date();
  101. var start=new Date();
  102. var end=new Date();
  103. start.setTime(now.getTime() + 3600 * 1000 * 24 * this.defaultRange[0]);
  104. end.setTime(now.getTime() + 3600 * 1000 * 24 * this.defaultRange[1]);
  105. this.dateRange.push(util.formatDate(start,this.valueFormat))
  106. this.dateRange.push(util.formatDate(end,this.valueFormat))
  107. this.onDateRangeChange(this.dateRange);
  108. }
  109. }
  110. },
  111. onDateRangeChange(dates){
  112. if(this.value && this.value instanceof Object){
  113. if(dates && dates.length===2){
  114. this.value[this.startKey]=dates[0]
  115. this.value[this.endKey]=dates[1]
  116. }else if(dates && dates.length===1){
  117. this.value[this.startKey]=dates[0]
  118. this.value[this.endKey]=null
  119. }else if(dates && dates.length===0){
  120. this.value[this.startKey]=null
  121. this.value[this.endKey]=null
  122. }else if(!dates){
  123. this.value[this.startKey]=null
  124. this.value[this.endKey]=null
  125. }
  126. this.$emit('input',this.value)
  127. }else if(this.value instanceof Array){
  128. this.$emit('input',dates)
  129. }
  130. this.$emit('change',dates)
  131. }
  132. },
  133. mounted(){
  134. this.initData();
  135. }
  136. }
  137. </script>
  138. <style rel="stylesheet/scss" lang="scss" scoped>
  139. </style>