Creating a straightforward timeline with CSS Grid, Vue.js, and moment.js/date-fns

I have created a basic calendar using Vuejs, momentjs, and css-grids layout. However, the calendar currently starts with Sunday (in American style) and I would like to change it to start with Monday (European style). I attempted to modify the column method by decrementing it to 2, but it only displays August and other months incorrectly starting with the wrong weekdays. Additionally, I believe I need to adjust the css option grid-template-columns to accommodate Mondays as the first days of the week.

You can view the codepen here: https://codepen.io/moogeek/pen/oNWyWvM

// Vue instance code goes here
// CSS styling code is here
// HTML structure code is presented below in the next block

upd. I have found a better solution which involves separating weekdays and days into different containers and adding the grid-column:7 property to the first day child. I also adjusted the column function so it no longer adds additional offset:

https://codepen.io/moogeek/pen/ExmRzgb

After implementing this update, the August month now displays correctly upon mounting. However, when I click on previous or next months, the display gets messed up and shows September's days order for all months. Can you please help me identify what I am doing wrong and how I can fix the code to ensure all months are displayed properly?

Answer №1

For those interested, I have implemented a modern solution using date-fns, which can be accessed on GitHub at: https://github.com/kissu/so-date-fns-calendar

You can also find the hosted version here:

Below is the relevant code snippet that demonstrates the functionality:

<template>
  // Code snippet goes here
</template>

<script>
  // JavaScript logic here
</script>

<style scoped>
  // Styling information here
</style>

After thorough testing one year before and after, all functionalities appear to be working seamlessly. Feel free to reach out for any additional clarifications/comments!

Additionally, I opted for a Nuxt application for quick development, but rest assured that the provided code is fully compatible with vanilla Vue.

Answer №2

Recently, I made a temporary adjustment to include an extra check that prevents assigning the grid-column property if the first day of the current month falls on a Sunday. To see the implementation, you can visit: https://codepen.io/moogeek/pen/JjNBBqo

<div v-for="(day, index) in days"
    :data-date="day.format('DD.MM.YYYY')" 
    :style="{gridColumn:setGridColumn(index)}"
    :class="{ day, today: isToday(day) }">
    <span>{{ day.format('D') }}</span>
</div>
//inside methods
setGridColumn(index){
      return (this.days[0].startOf('month').format('e')!=6) ? this.column(index) : null;
}

Are there perhaps alternative solutions or concerns about potential bugs arising from this approach?

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

What is the best way to implement click-to-play functionality for my HTML5 videos without disrupting the built-in controls?

I have implemented code to enable click-to-play functionality on HTML5 videos using the following script: $('video').click(function() { if ($(this).get(0).paused) { $(this).get(0).play(); } else { $(this).get(0).pause(); } }); W ...

Contrast objects and incorporate elements that are different

In my scenario, I have a list of days where employees are scheduled to work. However, on the website, I need to display the remaining days as leave. In cases where an employee has two different shifts, there can be two elements in the object. var WorkDays ...

What is the process for establishing Many-to-Many connections with personalized field titles using Bookshelf.js?

I am interested in setting up a many-to-many relationship using Bookshelf.js and I would like to customize the names for the foreign key columns. Additionally, I want to have access to the helper table in Bookshelf just like in the example below: var Phys ...

refresh PHP scripts using asynchronous JavaScript and XML (AJAX)

My PHP code looks like this: <div> <?php if($link AND $access AND !$timer_expired): ?> <font color="green">Status: Unlocked - You have successfully unlocked "<?php echo htmlspecialchars($link['link_title']);?>", the websi ...

Proceed to the section with modal

My goal is to create a modal with 4 sections, each loading its content dynamically using the .load() function to the container on the right side. The challenge I'm facing is that I have a footer menu that triggers the modal to open, and I need it to ...

How can I retrieve elements from an array that match a certain criteria and update the corresponding objects in

When an array contains matching IDs, the goal is to merge these objects into one object without affecting the original array. The current code only returns matching objects, but the expected result should combine them as described. main.ts const arr = [{ ...

What method is most effective for transferring arrays from C# to Javascript in a simple and straightforward manner?

Although there may be similar questions here, none of them are asking the exact same thing as mine. For my ASP.NET MVC4 project, I have decided to use aspx as my view engine. Within my views, I have integrated a JavaScript code that takes a table of value ...

What sets React$Element apart from ReactElement?

Attempting to implement flow with ReactJS and needing to specify types for prop children. The article on Flow + React does not provide any information. Despite encountering examples using ReactElement (e.g), I received an error message from flow stating i ...

React component fails to re-render after state change

For the past two days, I've been struggling with this error and can't seem to fix it! I'm currently working on creating a weather app in React which utilizes the API. The app features a Bootstrap Navbar with a search option that allows user ...

Iterating through two classes in a Javascript loop

Hello, I'm facing a small obstacle that I'm hoping to overcome using JavaScript/jquery. Essentially, I have multiple div classes and I want to create a loop that adds a class to specific divs without manually assigning an id to each one. The goal ...

Is the use of Youtube API with an unaffiliated laborer

What an unusual situation! I've implemented the YouTube JavaScript API to display a playlist on my website, but upon checking how things were operating in my workers, it appears that the API is directing its messages to an unexpected location. The da ...

The concept of Event Flow in JavaScript

When an element within another element has a click handler attached, clicking the inner element will trigger its click handler first and then bubble up to the parent element to execute its click handler as well. This is my interpretation of the process. I ...

Switching Between HTML Using Javascript

I am currently working on an app that allows users to easily check the local weather and temperature in either celsius or fahrenheit. However, I've encountered a problem when trying to switch between the two unit types by simply clicking on the temper ...

The lookAt function in threeJS seems to be pointing in the opposite direction due to inherited rotations

I'm attempting to align this._mesh.skeleton.bones[ 5 ] (located at the bend in the green object extending from the hand) with the yellow helper line. _FindBubble() { const handPosition2world = new THREE.Vector3(); this._anchor[&apo ...

Difficulty accessing class functions from the test application in Node.js NPM and Typescript

I created an NPM package to easily reuse a class. The package installs correctly and I can load the class, but unfortunately I am unable to access functions within the class. My project is built using TypeScript which compiles into a JavaScript class: For ...

What is the method to retrieve the local filepath from a file input using Javascript in Internet Explorer 9?

I'm experiencing some issues with the image-preview function on IE9, similar to the example photo shown. This method is not functioning properly and throwing an error in IE9, while it works perfectly fine in IE6-8. I am looking for a way to retrieve ...

Tips for avoiding a 500 GET error due to an empty request during page loading

I recently encountered an issue with a form containing a dependent drop-down menu that reloads after form submission to preselect the main choice that was made before submission. The problem arises when the page initially loads without any parameters being ...

What are some creative ways to incorporate a MobX store into a utility function?

Exploring the use of a mobx store in a utility function I am faced with a situation where I have a mobx store and a utility function for making an axios call. How can I access the store value within the utility function? // Store example export defaul ...

Adjusting the Image Width in React to Match Bootstrap Column Width

Exploring the capabilities of the react-image-mapper library, I stumbled upon an interesting feature: the ImageMapper component offers a prop called width, representing the image width in pixels. Integrating this component into my application based on the ...

Using multiple onChange and value attributes in React to handle different input fields and

I am having trouble setting a maximum input value in a text field, and I can't seem to figure out how to make it work. The issue seems to be with the two onChange events and values. I'm not sure how to resolve this. function Form({ onAddStudent ...