index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="flex gap-2 date-search" :class="{'justify-end': position === 'right', 'justify-start': position === 'left', 'justify-center': position === 'center'}">
  3. <div class="quick-select">
  4. <el-button
  5. :type="activeQuickSelect === 7 ? 'primary' : ''"
  6. @click="handleQuickSelect(7)"
  7. >7日</el-button
  8. >
  9. <el-button
  10. :type="activeQuickSelect === 14 ? 'primary' : ''"
  11. @click="handleQuickSelect(14)"
  12. >15日</el-button
  13. >
  14. <el-button
  15. :type="activeQuickSelect === 30 ? 'primary' : ''"
  16. @click="handleQuickSelect(30)"
  17. >30日</el-button
  18. >
  19. </div>
  20. <el-date-picker
  21. v-model="dateRange"
  22. type="daterange"
  23. range-separator="至"
  24. start-placeholder="开始日期"
  25. end-placeholder="结束日期"
  26. :disabled-date="disabledDate"
  27. value-format="YYYY-MM-DD"
  28. @change="handleDateChange"
  29. style="max-width: 320px"
  30. />
  31. <el-button type="primary" @click="handleSearch">搜索</el-button>
  32. <el-button @click="handleReset">重置</el-button>
  33. </div>
  34. </template>
  35. <script setup>
  36. import { ref, watch, nextTick } from "vue";
  37. import dayjs from "dayjs";
  38. const props = defineProps({
  39. modelValue: {
  40. type: Array,
  41. default: () => [],
  42. },
  43. position: {
  44. type: String,
  45. default: "right",
  46. },
  47. });
  48. const emit = defineEmits(["update:modelValue", "search"]);
  49. const dateRange = ref(props.modelValue);
  50. const activeQuickSelect = ref(7); // 默认选中7日
  51. // 监听外部值变化
  52. watch(
  53. () => props.modelValue,
  54. (newVal) => {
  55. dateRange.value = newVal;
  56. },
  57. { deep: true }
  58. );
  59. // 监听内部值变化
  60. watch(
  61. dateRange,
  62. (newVal) => {
  63. emit("update:modelValue", newVal);
  64. },
  65. { deep: true }
  66. );
  67. // 快捷选项点击
  68. const handleQuickSelect = (days) => {
  69. activeQuickSelect.value = days;
  70. const end = dayjs();
  71. const start = dayjs().subtract(days, "day");
  72. const newDateRange = [start.format("YYYY-MM-DD"), end.format("YYYY-MM-DD")];
  73. // 先更新日期范围
  74. dateRange.value = newDateRange;
  75. // 使用 nextTick 确保数据更新后再触发搜索
  76. nextTick(() => {
  77. emit("search", newDateRange);
  78. });
  79. };
  80. // 日期选择变化
  81. const handleDateChange = (val) => {
  82. // 清除快捷选项的选中状态
  83. activeQuickSelect.value = null;
  84. // 使用 nextTick 确保数据更新后再触发搜索
  85. nextTick(() => {
  86. emit("search", val);
  87. });
  88. };
  89. // 搜索按钮点击
  90. const handleSearch = () => {
  91. emit("search", dateRange.value);
  92. };
  93. // 重置按钮点击
  94. const handleReset = () => {
  95. // 重置为默认7天
  96. handleQuickSelect(7);
  97. };
  98. // 禁用未来日期
  99. const disabledDate = (time) => {
  100. return dayjs(time).isAfter(dayjs(), "day");
  101. };
  102. </script>