• vue2中年份季度选择器(需要安装element)


    调用

    1. <QuarterCom v-model="quart" clearable default-current/>

    组件代码

    1. <template>
    2. <div>
    3. <span style="margin-right: 10px">{{ label }}span>
    4. <mark
    5. style="position:fixed;top:0;bottom:0;left:0;right:0;background:rgba(0,0,0,0);z-index:999;"
    6. v-show="showSeason"
    7. @click.stop="showSeason=false"
    8. >mark>
    9. <el-input placeholder="请选择季度" :size="size" v-model="showValue" prefix-icon="el-icon-date"
    10. :style="{width}" @focus="showSeason=true" readonly :clearable="true">
    11. <i slot="suffix" class="el-input__icon el-icon-close pointer hover_blue" v-if="showValue && clearable" @click="clearAll">i>
    12. el-input>
    13. <el-card
    14. class="box-card"
    15. v-show="showSeason"
    16. >
    17. <div slot="header" class="clearfix" style="text-align:center;padding:0">
    18. <button
    19. type="button"
    20. aria-label="前一年"
    21. class="el-picker-panel__icon-btn el-date-picker__prev-btn el-icon-d-arrow-left pointer"
    22. @click="editYear()"
    23. >button>
    24. <el-date-picker
    25. size="mini"
    26. type="year"
    27. v-model="year"
    28. format="yyyy"
    29. placeholder="选择年">
    30. el-date-picker>
    31. <button
    32. type="button"
    33. aria-label="后一年"
    34. @click="editYear(1)"
    35. class="el-picker-panel__icon-btn el-date-picker__next-btn el-icon-d-arrow-right pointer"
    36. >button>
    37. div>
    38. <div class="season_box">
    39. <div
    40. v-for="(item, index) in list"
    41. :key="index"
    42. :class="['season_box_one', 'hover_highlight',index === quarter ? 'active_highlight' : '']"
    43. @click="quarter = index"
    44. >{{ item.label }}div>
    45. div>
    46. el-card>
    47. div>
    48. template>
    49. <script>
    50. /**
    51. * @desc: defaultCurrent可以默认当前年,当前季度
    52. * @desc: 返回格式可以自行修改,默认2023年第一季度
    53. * @desc: 需要element组件支持
    54. * @desc: @dateChange 获取年季度对象数据
    55. */
    56. export default {
    57. name: 'QuarterDate',
    58. props: {
    59. value: String,
    60. // 标签
    61. label: {
    62. default: '季度',
    63. type: String
    64. },
    65. // medium / small / mini
    66. size: {
    67. default: 'medium',
    68. type: String
    69. },
    70. // medium / small / mini
    71. clearable: {
    72. default: false,
    73. type: Boolean
    74. },
    75. // 宽度
    76. width: {
    77. default: '190px',
    78. type: String
    79. },
    80. // 默认当前年当前季度
    81. defaultCurrent: {
    82. default: false,
    83. type: Boolean
    84. }
    85. },
    86. computed: {
    87. showText(){
    88. return new Date(this.year).getFullYear() + '年' + this.list[this.quarter].text
    89. }
    90. },
    91. data() {
    92. return {
    93. list: [
    94. {label: 'Q1', value: 0, text: '第一季度'},
    95. {label: 'Q2', value: 1, text: '第二季度'},
    96. {label: 'Q3', value: 2, text: '第三季度'},
    97. {label: 'Q4', value: 3, text: '第四季度'}
    98. ],
    99. showSeason: false,
    100. year: new Date(), // 年
    101. quarter: 0, // 默认季节
    102. showValue: this.value,
    103. }
    104. },
    105. watch: {
    106. showText(nv){
    107. this.showValue = nv
    108. this.$emit('input', nv)
    109. this.$emit('dateChange', {
    110. year: new Date(this.year).getFullYear(),
    111. quarter: this.list[this.quarter]
    112. })
    113. },
    114. showValue: {
    115. handler(nv){
    116. // 双向绑定数据回显
    117. if(this.value !== this.showText){
    118. const year = Number(nv.substring(0,4))
    119. try {
    120. this.list.forEach((item, index) => {
    121. if(nv.includes(item.text)) {
    122. this.quarter = index
    123. throw true
    124. }
    125. })
    126. }catch (e) {
    127. }
    128. if( year > 1970 ) {
    129. this.year = new Date(year, 0, 1)
    130. } else if(this.defaultCurrent){
    131. this.year = new Date()
    132. this.quarter = Math.ceil((new Date().getMonth() + 1)/3)
    133. }
    134. }
    135. },
    136. immediate: true
    137. },
    138. },
    139. methods: {
    140. editYear(years = -1) {
    141. this.year = new Date(new Date(this.year).getFullYear() + years, 0, 1)
    142. },
    143. clearAll(){
    144. this.showValue = ''
    145. }
    146. }
    147. }
    148. script>
    149. <style scoped>
    150. .box-card {
    151. width:322px;
    152. padding: 0 3px 20px;
    153. margin-top:10px;
    154. z-index:1999;
    155. position: absolute;
    156. }
    157. .season_box_one {
    158. color: #606266;
    159. width: 30px;
    160. height: 30px;
    161. line-height: 30px;
    162. text-align: center;
    163. cursor: pointer;
    164. border-radius: 50%;
    165. }
    166. .hover_highlight:hover {
    167. background-color: #409dfe;
    168. color: white !important;
    169. }
    170. .active_highlight {
    171. background-color: #409dfe;
    172. color: white !important;
    173. position: relative;
    174. }
    175. .active_highlight::before {
    176. width: 6px;
    177. height: 6px;
    178. content: '';
    179. background-color: #409dfe;
    180. border-radius: 50%;
    181. position: absolute;
    182. left: calc(50% - 3px);
    183. bottom: -12px;
    184. }
    185. .season_box {
    186. text-align: center;
    187. height: 30px;
    188. display: grid;
    189. width: 100%;
    190. grid-template-columns: repeat(4, 30px);
    191. gap: 30px;
    192. justify-content: center;
    193. align-items: center;
    194. }
    195. .hover_blue:hover {
    196. color: #409dfe;
    197. }
    198. .pointer {
    199. cursor: pointer;
    200. }
    201. style>

  • 相关阅读:
    MySQL使用CASE WHEN统计SQL语句代替子查询SQL统计,CASE WHEN常用写法,根据不同的条件对数据进行分类、分组和聚合
    程 控 电 源1761程控模块电源
    register_chrdev和cdev_init区别
    Spring Bean 详解
    Spring Boot实践 --windows环境下 K8s 部署 Docker
    聊聊Go里面的闭包
    金仓数据库 KingbaseES 插件参考手册(22. dbms_sql)
    表压0.1~0.6MPa范围内0.1%超高精度压力控制解决方案及其考核试验结果
    制作Go程序的Docker容器(以及容器和主机的网络问题)
    【数据结构】八大排序算法(内含思维导图和画图分析)
  • 原文地址:https://blog.csdn.net/qq_30306717/article/details/133136665