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

Retrieving Information from Ajax Response Following a Successful Insert Query in Codeigniter

I am trying to use ajax method to insert form data into a database and then redirect it to the next page. I have successfully passed the data in ajax and inserted it into the database table. However, I am facing an issue with getting the generated referenc ...

Troubleshooting AngularJS: Directive unable to access controller's variable within scope

I am facing a challenge with an element that has both a controller and a directive featuring an isolate scope: scope: { dirVar: '= ' } My objective is to execute specific parts of the directive only when a certain variable is true. I am try ...

Issue: Error occurs when using _.sample on an array containing nested arrays

I am working with an array of arrays that looks like this: [[0,0], [0,1], [0,2], [0,3]...] My goal is to randomly select N elements from the array using Underscore's _.sample method: exampleArr = [[0,0], [0,1], [0,2], [0,3]...] _.sample(exampleArr, ...

What sets local Node package installation apart from global installation?

My curiosity sparked when I began the process of installing nodemon through npm. Initially, I followed the recommended command and noticed the instant results displayed on the right side of my screen: npm i nodemon This differed from the installation ins ...

Problem with CSS: In Chrome, text fields have 3px additional margin-right

I am facing an issue with the margins of my text fields. Even though I have set a margin-right of 5px for each field, Google Chrome seems to be adding an additional 3-4px automatically. This problem is evident when I dynamically generate more text fields u ...

Display customized modal content in React based on specific criteria

I have a list of product partners with a "Know More" link that opens a modal. I'm trying to figure out the best way to pass props to the modal in order to display the correct partner's information. Here is the code snippet related to this functi ...

Is it possible to utilize Vue's splice method within a forEach loop in order to delete multiple elements at

I am currently working on an image grid in array form. I am trying to implement a multi-deletion functionality. When a checkbox for each image in the grid is checked, I store the selected image's index in an array called selectedImages. Upon clickin ...

Integrating Watson Conversation with Oracle Database

Hello everyone, I am currently working on a project where I need Watson to fetch a response manually set from our Oracle Databases. To achieve this, I am using async to access the database sequentially and return the response. Initially, I faced an issue ...

Using the FileInput Shiny App to Upload JSON Files

I'm currently working on developing a shiny app that can load and display data from a .json file in a table format. I've attempted to import the data and present it using DataTable along with the fromJson function from Json Lite. options(shiny ...

Modify HTML text using conditional CSS without JavaScript

Apologies for asking such a beginner question, but I've searched high and low for a similar post, to no avail. Here's my query: I have a paragraph text in my HTML code that I want to automatically replace with new text when I click a specific B ...

Utilizing Jquery ajax to retrieve JSON data from a web service

Instead of utilizing a page WebMethod, I am considering using jQuery ajax call to retrieve JSON data from C# .NET. Currently, the response I receive in JSON format appears as follows: {"d":"{\"ID\":1,\"Value\":\"First Value&bsol ...

Placing a JavaScript button directly below the content it interacts with

I am currently working on a button that expands a div and displays content upon clicking. I am facing an issue where I want the button to always be positioned at the bottom of the div, instead of at the top as it is now, but moving it within the parent div ...

Troubiling Responsive Menu in React

I am currently working on developing a React Responsive Navigation with SCSS, but I am facing an issue. When I click on the hamburger button, nothing happens and the menu does not slide down in the mobile view. I tried inspecting the code in the browser to ...

Organize unstructured JSON data using a specific method

My service returns a JSON with irregular data structure as shown below: dataFromService: [ { event_data: '2021-03-18T15:20:31.314Z', // if !null = page event_category: 'news', event_title_en: 'page title ...

What are some alternative ways to redirect multiple pages in a Navbar component in React Js without encountering the 'useNavigate()' error?

How can I resolve this error while working with react js? Error: useNavigate() is only allowed within a <Router> component. ▶ 3 stack frames were collapsed. Navbar C:/Users/dell/OneDrive/Desktop/ReactJs/react-learning/src/components/Navbar.js:9 ...

Delivering a Logo to a Remote Website Based on Specific Conditions

I am in the process of creating a code snippet for a specific user group that has earned the privilege to display our seal on their website. The concept initially appeared straightforward, but when I attempted to implement it on a different site for testin ...

difficulty displaying an image sourced from a URL string within a JSON file in SwiftUI

In my SwiftUI file called ContentView.swift, I have the following code: struct Nasa_data: Codable { var copyright: String var date: Int var explanation: String var hdurl: String var media_type: String var service_version: String ...

Avoiding remote submission in Grails forms: Best practices

<g:formRemote name="form1" update="homeBody" url="[controller: 'xxx', action:'aaa']"> <Button type="submit">Save</Button> </g:formRemote> I am facing a scenario where I need to place a text field o ...

Switching class names on click in Vue.js

When clicked, I would like to toggle the class name from "product fav" to "product fav active". The change should be conditional; if the class is already active, it should be removed. ...

SharpDX and System.Drawing: A seamless collaboration

Is there a way to transfer an image drawn using default .net paint methods (System.Drawing methods) to a SharpDX Texture2D object for display as a texture, preferably with the SharpDX Toolkit? If so, how can this be achieved? Current approach I am attemp ...