[英]Using embedded attributes in Vuetify Calendar
I'm trying to use the v-calendar component from Vuetify.我正在尝试使用 Vuetify 的v-calendar组件。
I saw in the documentation I can use the event-start
prop if my events don't have the same attributes' names.我在文档中看到如果我的事件没有相同的属性名称,我可以使用event-start
道具。
The problem is that my events have embedded attributes and I don't know if event-start
handles this case.问题是我的事件有嵌入的属性,我不知道event-start
处理这种情况。
My events :我的活动:
events: [
{
id: 'b9d93291-6d95-47b9-994a-ee9f266fb6b8',
type: 'reservation_item',
attributes: {
start_date: '2020-09-23T00:00:00.000Z',
end_date: '2020-09-25T00:00:00.000Z',
},
},
]
The events example from vuetify : vuetify 的事件示例:
events: [
{
name: 'Weekly Meeting',
start: '2020-09-07 09:00',
end: '2020-09-07 10:00',
},
],
I tried to do something like that but it doesn't work.我试图做类似的事情,但它不起作用。
<v-calendar
ref="calendar"
locale="fr-fr"
:now="today"
:value="today"
:events="events"
event-start="attributes.start"
color="primary"
type="month"
></v-calendar>
After spelunking the source code for the vuetify plugin , the latter expects that the value be present in the event object, as a direct property.在探索vuetify 插件的源代码之后,后者期望该值作为直接属性存在于事件对象中。 So you cannot acces other nested "children", it has to be a direct property.所以你不能访问其他嵌套的“孩子”,它必须是一个直接的属性。
There are two alternatives to make this work:有两种选择可以使这项工作:
1- map your events array by moving the properties inside attributes
to the root of your object then pass this prop to v-calendar
: event-start="startDate"
1-通过将属性内的attributes
移动到对象的根来映射事件数组,然后将此道具传递给v-calendar
: event-start="startDate"
2- Create a javascript class (MyEvent) with a fromJson
method that take the raw JSON from your API ( this way you encapsulate the JSON into domain objects) and return an array of MyEvent instances. 2- 使用fromJson
方法创建一个 javascript 类 (MyEvent),该方法从您的 API 获取原始 JSON(这样您将 JSON 封装到域对象中)并返回一个 MyEvent 实例数组。 this way you can do for example : events[0].start
and you don't even have to pass it as a value to the event-start
prop, since by default it expects a start
attribute as a default value.通过这种方式,您可以执行例如: events[0].start
并且您甚至不必将其作为值传递给event-start
道具,因为默认情况下它需要一个start
属性作为默认值。
Another advantage of this alternative, is that since the event is now encapsulated into its own javascript class, you can add helper methods, or getters/setter or any logic that would otherwise be inside your "view" logic, and contribute to have a better separation of concerns.这种替代方法的另一个优点是,由于事件现在被封装到它自己的 javascript 类中,您可以添加辅助方法、getter/setter 或任何本来会在您的“视图”逻辑中的逻辑,并有助于获得更好的关注点分离。
To make this works, I had to change my events
data为了使它起作用,我不得不更改我的events
数据
<template>
<v-calendar
ref="calendar"
locale="fr-fr"
:events="myEvents"
event-start="start"
color="primary"
type="month"
></v-calendar>
</template>
data: () => ({
events: [
{
id: 'b9d93291-6d95-47b9-994a-ee9f266fb6b8',
type: 'reservation_item',
attributes: {
start_date: '2020-09-23T00:00:00.000Z',
end_date: '2020-09-25T00:00:00.000Z',
},
},
],
}),
computed: {
myEvents() {
const reservations = this.reservations
reservations.forEach((element) => {
element.start = element.attributes.start_date
element.name = 'test'
element.end = element.attributes.end_date
})
return reservations
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.