"Utilize Vuejs to establish a binding between two objects when necessary

With the help of moment, my calendar generates 41 days.

    for (let x = 0; x < 42; x++) {
        context.add(1, 'd');
        let day = {
            'date': moment(context),
            'events': []
        };
    }
    state.month = days;

After generating the days, I proceed to render them.

        <day
             v-for="(day, index) in days"
             v-bind:day="day"
             v-bind:index="index"
             v-bind:key="day.id">
            0
        </day>

To populate each day with events retrieved from the database per month, I fetch and send them to vue.

**Event**
id
name
date
is_accepted

public static function getEventsByMonth($month) {
    return Events::whereMonth('date', '=', $month);
}

The main question now is whether there is a vue method to properly bind events to days without manually looping through each event and comparing dates?

Edit:

Upon retrieving events from the database and storing them in my vuestore, they are as follows:

events:Object
2017-04-02 00:00:00:Object
2017-04-14 00:00:00:Object

Below are the generated days:

days:Array[42]
0:Object
1:Object

Answer №1

To create an array in the backend when retrieving events from a database, you can use the following code snippet (customize as needed) :

$events = array();

foreach( $eventsFromDatabase as $event) {
    if( !isset($events[$date]) ) {
        $events[$date] = array();
    }
    $events[$date][] = $event;
} 

return json_encode($events);

Then, in Vue :

<day
    v-for="(day, index) in days"
    v-bind:day="day"
    v-bind:index="index"
    v-bind:key="day.id">

   <!-- You must create the event component -->
   <event v-for="(event, index) in events[day]" :key="event"
          :id="event.id" :name="event.name" :date="event.date"
          :is-accepted="event['is_accepted']
          >
       
   </event>

</day>

UPDATED :

The logic here is that for each day, you place the event inside the day component if it exists:

<day v-for...> HERE PUT EVENT COMPONENT </day>

In the template definition of the day component, make sure to use <slot></slot> to accept HTML content from the parent component. Learn more about slots here.

Since looping through the days array gives you access to the value of the day (like day.date in case of moment.js), you can retrieve the relevant event by using :

events[day.date] // adjust .date according to your requirements (e.g., for moment.js)

Now, you can access the event name like this :

<day v-for="(day, index) in days"...>
    <div v-if="events[day.date] !== 'undefined'">
        <event :name="events[day.date].name ...etc..></event>
    <di>
</day>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with SoundCloud Javascript SDK 3.0 failing to execute put methods

Currently, I am developing a service that utilizes the SoundCloud Javascript SDK 3.0, and I seem to be encountering issues with the PUT methods. Every call I make results in an HTTP error code of 401 Unauthorized. Below is my JavaScript code, which close ...

Utilizing Vue3: Leveraging Functions Across Components

After starting to work with Vue3, I decided to create a basic app that allows users to add items to a list. The app consists of two components: ItemList and ItemForm, both added to the main component App.vue like this: App.vue - simplified <template&g ...

The URL does not contain the complete hash

I'm currently working on a gallery using Jquery Elastislide. var imageIndex = 0; if (window.location.hash) { var imageIndexStr = window.location.hash.replace('#',''); // removing # imageIndex = parseInt(imageIndexStr, 0) ...

Tips for resolving an issue with mongoose Model.create becoming unresponsive indefinitely

I'm having trouble understanding why my mongoose Model.create operation isn't completing successfully. The same connection is working well with other controller functions. vscode postman I am attempting to create a new document, but my code s ...

Fill the drop-down menu with the present day's date, month, and year

I'm new to this, so please bear with me. I have some html and jQuery code: $(document).ready(function() { var d = new Date(); var month = d.getMonth() + 1; var year = d.getFullYear(); $("#month").val(month); $("#year").val(year) ...

Encountering an undefined variable in the .env file

Once the .env file was created in the main React folder REACT_APP_API_KEY=gzomlK5CKLiaIWS.... I also installed the .env NPM library Despite this, I continued to receive undefined in the API file. What could be causing this issue? import React, { useState ...

Ways to prevent committed mutation in Vuex

Can you halt a mutation in vuex post commit? In the case of redux, we can determine that an action is halted if next is not invoked in middleware. ...

The utilization of a combobox in VueJS 3 alongside the Mitt event bus

I've set up mitt as a global event bus in my main.js: import { createApp } from "vue"; import App from './App.vue' import mitt from "mitt"; const emitter = mitt(); const app = createApp(App); app.config.globalProperties.emitter = emitter; ap ...

Utilizing Regex to Authenticate a CAGE Code

Our task is to create a RegEx pattern that can accurately validate a CAGE Code A CAGE Code consists of five (5) positions. The code must adhere to the following format: The first and fifth positions must be numeric. The second, third, and fourth position ...

Implement the color codes specified in the AngularJS file into the SASS file

Once the user logs in, I retrieve a color code that is stored in localstorage and use it throughout the application. However, the challenge lies in replacing this color code in the SASS file. $Primary-color:#491e6a; $Secondary-color:#e5673a; $button-color ...

How can we optimize axios requests with lodash debounce?

Utilizing a state prop named network busy status to control elements in the UI. Due to the rapid changes in status, my spinner appears overly active. Is there a simple method, utilizing lodash _.debounce, to throttle this section of code? const instance ...

When data is stored in Internet Explorer's cache, any changes made are not being reflected in

Internet Explorer stores data in cache and even if there are changes, it may not reflect onclick. However, when I open the developer mode and try to access the same, then it works perfectly. This issue only seems to occur in IE as all other browsers work f ...

Tips for continuously randomizing colors in p5.js

I recently began exploring p5.js and I have a query regarding color randomization. Currently, it seems that the color only changes randomly when I restart the code. Is there a way to trigger this randomization every time the mouse is clicked? Below is the ...

Python sends back a list containing garbled characters to Ajax

Need help fixing the output of a Python list returned to Ajax, as it appears strange. ap.py @app.route('/_get_comUpdate/', methods=['POST']) def _get_comUpdate(): comNr = request.form.get('comNr') com_result ...

Trouble encountered when utilizing jQuery for XML to HTML conversion and vice versa (CDATA mistakenly transformed into HTML comments)

I am in the process of developing a plugin/bookmarklet that is designed to extract an XML document from the <textarea> element on a web page, make modifications to the XML content, and then reinsert the updated version back into the <textarea> ...

What is the best way to add up the values of selected cells in DataTables?

I am seeking a way to add up the values from cells when they are checked. For example: I have discovered how to do this on the DataTables website, but it doesn't quite meet my requirements. The table will contain a lot of data, and I want to sum up t ...

What could be causing my form not to Submit when attempting submission without utilizing the submit button?

Here is the code for my validation: $(function () { function validateForm() { // Code to validate form inputs } $('#myform').submit(validateForm); }); After this, I want to submit the form when a certain action occurs: < ...

What could be causing the errors in my subscription function?

Working on an e-commerce website, I encountered errors in the "cartservice" specifically in the "checkoutFromCart()" function. The console displayed the following error: src/app/services/cart.service.ts:218:81 218 this.http.post(${this.serverUrl}ord ...

ReactJS how to prevent accordion from collapsing automatically

My custom accordion layout for the features needed in the site I am building is not working well with Jquery. How can I modify it to collapse properly? Specifically, I want it so that when I open number 2, number 1 will automatically close. I've trie ...

What is the best way to prompt Leaflet to refresh the map display?

I'm facing a challenge while integrating Leaflet with React, where Leaflet seems to want control over the DOM rendering as well based on my research. Currently, I have countries being properly colored according to specific color codes derived from ba ...