My API is returning chat messages in a specific object format.
messages : [
[
sender: "John Doe",
text: "Some message",
sent_at: "2020-09-26",
date_group: "Today",
],
[
sender: "John Doe",
text: "Some message",
sent_at: "2020-09-26",
date_group: "Today",
],
[
sender: "John Doe",
sent_at: "2020-09-26",
date_group: "Today",
],
[
sender: "John Doe",
text: "Some message",
sent_at: "2020-09-25",
date_group: "Yesterday",
],
[
sender: "John Doe",
text: "Some message",
sent_at: "2020-09-25",
date_group: "Yesterday",
],
[
sender: "John Doe",
text: "Some message",
sent_at: "2020-09-24",
date_group: "Thursday",
],
]
To display these messages according to their date_group
, I have implemented a comparison function for the dates.
printDateGroup(messageKey) {
return this.messages[messageKey - 1] && this.messages[messageKey].date_group !== this.messages[messageKey - 1].date_group
|| messageKey === 0;
},
While this method is effective, all messages are currently displayed within a single div, causing issues with the sticky positioning of the date_group
.
The current rendering structure is as follows.
<div class="d-flex flex-column message-wrapper">
<template v-for="(message, idx) in messages">
<div v-if="printDateGroup(idx)" class="date-label">
<span>{{ message.date_group }}</span>
</div>
<div class="message-text">{{message.text}}</div>
</template>
</div>
However, I desire to organize the messages into separate divs based on their date_group
, like so.
<div class="d-flex flex-column message-wrapper">
<div class="date-wrapper">
<div class="date-label">Yesterday</div>
<div class="message-text">Some message</div>
<div class="message-text">Some message</div>
<div class="message-text">Some message</div>
</div>
<div class="date-wrapper">
<div class="date-label">Today</div>
<div class="message-text">Some message</div>
<div class="message-text">Some message</div>
<div class="message-text">Some message</div>
</div>
</div>
I attempted to keep the divs open without closing them like in PHP, but it did not work as expected.