byDatePicker.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <el-date-picker
  3. v-model="value"
  4. :type="attrs.type"
  5. :placeholder="attrs.placeholder?attrs.placeholder:'请选择'"
  6. :readonly="attrs.readonly"
  7. :disabled="attrs.disabled"
  8. :clearable="attrs.clearable"
  9. :editable="attrs.editable?false:true"
  10. :format="attrs.format"
  11. :align="attrs.align"
  12. @change="onChange"
  13. >
  14. </el-date-picker>
  15. </template>
  16. <script lang="ts">
  17. /**
  18. config:{
  19. attr:{
  20. type:year/month/date //显示类型
  21. readonly:true/false //是否只读
  22. disabled:true/false //是否禁用
  23. editable:true/false //可输入
  24. clearable: true/false //是否清空
  25. placeholder:'' //占位符
  26. format:'' 格式
  27. align: left, center, right //对齐方式
  28. }
  29. }
  30. */
  31. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  32. import Format from '@/benyun/utils/dateFormat'
  33. import VueViews from '@/benyun/compVue/VueViews'
  34. @Component
  35. export default class ByDatePicker extends VueViews {
  36. @Watch('propValue')
  37. propValueChange(v:any){
  38. this.setValue(v);
  39. }
  40. created(){
  41. if(this.propConfig){
  42. this.setConfig(this.propConfig)
  43. }
  44. if(this.propValue){
  45. this.setValue(this.propValue)
  46. }
  47. }
  48. mounted(){
  49. this.defaultHandle()
  50. }
  51. setValue(data:any){
  52. if(data){
  53. this.value = (this as any).$lodash.cloneDeep(data);
  54. }else{
  55. this.value = '';
  56. }
  57. }
  58. getValue(){
  59. return (this as any).$lodash.cloneDeep(this.value);
  60. }
  61. // 清空数据
  62. clearValue(){
  63. this.value = '';
  64. }
  65. defaultHandle(){
  66. if(this.attrs.defaultNow){
  67. this.value = this.setFormat(new Date());
  68. this.onChange();
  69. }
  70. }
  71. setFormat(v:any){
  72. if(!v){
  73. return v;
  74. }
  75. let val:any = '';
  76. let type = 'yyyy-MM-dd';
  77. if(this.attrs.type == 'date'){
  78. type = 'yyyy-MM-dd'
  79. }else if(this.attrs.type == 'datetime'){
  80. type = 'yyyy-MM-dd HH:mm:ss'
  81. }else if(this.attrs.type == 'year'){
  82. type = 'yyyy'
  83. }else if(this.attrs.type == 'month'){
  84. type = 'yyyy-MM'
  85. }else if(this.attrs.type == 'dates'){
  86. type = 'yyyy-MM-dd'
  87. }else if(this.attrs.type == 'week'){
  88. type = 'yyyy 第 WW 周'
  89. }else if(this.attrs.type == 'datetimerange'){
  90. type = 'yyyy-MM-dd HH:mm:ss'
  91. }else if(this.attrs.type == 'daterange'){
  92. type = 'yyyy-MM-dd'
  93. }else if(this.attrs.type == 'monthrange'){
  94. type = 'yyyy-MM'
  95. }
  96. if(this.attrs.type == 'dates'
  97. || this.attrs.type == 'datetimerange'
  98. || this.attrs.type == 'daterange'
  99. || this.attrs.type == 'monthrange'){
  100. if(v && v.constructor == Array){
  101. val = []
  102. for(const item of v){
  103. val.push(Format(item,type))
  104. }
  105. }else{
  106. val = Format(v,type);
  107. }
  108. }else{
  109. val = Format(v,type)
  110. }
  111. return val;
  112. }
  113. onChange(){
  114. this.value = this.setFormat(this.value);
  115. this.$emit('onChange',this.value);
  116. }
  117. }
  118. </script>
  119. <style lang="scss" scoped>
  120. </style>