audit-year-time.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div :class="$style.year">
  3. <a-select
  4. v-model="yearList"
  5. mode="multiple"
  6. :open="false"
  7. :value="value"
  8. :max-tag-count="4"
  9. @focus="showModel"
  10. @change="yearChange"
  11. >
  12. <template slot="maxTagPlaceholder">
  13. ...
  14. </template>
  15. <a-icon slot="suffixIcon" type="unordered-list" />
  16. <a-select-option v-for="(item, i) in yearOptionList" :key="i" :value="item.value">{{
  17. item.value
  18. }}</a-select-option>
  19. </a-select>
  20. <div v-if="show" :class="$style.yearBody">
  21. <!-- 上下两个部分 -->
  22. <!-- 上部显示当前是多少年到多少年 左右有切换按钮 -->
  23. <div :class="$style.yearTop">
  24. <a-icon
  25. type="double-left"
  26. :style="{
  27. cursor: 'pointer',
  28. color: '#ccc',
  29. }"
  30. @click="addYearOptionList('prve')"
  31. />
  32. <span>{{ showTitlenYear }}</span>
  33. <a-icon
  34. type="double-right"
  35. :style="{
  36. cursor: 'pointer',
  37. color: '#ccc',
  38. }"
  39. @click="addYearOptionList('next')"
  40. />
  41. </div>
  42. <!-- 下部显示当前年份所在的十年的年份列表 一行有三个 共四行 其中 第一个和最后一个置灰 -->
  43. <div :class="$style.yearBottom">
  44. <a-row>
  45. <a-col v-for="(item, i) in yearOptionList" :key="i" :span="8" :class="$style.colyear">
  46. <!-- 'year-item': true,
  47. 'year-item-active': yearList.includes(item.value),
  48. 'year-item-disabled': i === 0 || i === yearOptionList.length - 1, -->
  49. <div
  50. :class="[
  51. $style.yearItem,
  52. yearList.includes(item.value) ? $style.yearItemActive : '',
  53. i === 0 || i === yearOptionList.length - 1 ? $style.yearItemDisabled : '',
  54. ]"
  55. :disabled="i === 0 || i === yearOptionList.length - 1"
  56. @click="onAction(item)"
  57. >
  58. {{ item.value }}
  59. </div>
  60. </a-col>
  61. </a-row>
  62. </div>
  63. </div>
  64. </div>
  65. </template>
  66. <script>
  67. export default {
  68. name: 'AuditYearTime',
  69. metaInfo: {
  70. title: 'AuditYearTime',
  71. },
  72. model: {
  73. prop: 'value',
  74. event: 'change',
  75. },
  76. props: {
  77. value: {
  78. type: Array,
  79. default: () => [],
  80. },
  81. // 是否单选
  82. single: {
  83. type: Boolean,
  84. default: false,
  85. },
  86. defaultValue: {
  87. type: Array,
  88. default: () => [],
  89. },
  90. // 选择类型
  91. selectType: {
  92. type: String,
  93. default: 'all',
  94. },
  95. },
  96. data() {
  97. return {
  98. yearList: [],
  99. yearOptionList: [],
  100. show: false,
  101. showTitlenYear: '',
  102. onShowTitle: 0,
  103. }
  104. },
  105. watch: {
  106. value(val) {
  107. this.yearList = val
  108. },
  109. },
  110. created() {
  111. document.addEventListener('click', this.clickBody)
  112. this.yearList = this.value
  113. },
  114. methods: {
  115. addYearOptionList(type = 'on') {
  116. // on表示当前 prve表示上一个 next表示下一个
  117. if (type === 'on') {
  118. this.onShowTitle = 0
  119. } else if (type === 'prve') {
  120. this.onShowTitle -= 10
  121. } else if (type === 'next') {
  122. this.onShowTitle += 10
  123. }
  124. // showTitlenYear等于当前年份所在的十年
  125. // yearOptionLis 为年份列表
  126. const year = new Date().getFullYear() + this.onShowTitle
  127. // 计算当前年份应该减去的年份 和应该加上的年份
  128. const subYear = year - (year % 10)
  129. const addYear = subYear + 9
  130. this.showTitlenYear = `${subYear}-${addYear}`
  131. this.yearOptionList = Array.from({ length: 12 }, (v, i) => subYear - 1 + i).map((item) => {
  132. return {
  133. value: item,
  134. label: item,
  135. }
  136. })
  137. },
  138. showModel() {
  139. this.show = true
  140. this.addYearOptionList()
  141. },
  142. onAction(item) {
  143. // 如果点击的是最后一个 或者第一个 则只表示切换页
  144. if (
  145. item.value === this.yearOptionList[0].value ||
  146. item.value === this.yearOptionList[this.yearOptionList.length - 1].value
  147. ) {
  148. return this.addYearOptionList(item.value === this.yearOptionList[0].value ? 'prve' : 'next')
  149. }
  150. // 是否含有
  151. const isInclud = this.yearList.includes(item.value)
  152. if (this.yearList.includes(item.value)) {
  153. this.yearList = this.yearList.filter((i) => i !== item.value)
  154. } else {
  155. if (this.single) {
  156. this.yearList = [item.value]
  157. } else {
  158. this.yearList.push(item.value)
  159. }
  160. }
  161. if (this.selectType === 'mode') {
  162. // 最大数到最小数之间的所有年份
  163. // 给数组排序
  164. this.yearList = this.yearList.map((item) => parseInt(item))
  165. this.yearList = this.yearList.sort((a, b) => a - b)
  166. const yearData = []
  167. let min = this.yearList[0]
  168. let max = this.yearList[this.yearList.length - 1]
  169. if (isInclud) {
  170. // 取消的时候 如果大于最小值
  171. if (min < parseInt(item.value)) {
  172. max = parseInt(item.value) - 1
  173. }
  174. if (min > parseInt(item.value)) {
  175. max = min
  176. min = parseInt(item.value)
  177. }
  178. }
  179. for (let i = min; i <= max; i++) {
  180. yearData.push(i)
  181. }
  182. this.yearList = yearData
  183. }
  184. this.$emit('change', this.yearList)
  185. },
  186. // 创建监听事件,如果点击的是当前组件之外的地方,关闭下拉框
  187. clickBody(e) {
  188. if (this.show) {
  189. if (!this.$el.contains(e.target)) {
  190. this.show = false
  191. }
  192. }
  193. },
  194. yearChange(val) {
  195. this.$emit('change', val)
  196. },
  197. },
  198. }
  199. </script>
  200. <style module lang="scss">
  201. @use '@/common/design' as *;
  202. .year {
  203. position: relative;
  204. .year-body {
  205. position: absolute;
  206. top: 100%;
  207. left: 0;
  208. z-index: 11;
  209. width: 280px;
  210. // 禁止用户选择
  211. user-select: none;
  212. background-color: #fff;
  213. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  214. .year-top {
  215. display: flex;
  216. align-items: center;
  217. justify-content: space-between;
  218. padding: 0 10px;
  219. border-bottom: 1px solid #f0f0f0;
  220. }
  221. .year-bottom {
  222. padding: 10px;
  223. }
  224. }
  225. .colyear {
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. // 93 66
  230. width: 86px;
  231. height: 66px;
  232. }
  233. .year-item {
  234. display: flex;
  235. align-items: center;
  236. justify-content: center;
  237. height: 24px;
  238. padding: 0 8px;
  239. line-height: 24px;
  240. cursor: pointer;
  241. border-radius: 2px;
  242. // 鼠标放置
  243. &:hover {
  244. color: $blue-6;
  245. background-color: $blue-1;
  246. }
  247. }
  248. .year-item-active {
  249. color: $text-color-inverse;
  250. background-color: $blue-6;
  251. &:hover {
  252. color: $text-color-inverse;
  253. background-color: $blue-6;
  254. }
  255. }
  256. .year-item-disabled {
  257. color: $normal-color;
  258. }
  259. }
  260. </style>