datetime-picker.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. defineOptions({
  4. name: "DateTimePicker"
  5. });
  6. const size = ref("default");
  7. const dynamicSize = ref();
  8. const value = ref("");
  9. const shortcuts = [
  10. {
  11. text: "今天",
  12. value: new Date()
  13. },
  14. {
  15. text: "昨天",
  16. value: () => {
  17. const date = new Date();
  18. date.setTime(date.getTime() - 3600 * 1000 * 24);
  19. return date;
  20. }
  21. },
  22. {
  23. text: "一周前",
  24. value: () => {
  25. const date = new Date();
  26. date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
  27. return date;
  28. }
  29. }
  30. ];
  31. const value1 = ref("");
  32. const datetimeFormat = ref("");
  33. const value2 = ref("");
  34. const shortcuts1 = [
  35. {
  36. text: "上周",
  37. value: () => {
  38. const end = new Date();
  39. const start = new Date();
  40. start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
  41. return [start, end];
  42. }
  43. },
  44. {
  45. text: "上个月",
  46. value: () => {
  47. const end = new Date();
  48. const start = new Date();
  49. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
  50. return [start, end];
  51. }
  52. },
  53. {
  54. text: "三个月前",
  55. value: () => {
  56. const end = new Date();
  57. const start = new Date();
  58. start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
  59. return [start, end];
  60. }
  61. }
  62. ];
  63. const value3 = ref("");
  64. const datePickerRef = ref();
  65. const placement = ref("auto");
  66. const checkTag = ref([
  67. {
  68. title: "auto", // https://popper.js.org/docs/v2/constructors/#options
  69. checked: false
  70. },
  71. {
  72. title: "auto-start",
  73. checked: false
  74. },
  75. {
  76. title: "auto-end",
  77. checked: false
  78. },
  79. {
  80. title: "top",
  81. checked: false
  82. },
  83. {
  84. title: "top-start",
  85. checked: false
  86. },
  87. {
  88. title: "top-end",
  89. checked: false
  90. },
  91. {
  92. title: "bottom",
  93. checked: false
  94. },
  95. {
  96. title: "bottom-start",
  97. checked: false
  98. },
  99. {
  100. title: "bottom-end",
  101. checked: false
  102. },
  103. {
  104. title: "right",
  105. checked: false
  106. },
  107. {
  108. title: "right-start",
  109. checked: false
  110. },
  111. {
  112. title: "right-end",
  113. checked: false
  114. },
  115. {
  116. title: "left",
  117. checked: false
  118. },
  119. {
  120. title: "left-start",
  121. checked: false
  122. },
  123. {
  124. title: "left-end",
  125. checked: false
  126. }
  127. ]);
  128. const curTagMap = ref({});
  129. function onChecked(tag, index) {
  130. if (size.value === "disabled") return;
  131. placement.value = tag.title;
  132. curTagMap.value[index] = Object.assign({
  133. ...tag,
  134. checked: !tag.checked
  135. });
  136. checkTag.value.map(item => (item.checked = false));
  137. checkTag.value[index].checked = curTagMap.value[index].checked;
  138. // 外部触发日期时间选择面板的打开与关闭
  139. curTagMap.value[index].checked
  140. ? datePickerRef.value.handleOpen()
  141. : datePickerRef.value.handleClose();
  142. }
  143. watch(size, val =>
  144. val === "disabled"
  145. ? (dynamicSize.value = "default")
  146. : (dynamicSize.value = size.value)
  147. );
  148. </script>
  149. <template>
  150. <el-card shadow="never" :style="{ height: '100vh' }">
  151. <template #header>
  152. <div class="card-header">
  153. <el-space wrap :size="40">
  154. <el-link
  155. v-tippy="{
  156. content: '点击查看详细文档'
  157. }"
  158. href="https://element-plus.org/zh-CN/component/datetime-picker.html"
  159. target="_blank"
  160. style="font-size: 16px; font-weight: 800"
  161. >
  162. 日期时间选择器
  163. </el-link>
  164. <el-radio-group v-model="size" size="small">
  165. <el-radio value="large">大尺寸</el-radio>
  166. <el-radio value="default">默认尺寸</el-radio>
  167. <el-radio value="small">小尺寸</el-radio>
  168. <el-radio value="disabled">禁用</el-radio>
  169. </el-radio-group>
  170. </el-space>
  171. </div>
  172. </template>
  173. <p class="mb-2">日期和时间点</p>
  174. <el-date-picker
  175. v-model="value"
  176. type="datetime"
  177. class="!w-[200px]"
  178. placeholder="请选择日期时间"
  179. :shortcuts="shortcuts"
  180. :size="dynamicSize"
  181. :disabled="size === 'disabled'"
  182. />
  183. <p class="mb-2 mt-4">日期时间格式</p>
  184. <el-radio-group
  185. v-model="datetimeFormat"
  186. class="mb-2"
  187. :disabled="size === 'disabled'"
  188. @change="value1 = ''"
  189. >
  190. <el-radio value="">Date</el-radio>
  191. <el-radio value="YYYY-MM-DD HH:mm:ss">年月日 时分秒</el-radio>
  192. <el-radio value="x">时间戳</el-radio>
  193. </el-radio-group>
  194. <br />
  195. <el-space wrap>
  196. <el-date-picker
  197. v-model="value1"
  198. type="datetime"
  199. class="!w-[200px]"
  200. placeholder="请选择日期时间"
  201. format="YYYY/MM/DD hh:mm:ss"
  202. :value-format="datetimeFormat"
  203. :size="dynamicSize"
  204. :disabled="size === 'disabled'"
  205. />
  206. <span class="ml-2">{{ value1 }}</span>
  207. </el-space>
  208. <p class="mb-2 mt-4">日期和时间范围</p>
  209. <el-date-picker
  210. v-model="value2"
  211. type="datetimerange"
  212. :shortcuts="shortcuts1"
  213. range-separator="至"
  214. start-placeholder="开始日期时间"
  215. end-placeholder="结束日期时间"
  216. :popper-options="{
  217. placement: 'bottom-start'
  218. }"
  219. :size="dynamicSize"
  220. :disabled="size === 'disabled'"
  221. />
  222. <p class="mb-2 mt-4">
  223. 弹出面板位置可控(如果弹出位置不足以完整展示面板会自动调整位置)
  224. </p>
  225. <el-space wrap class="w-[400px]">
  226. <el-check-tag
  227. v-for="(tag, index) in checkTag"
  228. :key="index"
  229. :class="[
  230. 'select-none',
  231. size === 'disabled' && 'tag-disabled',
  232. tag.checked && 'is-active'
  233. ]"
  234. :checked="tag.checked"
  235. @change="onChecked(tag, index)"
  236. >
  237. {{ tag.title }}
  238. </el-check-tag>
  239. </el-space>
  240. <el-date-picker
  241. ref="datePickerRef"
  242. v-model="value3"
  243. type="datetime"
  244. class="ml-[15%]"
  245. placeholder="请选择日期时间"
  246. :popper-options="{
  247. placement
  248. }"
  249. :size="dynamicSize"
  250. :disabled="size === 'disabled'"
  251. />
  252. </el-card>
  253. </template>
  254. <style lang="scss" scoped>
  255. .tag-disabled {
  256. color: var(--el-disabled-text-color);
  257. cursor: not-allowed;
  258. background-color: var(--el-color-info-light-9);
  259. &:hover {
  260. background-color: var(--el-color-info-light-9);
  261. }
  262. &.is-active {
  263. background-color: var(--el-color-primary-light-9);
  264. }
  265. }
  266. </style>