当前位置:   article > 正文

vue 3.2 table 简易日历组件

vue 3.2 table 简易日历组件
<template>
  <table>
    <thead>
      <th v-for="(item, index) in title" :key="index">
        {{ item }}
      </th>
    </thead>
    <tbody>
      <tr v-for="(item, index) in trData" :key="index">
        <td
          v-for="(num, key) in item"
          :key="key"
          @click="clickMonth(num)"
          :class="{ active: choosedData.includes(num) }"
        >
          {{ num }}
        </td>
      </tr>
    </tbody>
  </table>
</template>
<script>
export default defineComponent({
  name: 'SCalendar',
  props: {
    modelValue: {
      type: Array,
      default: () => []
    },
    days: {
      type: Number,
      default: 31
    },
    groupNum: {
      type: Number,
      default: 7
    },
    title: {
      type: Array,
      default: () => ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']
    }
  },
  setup(props, { emit }) {
    const { days, groupNum, modelValue } = props
    const trData = ref([])
    const choosedData = computed({
      get: () => modelValue,
      set: (val) => {
        emit('update:modelValue', val)
      }
    })
    function init() {
      const list = Array.from({ length: days + 1 }, (_, i) => i)
      let groups = []
      for (let i = 1; i <= list.length; i += groupNum) {
        const group = list.slice(i, i + groupNum)
        groups.push(group)
      }
      trData.value = groups
    }
    function clickMonth(val) {
      const data = choosedData.value
      if (data.includes(val)) {
        const index = data.findIndex((g) => g === val)
        data.splice(index, 1)
      } else {
        data.push(val)
      }
    }
    init()
    return { trData, choosedData, clickMonth }
  }
})
</script>
<style scoped>
table {
  border-collapse: collapse;
  text-align: center;
}
th,
td {
  border: 1px solid #d9d9d9;
}

td {
  width: 50px;
  height: 50px;
  cursor: pointer;
}
.active {
  background-color: #1890ff;
  color: #fff;
}
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
 <SCalendar v-model="list" />
  • 1

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45516
推荐阅读
相关标签
  

闽ICP备14008679号