Template not being properly populated by handlebar for JSON output

Hello there! I am currently working on making an http GET call to an MVC controller action which returns JSON data. The JSON data looks something like this:

[
    {
        "PlanCode": "P001",
        "PlanName": "Plan1"
    },
    {
        "PlanCode": "P002$",
        "PlanName": "Plan2$"
    },
    ...
]

I am using Handlebars.js for templating and have the following code snippet:

var PLAN_METHOD = {

    handlerData: function (planJSON) {

        var templateSource = $("#plan-template").html();
        template = Handlebars.compile(templateSource);
        var context = planJSON;
        plansHTML = template({planJSON:context});
        $('#plans-div').html(plansHTML);

    },

    loadPlansData: function () {

        $.get("http://localhost:41801/plan/getplans", null, this.handlerData)

    }
};

$(document).ready(function () { 

    PLAN_METHOD.loadPlansData();

});

The template HTML structure is as follows:

<div id="plans-div">
</div>

<script id="plan-template" type="text/x-handlebars-template">
    <table>
        <thead>
            <tr>
                <th>Plan Code</th>
                <th>Plan Name</th>
            </tr>
        </thead>
        <tbody>
            {{#each Plans}}
            <tr>
                <td>{{this.PlanCode}}</td>
                <td>{{this.PlanName}}</td>
            </tr>
            {{/each}}
        </tbody>
    </table>
</script>

However, I am facing an issue where the 'plansHTML' in the Handlebars JavaScript code is not being populated with the JSON data rows. Any suggestions or help would be greatly appreciated!

Answer №1

The template you are using is expecting an array under the Plans property in the data. However, there is no Plans property in your data. To resolve this issue, all you need to do is use the accurate property name in your template data object:

htmlPlans = loadTemplate({ Plans: info });

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

Restrict access to PHP scripts to only be allowed through AJAX requests

Within my content management system (CMS), I have a specific page that fetches various mini-interfaces from PHP files located in an /ajax directory using AJAX. I am wondering if there is a way to restrict access to these files solely through AJAX requests ...

Send binary information using Prototype Ajax request

Currently, I am utilizing Prototype to send a POST request, and within the postdata are numerous fields. One of these fields contains binary data from a file, such as an Excel spreadsheet chosen by the user for upload. To retrieve the contents of the file ...

Array Ninjutsu Multi-dimensional Fusion

I'm currently working with Delphi XE4 and SuperObject 1.24 Below is the structure I am dealing with : type TMyArray = Array of Array of Variant; TMyRecord = Record Values : TMyArray; end; var T, W: TMyRecord; S: String; i: integer; ...

Retrieve the value from another select element

Is there a way to create a function in pure JavaScript that changes the selected options in two select tags based on each other? For example, if I choose a French word in one select tag, the English equivalent automatically changes in the other select tag ...

Ways to swap webpack-dev-server for alternative servers such as express or apache

After using vue-cli to scaffold a Vue app, I am looking to set up a new web server instead of webpack-dev-server. I am interested in having a separate configuration file to customize the server settings, similar to using Node Express for production deploym ...

Is it possible to use TypeScript in a React Native project with a JavaScript file?

Currently, I am learning React Native by working on app clones like Instagram and YouTube. I have recently started an AirBnb clone project, but I'm facing some issues with the initial build. One issue I noticed is that in 'App.js', the temp ...

Disabling eventListener upon the occurrence of another event is ineffective

I am having trouble with two functions in my app that create different HTML structures. I want to close the info button event when the menu_div button is clicked, and vice versa. Can anyone provide some help? const menu_div = document.createElement("butt ...

Trouble encountered when transmitting JavaScript array through AJAX to Laravel

I am encountering an issue with sending a JavaScript array to a controller via an AJAX GET method. Here is the structure of my JavaScript: var requestData = JSON.stringify(commentsArray); console.log(requestData); //I can see the correct JSO ...

Implementing additional input with Jquery causes tooltips to malfunction

I am developing a form that allows users to add as many rows as needed. Each input is being treated as an array for easy data storage in the database, which is a new concept for me. $(document).ready(function() { var maxField = 10; //Limitati ...

Peruse a spreadsheet for relevant information

I am currently facing an issue with a search bar that I implemented to filter through a table. However, for some reason, the filtering function does not seem to work on the tbody section of the table. The content in the tbody is generated dynamically usi ...

Exploring D3 to extract nested information and build an interactive navigation tree

Recently, I've been working with d3's zoom circle feature from here: The data I'm dealing with is structured as a json array with various levels of arrays and objects. object = { class: "show", name: "Game of Thrones", childre ...

Utilizing onMouseEnter and onMouseLeave events in React using hooks

My goal is to create a drop-down menu with a delay, allowing users to hover over the child list items before they disappear. However, I seem to have made an error somewhere but can't pinpoint it. It's likely just a simple mistake that my eyes are ...

Clipped Words & Silhouettes

I'm having trouble ensuring the text in this particular example displays correctly. I am experiencing challenges with the clipping of text and shadows on certain letters, and I'm struggling to identify the root cause as well as the solution. In ...

In React components, encountering "cannot read properties of undefined" error occurs, whereas it functions perfectly fine within the getStaticProps

I am currently in the process of setting up a blog using Node.js, React, and graphql. Despite being new to these technologies, including Java and Typescript, I am seeking assistance from anyone who may be able to help. My current roadblock involves display ...

Utilizing class references within a method

I have been developing a script that is designed to dynamically load content into multiple predefined DIVs located in the topbar section of my website. Within the Topbar Object, there is an object called Ribbon which contains functions for manipulating on ...

Is there a way to dynamically update the content of <li> tag with values from an array every x seconds

I am trying to create a li list using jQuery that will update every 2 seconds with new content, similar to game news updates. However, I'm encountering an issue during the first cycle where it skips the second item in the array. The subsequent cycles ...

Unable to parse JSON due to the Identifiable model

My JSON data looks like this: [ { "name": "car", "color": "red" }, { "name": "bike", "color": "blue" }, ... etc ] Here is the model I'm using for decodi ...

What is the best way to retrieve Firebase data and assign it to a variable in React after using setState

Working with react and firebase real-time database for the first time has been quite challenging. I'm struggling to extract the data and insert it into a constant called items. It seems like the firebase code is asynchronous, which means it executes a ...

Adjusting the color of a cell based on its value

Currently, I am in the process of converting a CSV file to an HTML table by utilizing a tool available at . However, I am facing a challenge in modifying the background color of cells based on their values. I would greatly appreciate any help or guidance w ...

Swapping out a class or method throughout an entire TypeScript project

Currently, I am working on a software project built with TypeScript. This project relies on several third-party libraries that are imported through the package.json file. One such library includes a utility class, utilized by other classes within the same ...