输出选定日期之间的日期,通过使用 indexOf。

huangapple go评论68阅读模式
英文:

Output dates between selected days via indexOf

问题

"between" hover all days of the month (css styles are written)
:class="{ between: between.indexOf(date)} "
what i do wrong

"between"悬停在月份的所有日期上(已编写CSS样式)。
:class="{ between: between.indexOf(date)} "
我做错了什么

英文:

I make a datepicker on vue3, When choosing two dates, I need the days between the selected dates to be hover. I use the "indexOf" method, but the desired result is not obtained

<div
            v-for="date in daysInMonth(currentYear, currentMonthInNumber, firstDay, lastDay)"
            :key="date"
            ref="date"
            class="day"
            :class="{ active: date === firstDay || date === lastDay, between: between.indexOf(date)}"
            @click="choosingDates(date)" > {{ date }} </div>
<script>
      firstDay: false,
      between: [],
      lastDay: false,
      firstDaySelected: false,
    };
  },
  methods: {
    choosingDates(date) {
      if (this.firstDay === false) {
        this.firstDay = date;
      } else if (this.lastDay === false) {
        this.lastDay = date;;
      }
    },

With this code, "between" hover all days of the month (css styles are written)
:class="{ between: between.indexOf(date)} "
what i do wrong

答案1

得分: 1

请参考以下翻译:

<template>
  <div class="datepicker">
    <div class="month-header">
      <i class="prev-month" @click="prevMonth"></i>
      {{ currentMonth }} {{ currentYear }}
      <i class="next-month" @click="nextMonth"></i>
    </div>
    <div class="weekdays">
      <div class="weekday" v-for="weekday in weekdays" :key="weekday">
        {{ weekday }}
      </div>
    </div>
    <div class="days">
      <div
        v-for="date in daysInMonth(
          currentYear,
          currentMonthInNumber,
          firstDay,
          lastDay
        )"
        :key="date"
        class="day"
        :class="{
          active: date === firstDay || date === lastDay,
          between: between.includes(date),
        }"
        @click="chooseDate(date)"
      >
        {{ date }}
      </div>
    </div>
    {{ between }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      weekdays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
      months: [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
      ],
      currentMonth: "",
      currentMonthInNumber: "",
      currentYear: "",
      firstDay: false,
      between: [],
      lastDay: false,
    };
  },
  mounted() {
    const date = new Date();
    this.currentMonth = this.months[date.getMonth()];
    this.currentMonthInNumber = date.getMonth();
    this.currentYear = date.getFullYear();
  },
  methods: {
    prevMonth() {
      this.currentMonthInNumber--;
      if (this.currentMonthInNumber < 0) {
        this.currentMonthInNumber = 11;
        this.currentYear--;
      }
      this.currentMonth = this.months[this.currentMonthInNumber];
    },
    nextMonth() {
      this.currentMonthInNumber++;
      if (this.currentMonthInNumber > 11) {
        this.currentMonthInNumber = 0;
        this.currentYear++;
      }
      this.currentMonth = this.months[this.currentMonthInNumber];
    },
    daysInMonth(year, month, firstDay, lastDay) {
      let date = new Date(year, month, 1);
      let days = [];
      while (date.getMonth() === month) {
        days.push(date.getDate());
        date.setDate(date.getDate() + 1);
      }
      return days;
    },
    chooseDate(date) {
      if (this.firstDay === false) {
        this.firstDay = date;
      } else if (this lastDay === false) {
        this.lastDay = date;
        this.setBetween();
      } else {
        this.firstDay = date;
        this.lastDay = false;
        this.between = [];
      }
    },
    setBetween() {
      if (this.firstDay > this.lastDay) {
        [this.firstDay, this.lastDay] = [this.lastDay, this.firstDay];
      }
      let date = new Date(
        this.currentYear,
        this.currentMonthInNumber,
        this.firstDay
      );
      while (date.getDate() <= this.lastDay) {
        this.between.push(date.getDate());
        date.setDate(date.getDate() + 1);
      }
    },
  },
};
</script>

<style scoped>
.datepicker {
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  text-align: center;
}

.month-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.prev-month,
.next-month {
  cursor: pointer;
}

.weekdays {
  display: flex;
}

.weekday {
  flex: 1;
  font-weight: bold;
  padding: 10px 0;
}

.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 10px;
  margin-top: 20px;
}

