vue日期左右可切换组件

这是一个基于Vue.js和Element-UI的自定义组件,用于创建一个可以左右滑动切换的日期选择器。组件支持两种数据源格式,既可以直接使用日期字符串,也可以使用包含日期字段的对象数组。用户可以通过点击左右箭头来滚动选择日期,并通过`selectedData`事件返回选中的日期。默认值可以通过`defaultValue`属性设置,`labelName`属性用于指定数据源中表示日期的字段名。
摘要由CSDN通过智能技术生成

基于vue与element-ui封装的可以左右切换的日期组件

<template>
  <div class="calendar-flex">
    <div class="el-icon-arrow-left calendar-arrow" @click="arrowLeft" />
    <div ref="refTotal" class="calendar-date">
      <div
        v-for="(item,index) in dataSource"
        ref="refDate"
        :key="index"
        :class="selectedItem=== (labelName? item[labelName] : item)? 'calendar-item-active':'calendar-item'"
        @click="getSelectedValue(item, index)"
      >{{ labelName?item[labelName]:item }}</div>
    </div>
    <div class="el-icon-arrow-right calendar-arrow" @click="arrowright" />
  </div>
</template>

<script>
export default {
  props: {
    // 传入的数据源
    dataSource: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 默认选中值
    defaultValue: {
      type: String,
      default: ''
    },
    // labelName名称,遍历数组是对象必须配置该值,默认遍历文本
    labelName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      selectedItem: '' // 选中值
    }
  },
  watch: {
    // 默认值变化赋值
    defaultValue: {
      handler() {
        this.selectedItem = this.defaultValue
      },
      immediate: true
    },
    // 监听选中值变化
    selectedItem: {
      handler(val) {
        const index = this.dataSource.findIndex((item) => {
          return this.labelName ? item[this.labelName] === val : item === val
        })
        index < 0 ? this.$emit('selectedData') : this.$emit('selectedData', this.dataSource[index])
      },
      immediate: true
    }
  },
  mounted() {
    const index = this.dataSource.findIndex((item) => {
      return this.labelName ? item[this.labelName] === this.defaultValue : item === this.defaultValue
    })
    if (this.defaultValue) {
      this.$refs.refTotal.scrollLeft = this.$refs.refDate[index].offsetWidth * index
    }
  },
  methods: {
    // 选中值
    getSelectedValue(data, index) {
      this.selectedItem = this.labelName ? data[this.labelName] : data
      this.$refs.refTotal.scrollLeft = this.$refs.refDate[index].offsetWidth * index
    },
    // 左箭头
    arrowLeft() {
      let scrollIndex = this.dataSource.findIndex((item) => {
        return this.labelName ? item[this.labelName] === this.selectedItem : item === this.selectedItem
      })
      if (!this.selectedItem) { // 没有默认值,赋值
        scrollIndex = 0
        this.selectedItem = this.labelName ? this.dataSource[0][this.labelName] : this.dataSource[0]
      }
      if ((this.labelName ? this.dataSource[scrollIndex][this.labelName] : this.dataSource[scrollIndex]) === (this.labelName ? this.dataSource[0][this.labelName] : this.dataSource[0])) return
      scrollIndex--
      this.selectedItem = this.labelName ? this.dataSource[scrollIndex][this.labelName] : this.dataSource[scrollIndex]
      this.$refs.refTotal.scrollLeft = this.$refs.refDate[scrollIndex].offsetWidth * scrollIndex
    },
    // 右箭头
    arrowright() {
      let scrollIndex = this.dataSource.findIndex((item) => {
        return this.labelName ? item[this.labelName] === this.selectedItem : item === this.selectedItem
      })
      if (!this.selectedItem) { // 没有默认值,赋值
        scrollIndex = 0
        this.selectedItem = this.labelName ? this.dataSource[0][this.labelName] : this.dataSource[0]
      }
      if ((this.labelName ? this.dataSource[scrollIndex][this.labelName] : this.dataSource[scrollIndex]) === (this.labelName ? this.dataSource[this.dataSource.length - 1][this.labelName] : this.dataSource[this.dataSource.length - 1])) return
      scrollIndex++
      this.selectedItem = this.labelName ? this.dataSource[scrollIndex][this.labelName] : this.dataSource[scrollIndex]
      this.$refs.refTotal.scrollLeft = this.$refs.refDate[scrollIndex].offsetWidth * scrollIndex
    }
  }
}
</script>

<style lang="scss" scoped>
.calendar-flex {
  display: flex;
  margin: 10px 0;
  .calendar-arrow {
    cursor: pointer;
    color: #409EFF;
    font-size: 18px;
    padding: 10px 20px;
  }
  .calendar-arrow:active {
    transform: scale(0.85);
  }
  .calendar-date {
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    scrollbar-width: none;
    -ms-overflow-style: none;
    .calendar-item {
      padding:10px 20px;
      cursor: pointer;
    }
    .calendar-item:hover {
      background-color:#f8f8f8;
      opacity: 0.8
    }
    .calendar-item-active {
      background-color: #409EFF;
      padding:10px 20px;
      color: #fff;
      cursor: pointer;
    }
  }
  .calendar-date::-webkit-scrollbar {
    display: none;
  }
}
</style>

引用组件支持两种数据源格式

# 基于vue和element-ui,点击左右箭头及选中滚动
基础的用法
#
# selectedData function 选中及当前滚动选中的事件
# defaultValue string 默认选中的值, 可选
# dataSource array 数据源
# labelName string 数据源中的要显示的元素名称
#
dataSource = ['2022-08-01','2022-08-02','2022-08-03','2022-08-04'] dataSource数组中的元素为字符串,可以直接这样使用,不设置默认值,没有选中值,点击左右箭头时会取值数据源中的第一个值
<scroll-arrow 
 @selectedData="selectedData" 
 :data-source="dataSource"
/>
# dataSource 数组元素是对象的用法,此时labelName必须配置
dataSource=[{date:'2022-08-01'},{date:'2022-08-02'},{date:'2022-08-03'},{date:'2022-08-04'}]
<scroll-arrow 
 @selectedData="selectedData" 
 :data-source="dataSource"
 :label-name="date"
/>

效果图如下:
日期可左右切换组件

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值