123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <template>
- <div :class="$style.year">
- <a-select
- v-model="yearList"
- mode="multiple"
- :open="false"
- :value="value"
- :max-tag-count="4"
- @focus="showModel"
- @change="yearChange"
- >
- <template slot="maxTagPlaceholder">
- ...
- </template>
- <a-icon slot="suffixIcon" type="unordered-list" />
- <a-select-option v-for="(item, i) in yearOptionList" :key="i" :value="item.value">{{
- item.value
- }}</a-select-option>
- </a-select>
- <div v-if="show" :class="$style.yearBody">
- <!-- 上下两个部分 -->
- <!-- 上部显示当前是多少年到多少年 左右有切换按钮 -->
- <div :class="$style.yearTop">
- <a-icon
- type="double-left"
- :style="{
- cursor: 'pointer',
- color: '#ccc',
- }"
- @click="addYearOptionList('prve')"
- />
- <span>{{ showTitlenYear }}</span>
- <a-icon
- type="double-right"
- :style="{
- cursor: 'pointer',
- color: '#ccc',
- }"
- @click="addYearOptionList('next')"
- />
- </div>
- <!-- 下部显示当前年份所在的十年的年份列表 一行有三个 共四行 其中 第一个和最后一个置灰 -->
- <div :class="$style.yearBottom">
- <a-row>
- <a-col v-for="(item, i) in yearOptionList" :key="i" :span="8" :class="$style.colyear">
- <!-- 'year-item': true,
- 'year-item-active': yearList.includes(item.value),
- 'year-item-disabled': i === 0 || i === yearOptionList.length - 1, -->
- <div
- :class="[
- $style.yearItem,
- yearList.includes(item.value) ? $style.yearItemActive : '',
- i === 0 || i === yearOptionList.length - 1 ? $style.yearItemDisabled : '',
- ]"
- :disabled="i === 0 || i === yearOptionList.length - 1"
- @click="onAction(item)"
- >
- {{ item.value }}
- </div>
- </a-col>
- </a-row>
- </div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: 'AuditYearTime',
- metaInfo: {
- title: 'AuditYearTime',
- },
- model: {
- prop: 'value',
- event: 'change',
- },
- props: {
- value: {
- type: Array,
- default: () => [],
- },
- // 是否单选
- single: {
- type: Boolean,
- default: false,
- },
- defaultValue: {
- type: Array,
- default: () => [],
- },
- // 选择类型
- selectType: {
- type: String,
- default: 'all',
- },
- },
- data() {
- return {
- yearList: [],
- yearOptionList: [],
- show: false,
- showTitlenYear: '',
- onShowTitle: 0,
- }
- },
- watch: {
- value(val) {
- this.yearList = val
- },
- },
- created() {
- document.addEventListener('click', this.clickBody)
- this.yearList = this.value
- },
- methods: {
- addYearOptionList(type = 'on') {
- // on表示当前 prve表示上一个 next表示下一个
- if (type === 'on') {
- this.onShowTitle = 0
- } else if (type === 'prve') {
- this.onShowTitle -= 10
- } else if (type === 'next') {
- this.onShowTitle += 10
- }
- // showTitlenYear等于当前年份所在的十年
- // yearOptionLis 为年份列表
- const year = new Date().getFullYear() + this.onShowTitle
- // 计算当前年份应该减去的年份 和应该加上的年份
- const subYear = year - (year % 10)
- const addYear = subYear + 9
- this.showTitlenYear = `${subYear}-${addYear}`
- this.yearOptionList = Array.from({ length: 12 }, (v, i) => subYear - 1 + i).map((item) => {
- return {
- value: item,
- label: item,
- }
- })
- },
- showModel() {
- this.show = true
- this.addYearOptionList()
- },
- onAction(item) {
- // 如果点击的是最后一个 或者第一个 则只表示切换页
- if (
- item.value === this.yearOptionList[0].value ||
- item.value === this.yearOptionList[this.yearOptionList.length - 1].value
- ) {
- return this.addYearOptionList(item.value === this.yearOptionList[0].value ? 'prve' : 'next')
- }
- // 是否含有
- const isInclud = this.yearList.includes(item.value)
- if (this.yearList.includes(item.value)) {
- this.yearList = this.yearList.filter((i) => i !== item.value)
- } else {
- if (this.single) {
- this.yearList = [item.value]
- } else {
- this.yearList.push(item.value)
- }
- }
- if (this.selectType === 'mode') {
- // 最大数到最小数之间的所有年份
- // 给数组排序
- this.yearList = this.yearList.map((item) => parseInt(item))
- this.yearList = this.yearList.sort((a, b) => a - b)
- const yearData = []
- let min = this.yearList[0]
- let max = this.yearList[this.yearList.length - 1]
- if (isInclud) {
- // 取消的时候 如果大于最小值
- if (min < parseInt(item.value)) {
- max = parseInt(item.value) - 1
- }
- if (min > parseInt(item.value)) {
- max = min
- min = parseInt(item.value)
- }
- }
- for (let i = min; i <= max; i++) {
- yearData.push(i)
- }
- this.yearList = yearData
- }
- this.$emit('change', this.yearList)
- },
- // 创建监听事件,如果点击的是当前组件之外的地方,关闭下拉框
- clickBody(e) {
- if (this.show) {
- if (!this.$el.contains(e.target)) {
- this.show = false
- }
- }
- },
- yearChange(val) {
- this.$emit('change', val)
- },
- },
- }
- </script>
- <style module lang="scss">
- @use '@/common/design' as *;
- .year {
- position: relative;
- .year-body {
- position: absolute;
- top: 100%;
- left: 0;
- z-index: 11;
- width: 280px;
- // 禁止用户选择
- user-select: none;
- background-color: #fff;
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
- .year-top {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 0 10px;
- border-bottom: 1px solid #f0f0f0;
- }
- .year-bottom {
- padding: 10px;
- }
- }
- .colyear {
- display: flex;
- align-items: center;
- justify-content: center;
- // 93 66
- width: 86px;
- height: 66px;
- }
- .year-item {
- display: flex;
- align-items: center;
- justify-content: center;
- height: 24px;
- padding: 0 8px;
- line-height: 24px;
- cursor: pointer;
- border-radius: 2px;
- // 鼠标放置
- &:hover {
- color: $blue-6;
- background-color: $blue-1;
- }
- }
- .year-item-active {
- color: $text-color-inverse;
- background-color: $blue-6;
- &:hover {
- color: $text-color-inverse;
- background-color: $blue-6;
- }
- }
- .year-item-disabled {
- color: $normal-color;
- }
- }
- </style>
|