DatePicker.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <input type="text" :placeholder="placeholder">
  3. </template>
  4. <script>
  5. import moment from 'moment'
  6. import 'assets/bootstrap-datepicker-1.7.1/css/bootstrap-datepicker3.css'
  7. import 'assets/bootstrap-datepicker-1.7.1/js/bootstrap-datepicker.js'
  8. import 'assets/bootstrap-datepicker-1.7.1/locales/bootstrap-datepicker.zh-CN.min.js'
  9. export default {
  10. data() {
  11. return {
  12. flags: {},
  13. defaultFlags: "0000000000000000000000000000000"
  14. }
  15. },
  16. props: {
  17. id: {
  18. default: ""
  19. },
  20. placeholder: {
  21. default: "选择日期"
  22. },
  23. clearBtn: {
  24. type: Boolean,
  25. default: false
  26. },
  27. day: {
  28. default: ""
  29. }
  30. },
  31. mounted() {
  32. $(this.$el).datepicker({
  33. language: "zh-CN",
  34. autoclose: true,
  35. clearBtn: this.clearBtn,
  36. format: "yyyy-mm-dd",
  37. todayHighlight: true
  38. }).on("changeDate", e => {
  39. if (!e.date) {
  40. this.$emit("update:day", "");
  41. } else {
  42. this.$emit("update:day", moment(e.date).format('YYYYMMDD'));
  43. }
  44. }).on("change", () => {
  45. if (!this.$el.value) {
  46. this.$emit("update:day", "");
  47. }
  48. })
  49. if(this.day) {
  50. $(this.$el).datepicker("setDate", moment(this.day, "YYYYMMDD").toDate());
  51. }
  52. this.update();
  53. },
  54. watch: {
  55. day: function (val) {
  56. if (!val) return;
  57. let d = $(this.$el).datepicker("getDate");
  58. if (val != moment(d).format("YYYYMMDD")) {
  59. $(this.$el).datepicker("setDate", moment(val, "YYYYMMDD").toDate());
  60. }
  61. },
  62. id: function (val) {
  63. this.update();
  64. }
  65. },
  66. computed: {},
  67. methods: {
  68. update() {
  69. $(this.$el).datepicker('update');
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="less">
  75. .datepicker {
  76. z-index: 9999 !important;
  77. }
  78. </style>