.day {
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 5px;
  cursor: pointer;
  height: 40px;
  line-height: 40px;
  text-align: center;
  user-select: none;
}

.day.active {
  background-color: #3c8dbc;
  color: #fff;
}

.day.between {
  background-color: #ddd;
}
</style>
英文:

Try this :

&lt;template&gt;
&lt;div class=&quot;datepicker&quot;&gt;
&lt;div class=&quot;month-header&quot;&gt;
&lt;i class=&quot;prev-month&quot; @click=&quot;prevMonth&quot;&gt;&lt;/i&gt;
{{ currentMonth }} {{ currentYear }}
&lt;i class=&quot;next-month&quot; @click=&quot;nextMonth&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;div class=&quot;weekdays&quot;&gt;
&lt;div class=&quot;weekday&quot; v-for=&quot;weekday in weekdays&quot; :key=&quot;weekday&quot;&gt;
{{ weekday }}
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;days&quot;&gt;
&lt;div
v-for=&quot;date in daysInMonth(
currentYear,
currentMonthInNumber,
firstDay,
lastDay
)&quot;
:key=&quot;date&quot;
class=&quot;day&quot;
:class=&quot;{
active: date === firstDay || date === lastDay,
between: between.includes(date),
}&quot;
@click=&quot;chooseDate(date)&quot;
&gt;
{{ date }}
&lt;/div&gt;
&lt;/div&gt;
{{ between }}
&lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
data() {
return {
weekdays: [&quot;Sun&quot;, &quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;],
months: [
&quot;January&quot;,
&quot;February&quot;,
&quot;March&quot;,
&quot;April&quot;,
&quot;May&quot;,
&quot;June&quot;,
&quot;July&quot;,
&quot;August&quot;,
&quot;September&quot;,
&quot;October&quot;,
&quot;November&quot;,
&quot;December&quot;,
],
currentMonth: &quot;&quot;,
currentMonthInNumber: &quot;&quot;,
currentYear: &quot;&quot;,
firstDay: false,
between: [],
lastDay: false,
};
},
mounted() {
const date = new Date();
this.currentMonth = this.months[date.getMonth()];
this.currentMonthInNumber = date.getMonth();
this.currentYear = date.getFullYear();
},
methods: {
prevMonth() {
this.currentMonthInNumber--;
if (this.currentMonthInNumber &lt; 0) {
this.currentMonthInNumber = 11;
this.currentYear--;
}
this.currentMonth = this.months[this.currentMonthInNumber];
},
nextMonth() {
this.currentMonthInNumber++;
if (this.currentMonthInNumber &gt; 11) {
this.currentMonthInNumber = 0;
this.currentYear++;
}
this.currentMonth = this.months[this.currentMonthInNumber];
},
daysInMonth(year, month, firstDay, lastDay) {
let date = new Date(year, month, 1);
let days = [];
while (date.getMonth() === month) {
days.push(date.getDate());
date.setDate(date.getDate() + 1);
}
return days;
},
chooseDate(date) {
if (this.firstDay === false) {
this.firstDay = date;
} else if (this.lastDay === false) {
this.lastDay = date;
this.setBetween();
} else {
this.firstDay = date;
this.lastDay = false;
this.between = [];
}
},
setBetween() {
if (this.firstDay &gt; this.lastDay) {
[this.firstDay, this.lastDay] = [this.lastDay, this.firstDay];
}
let date = new Date(
this.currentYear,
this.currentMonthInNumber,
this.firstDay
);
while (date.getDate() &lt;= this.lastDay) {
this.between.push(date.getDate());
date.setDate(date.getDate() + 1);
}
},
},
};
&lt;/script&gt;
&lt;style scoped&gt;
.datepicker {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
}
.month-header {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.prev-month,
.next-month {
cursor: pointer;
}
.weekdays {
display: flex;
}
.weekday {
flex: 1;
font-weight: bold;
padding: 10px 0;
}
.days {
display: grid;
grid-template-columns: repeat(7, 1fr);
grid-gap: 10px;
margin-top: 20px;
}
.day {
background-color: #fff;
border: 1px solid #ddd;
border-radius: 5px;
cursor: pointer;
height: 40px;
line-height: 40px;
text-align: center;
user-select: none;
}
.day.active {
background-color: #3c8dbc;
color: #fff;
}
.day.between {
background-color: #ddd;
}
&lt;/style&gt;

huangapple
  • 本文由 发表于 2023年2月13日 23:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定