Vue、Javascript——展示近三年的数据

效果:
默认的当年数据,左右箭头点击时分别加减一年

写法一:

<i class="el-icon-edit" @click="goLeft" v-show="yearIndex == 1 || yearIndex == 2"></i>
	 <span> {{ new Date().getFullYear() - 2 }}</span>
	 <span> {{ new Date().getFullYear() - 1 }}</span>
	 <span> {{ new Date().getFullYear()}}</span>
 <i class="el-icon-edit" @click="goRight" v-show="yearIndex == 0 || yearIndex == 1"></i>

data(){
 return{
   yearIndex: new Date().getFullYear(),
     }
},
computed: {
    year() {
      switch (this.yearIndex) {
        case 0:
          return new Date().getFullYear() - 2;
        case 1:
          return new Date().getFullYear() - 1;
        case 2:
          return new Date().getFullYear();
      }
    }
  },
  methods:{
	 goRight() {
	      ++this.yearIndex;
	    },
    goLeft() {
      --this.yearIndex;
    },
}



uni-app:
写法二:
<view class="switch flex-data">
      <uni-icons type="left" color="#999" size="20" @click="leftClick(typeTime)" :class="[showOnly === true ? 'form-onlyed' : '']"></uni-icons>
      <text class="data">{{ typeTime }}</text>
      <uni-icons type="right" color="#999" size="20" @click="rightClick(typeTime)" :class="[isShow === true ? 'form-onlyed' : '']"></uni-icons>
    </view>

data(){
 return:{
 	  showOnly: false,
      isShow: false,
      typeTime: new Date().getFullYear(),
}
},
methods:{

leftClick(v) {
      let newYear = new Date().getFullYear();
      let rightYear = newYear - 2;
      if (this.typeTime === rightYear) {
        this.showOnly = true;
        return;
      }
      this.typeTime--;
      this.getData();
    },
 rightClick(v) {
      let newYear = new Date().getFullYear();
      if (this.typeTime === newYear) {
        this.isShow = true;
        return;
      }
      this.typeTime++;
      this.getData();
    },
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值