如何向日历中添加日程

本文介绍了如何使用Vue.js和axios库获取日程数据,并通过el-calendar组件展示。通过计算属性实现实时过滤和计算显示特定日期的日程,同时利用Popover插件在鼠标悬停时弹出详细事件信息。代码展示了前端开发者如何处理日程管理和UI交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.获取所有日程

const getAllSchedules = async () => {
  let res = await axios.get("calendar/get");
  state.schedules = res.data;
};

2.计算属性同步更新日程

const getOneSchedule = computed(() => {
  return function (data) {
    let theDay = [];
    state.schedules.find((item) => {
      if (item.date === data.day) {
        theDay.push(item);
      }
    });
    return theDay;
  };
});

3.通过popover插入日历 

<el-calendar v-model="state.value"
    ><template #dateCell="{ data }">
      <el-popover
        placement="top-start"
        trigger="hover"
        v-if="getOneSchedule(data).length"
        width="auto"
      >
        <li v-for="item in getOneSchedule(data)">
          <span>{{ item.event }}</span>
          <span v-if="item.completed" style="margin-left: 8px">—{{ item.completed }}%</span>
        </li>
        <template #reference>
          <div class="hasSchedule">
            {{ data.day.split("-").slice(2).join("") }}
          </div>
        </template>
      </el-popover>
      <div v-else>
        {{ data.day.split("-").slice(2).join("") }}
      </div>
    </template></el-calendar
  >

4.伪元素显示有日程的日期

.hasSchedule::before {
  content: "";
  position: absolute;
  width: 18px;
  height: 2px;
  background: #409eff;
  margin-top: 36px;
  margin-left: 0;
}

5.最终效果及完整代码如下

<script setup>
import axios from "axios";
import { computed, onMounted, reactive } from "vue";

const state = reactive({
  schedules: [],
});

onMounted(async () => {
  getAllSchedules();
});

const getAllSchedules = async () => {
  let res = await axios.get("calendar/get");
  state.schedules = res.data;
};

const getOneSchedule = computed(() => {
  return function (data) {
    let theDay = [];
    state.schedules.find((item) => {
      if (item.date === data.day) {
        theDay.push(item);
      }
    });
    return theDay;
  };
});
</script>

<template>
  <el-calendar v-model="state.value"
    ><template #dateCell="{ data }">
      <el-popover
        placement="top-start"
        trigger="hover"
        v-if="getOneSchedule(data).length"
        width="auto"
      >
        <li v-for="item in getOneSchedule(data)">
          <span>{{ item.event }}</span>
          <span v-if="item.completed" style="margin-left: 8px"
            >—{{ item.completed }}%</span
          >
        </li>
        <template #reference>
          <div class="hasSchedule">
            {{ data.day.split("-").slice(2).join("") }}
          </div>
        </template>
      </el-popover>
      <div v-else>
        {{ data.day.split("-").slice(2).join("") }}
      </div>
    </template></el-calendar
  >
</template>

<style scoped lang="scss">
.el-calendar {
  --el-calendar-header-border-bottom: transparent;
  :deep(.el-calendar__header) {
    flex-direction: column;
    height: 56px;
    padding: 0;
  }
  :deep(.el-calendar__body) {
    padding: 0;
    .el-calendar-day {
      height: 56px;
      div {
        height: 100%;
        width: 100%;
        line-height: 40px;
      }
      .hasSchedule::before {
        content: "";
        position: absolute;
        width: 18px;
        height: 2px;
        background: #409eff;
        margin-top: 36px;
        margin-left: 0;
      }
    }
  }
}
</style>

在Android中,为了向系统日历添加日程事件,你需要使用`CalendarContract`和`AlarmManager`。以下是一个基本的过程: 1. **权限检查**:确保你的应用已获得所需的权限,如`READ_CALENDAR`和`WRITE_CALENDAR`,在`AndroidManifest.xml`中添加: ```xml <uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /> ``` 2. **获取日历访问助手**: ```java ContentResolver cr = getContentResolver(); CalendarContract.Calendarendars calendars = CalendarContract.Calendarendars.CONTENT_URI; Cursor cursor = cr.query(calendars, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { int idIndex = cursor.getColumnIndex(CalendarContract.Calendarendars._ID); String primaryCalendarsId = cursor.getString(idIndex); cursor.close(); } ``` 3. **创建日程事件**: ```java Calendar calendar = Calendar.getInstance(); ContentValues values = new ContentValues(); values.put(CalendarContract.CalendarEvents.CALENDAR_ID, primaryCalendarsId); values.put(CalendarContract.CalendarEvents.TITLE, "事件标题"); values.put(CalendarContract.CalendarEvents.DESCRIPTION, "事件描述"); values.put(CalendarContract.CalendarEvents.EVENT_TIMEZONE, "UTC"); // 设置时区 values.put(CalendarContract.CalendarEvents.DTSTART, calendar.getTimeInMillis()); // 开始时间 if (hasEndTime) { values.put(CalendarContract.CalendarEvents.DTEND, calendarEnd.getTimeInMillis()); // 结束时间 } else { values.put(CalendarContract.CalendarEvents.DURATION, -1); // 持续一天的活动 } Uri eventUri = cr.insert(CalendarContract.CalendarEvents.CONTENT_URI, values); ``` 4. **设置提醒(如果需要)**: 使用`AlarmManager`设置事件提醒,这里仅示例如何设置一个延迟1小时的提醒: ```java Intent intent = new Intent(this, AlarmReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0); long triggerAtTime = calendar.getTimeInMillis() + (60 * 1000 * 60); // 1小时后触发 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, alarmIntent); ``` 5. **编写接收器(AlarmReceiver)**:处理闹钟响铃时的操作,比如更新通知或打开应用内的事件详情。 务必注意,用户可能会对事件进行编辑或删除,所以建议定期同步数据以避免冲突。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值