Vue自定义季度选择框控件

项目需求要有季度选择控件,由于ElementUI没有该控件,需要自己封装。

实现效果:

实现思路:

        el-popover结合el-input实现季度框的显示和隐藏,table表格形式显示四个季度,上一年和下一年事件分别将当前年进行减和加操作。

实现代码:

    <div>
        <div
                class="mask-wrapper"
                v-show="showSeason"
                @click.stop="showSeason = false"
        ></div>
        <el-popover placement="bottom" popper-class="quarter-popover-custom" trigger="manual" v-model="showSeason">

            <div class="season-picker-box">
                <div class="el-date-picker__header el-date-picker__header--bordered">
                    <i
                            aria-label="前一年"
                            class="el-picker-panel__icon-btn el-date-picker__prev-btn el-icon-d-arrow-left"
                            @click="prev"
                    />
                    <span role="button" class="el-date-picker__header-label" >{{ year }}年</span>
                    <i
                            aria-label="后一年"
                            @click="next"
                            class="el-picker-panel__icon-btn el-date-picker__next-btn el-icon-d-arrow-right"
                    />
                </div>

                <div class="el-picker-panel__content">
                    <table class="el-month-table">
                        <tr>
                            <td :class="{'today': current === 1}">
                                <div>
                                    <a class="cell" @click="selectSeason(1)">一季度</a>
                                </div>
                            </td>
                            <td :class="{'today': current === 2}">
                                <div>
                                    <a class="cell" @click="selectSeason(2)">二季度</a>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td :class="{'today': current === 3}">
                                <div>
                                    <a class="cell" @click="selectSeason(3)">三季度</a>
                                </div>
                            </td>
                            <td :class="{'today': current === 4}">
                                <div>
                                    <a class="cell" @click="selectSeason(4)">四季度</a>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

            <el-input
                    slot="reference"
                    placeholder="请选择季度"
                    v-model="strValue"
                    style="width: 12rem"
                    prefix-icon="el-icon-date"
                    @focus="showSeason = true"
            />
        </el-popover>
    </div>
         model: {
            prop: 'quarterValue',
            event: 'quarterChange'
         },
         props: {
            quarterValue: '',
            resetvalue:""
         },

         data() {
            return {
                showSeason: false,
                year: new Date().getFullYear(),
                strValue: '',
                current: ''
            };
         },

         watch: {
            showSeason(val) {
                if (val) {
                   this.current = this.quarterValue ? Number(this.quarterValue.split('-')[1]) : ''
                }
            },
        },

        methods: {
            resetfn(){
                this.strValue=''
            },
            prev() {
                this.year = this.year * 1 - 1;
            },

            next() {
                this.year = this.year * 1 + 1;
            },

            selectSeason(i) {
                this.season = `${this.year}-${i}`;
                this.$emit('quarterChange', this.season);
                this.showSeason = false;
                const cnNum = ['一', '二', '三', '四']
                this.strValue = `${this.year}年${cnNum[i-1]}季度`;
            }
        },
<style lang="scss" scoped>
    .quarter-popover-custom {
        padding: 0;
        width: 12rem !important;
    }
    .mask-wrapper {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, 0);
        z-index: 999;
    }

    .season-picker-box {
        margin-bottom: 0;
        border-bottom: 1px solid #ebeef5;
        .el-month-table {
            width: 100%;
        }
        .el-month-table td .cell {
            width: 80px;
        }
    }

    .box-card {
        width: 240px;
        padding: 0 3px;
        margin-top: 10px;
        position: fixed;
        z-index: 9999;

        .item {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            .el-button {
                width: 80px;
                margin: 5px;
                color: #606266;
                &:hover {
                    color: #3498db;
                }
            }
        }
    }
</style>

引入组件:

组件赋值:

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值