YearPicker.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <select
  3. v-model="year"
  4. class="m-select form-control"
  5. :readonly="readonly"
  6. style="width:100%"
  7. >
  8. <option
  9. v-for="item in years"
  10. :key="item"
  11. :value="item"
  12. >
  13. {{ item }}
  14. </option>
  15. </select>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'YearPicker',
  20. props: {
  21. 'modelValue':
  22. {
  23. type: String,
  24. default: '',
  25. },
  26. 'readonly':{
  27. type: Boolean,
  28. default: false,
  29. },
  30. },
  31. emits: ['update:modelValue'],
  32. data: function () {
  33. return {
  34. start: 1990,
  35. end: 2050,
  36. years: [],
  37. year: '',
  38. };
  39. },
  40. watch: {
  41. 'year': function () {
  42. this.$emit('update:modelValue', this.year);
  43. },
  44. 'modelValue': {
  45. handler: function (newValue, oldValue) { // eslint-disable-line
  46. this.year = newValue;
  47. },
  48. immediate: true,
  49. },
  50. },
  51. mounted: function () {
  52. this.initYear();
  53. },
  54. methods: {
  55. /**
  56. * 初始化时间年份列表
  57. */
  58. initYear: function () {
  59. var _self = this;
  60. if (!_self.end) {
  61. _self.end = new Date().getFullYear();
  62. }
  63. for (var i = _self.start; i <= _self.end; i++) {
  64. _self.years.push(i);
  65. }
  66. _self.years.sort(function (a, b) {
  67. return b - a;
  68. });
  69. },
  70. /**
  71. * 判断年份是否存在,如果年不存在就添加
  72. */
  73. checkYear: function (value) {
  74. if (value != null && value.length == 4 && this.years.indexOf(value) < 0) {
  75. this.years.push(value);
  76. }
  77. },
  78. },
  79. };
  80. </script>
  81. <style scoped>
  82. .m-select {
  83. border-radius: 4px !important;
  84. }
  85. </style>