Is there a gentle approach to transferring calendar event variables in JavaScript?

The example provided below showcases a @model that contains data.

To utilize specific portions of the data, I have transformed it into a Json object using Json.Serialize. This was necessary because the events:[ ] section requires data in a particular format resembling JSON.

In the events:[ ] section, you can observe my efforts to structure the data in a way that aligns with the requirements of the events:[ ] segment of the program (and everything works smoothly in this manner).

The issue arises from the fact that the number of records in the json may vary as it retrieves data from a database table.

My goal is to find a more efficient method for incorporating essential data into the events:[ ] section.

There might be an optimal approach to utilizing the json effectively which I am currently unaware of. I would greatly appreciate any insights or suggestions you may have on this matter.

@model IEnumerable<WebAppV2.Models.CalendarEvent>


<script>

    document.addEventListener('DOMContentLoaded', function () {
        var calendarEl = document.getElementById('calendar');


        var json = @Html.Raw(Json.Serialize(@Model));


        var calendar = new FullCalendar.Calendar(calendarEl, {
            initialDate: '2021-01-01',
            editable: false,
            selectable: false,
            businessHours: true,
            dayMaxEvents: true,
            events: [


                {
                    title: json[0].title,
                    start: json[0].start,
                    end: json[0].end
                },

                {
                    title: json[1].title,
                    start: json[1].start,
                    end: json[1].end
                },

                {
                    title: json[2].title,
                    start: json[2].start,
                    end: json[2].end
                },


            ]
        });

        calendar.render();
    });

</script>

Answer №1

This solution worked perfectly for me.

const data = @Html.Raw(Json.Serialize(@Model.Select(item => new
{
item.name,
item.date,
item.quantity
})));

Later on,

const chart = new Chart(chartEl, {
    initialDate: '2022-01-01',
    editable: true,
    selectable: true,
    businessHours: false,
    dayMaxEvents: false,
    data: data
});

chart.render();

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

Are MEAN stacks and boilerplates suitable for custom applications requiring specialized functionality?

After spending a significant amount of time learning and coding, I have encountered a common issue. You can find the link to the discussion below. Instead of reinventing the wheel, should I utilize existing boilerplates that combine Express, Angular, conn ...

Scrolling with animation

While exploring the Snapwiz website, I came across a captivating scroll effect that I would love to implement on my own site. The background picture changes seamlessly as you scroll, with the front image sliding elegantly into view. A similar type of scro ...

Change element's parent property's child property into an array form

Question 1: Do you believe that element.property is the same as element[property]? If your answer to question 1 is affirmative, then move on to Question 2: Can you convert element.parentProperty.childProperty into array syntax as explained in question ...

Changing the size of shapes in cytoscape.js when using the dagre layout

My goal is to resize the shapes of the nodes based on the size of the node text. I tried following the steps outlined in https://github.com/cytoscape/cytoscape.js/issues/649, but encountered some issues: The shapes consistently end up with equal height a ...

What is the solution for fixing the '$ not defined error' in a .js file with $ajax code?

var example = document.createElement("SCRIPT"); example.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"; var nodeScript= document.createTextNode("function test() {console.log('test message');$.ajax({ type: \"POST&bs ...

Implement a procedure for printing a page once the columnize operation has been completed

Hello, I have run into a problem that I need help with. Currently, I am trying to print a page after the function "columnize" has finished its task. However, the print function is executing before the columnize function completes. How can I ensure that t ...

When collapsing an accordion, Bootstrap radio buttons fail to properly select

I have attempted various methods to achieve the desired functionality of having the accordion content close and open accordingly when checking a radio button, while also having the button visually appear as 'checked'. For a more in-depth example, ...

Custom styling options for Google Chart

I have been attempting to dynamically adjust the width and height of a google-chart directive by utilizing input field values. However, I am encountering an issue where the width and height are not updating as expected. Check out the Plunker here $scope. ...

Creating a Bootstrap table that populates with data from a JSON file,

I'm encountering an issue with the bootstrap table plugin as it is not loading any values into the table. Despite successfully using it in the past, I am unable to identify the mistake in my current implementation. While the rows are appearing, the ta ...

Automatically convert a string containing http/json data into an object in Java

As a programming novice, I require some assistance. Is it feasible to automatically convert an HTTP call (that returns Json) into an object in Java? For instance, when the request is read, can System.out.println(obj) be used to directly return an OBJECT of ...

The changing of colors does not function properly when clicked in JavaScript

I am having an issue with a drop-down list that offers two options: blue and green. When I select blue and click on the text input field, its background color alternates between blue and black (the default text field color). The same applies when I choose ...

Using Node.js to execute JavaScript with imported modules via the command line

Having limited experience with running JavaScript from the command line, I am facing a situation where I need to utilize an NPM package for controlling a Panasonic AC unit, which includes a wrapper for their unofficial API. My objective is to create a sim ...

Encountering a glitch while iterating through function results in failure after the initial modification

I am facing some errors with the script provided below: var sections = ["#general_info", "#address_records", "#employment_history", "#driver_experience", "#military_experience", "#eeo_survey", &qu ...

Center the text within the div and have it expand outwards from the middle if there is an abundance of text

I have a div that is in the shape of a square box and I want it to be centered. No matter how much text is inside, I would like it to remain in the middle. I am using JQuery to create my square box and would like to center it using CSS. Here is my code: &l ...

Exploring the World of React JS by Diving into Backend Data

Let's consider an application that consists of three pages: "HomePage" "PrivatePage" "UserManagementPage" In addition, there is a file called "BackendCommunication.js" responsible for handling communication with the backend. "Homepage.js" import Re ...

Guide on implementing themes to HTML within the append() function

I am currently working on a project where I need to dynamically add HTML tags using JavaScript. However, I have noticed that the themes or styles are not being applied to the newly added elements within the append method. In my HTML file, I am using jQue ...

Is there a way to allow only the block code to shift while keeping the other span tags stationary?

Is it possible to change the text without affecting other span tags in the code? I want to make sure only this specific text is updated. How can I achieve that? var para_values = [ { content: "BRAND " }, { content: "MISSION" } ]; functi ...

Testing API route handlers function in Next.js with Jest

Here is a health check function that I am working with: export default function handler(req, res) { res.status(200).json({ message: "Hello from Next.js!" }); } Alongside this function, there is a test in place: import handler from "./heal ...

Consistently directing to a single page on the Node Js web server

I'm in the process of creating a website with multiple HTML pages. Currently, I have code that successfully links to the login page, but unfortunately, the localstores page also directs to the login page. const express = require('express') ...

The process of altering a span label's class with JavaScript

I am currently working with an HTML element that looks like this: <dd><span class="label label-success" id="status">Production</span></dd> My goal is to update it to: <dd><span class="label label-warning" id="status"> ...