Convert JSON data in an array with multiple elements into an inline function using json2html

I am currently working with JSON data that looks like this:

{"orders":[
{"id":16,"status":"completed","total":"45.00"},
{"id":17,"status":"completed","total":"55.00"}
]}

My goal is to convert this data into HTML using json2html. While ${orders.0.total} works for the first array item, I want it to transform all orders, not just the one at index 0.

I attempted a solution found in this answer, but unfortunately, it did not work as expected.

This is what my current implementation looks like:

<body>
    <ul id="list"></ul>
</body>

<script type="text/javascript">
    //List items
    var myjson = [{"data":[
                 {"id":16,"status":"completed","total":"45.00"},
                 {"id":17,"status":"completed","total":"55.00"}
                 ]}];

    //List item transform
    var orderTransform = {"tag":"div","html":"${total}"}

    var transform = {"tag":"div","children":function(){
        return( json2html.transform(this,orderTransform) );
    }};

    $(function(){
        //Create the list
        $('#list').json2html(myjson,transform);
    });
</script>

Thank you.

Answer №1

Please update your transform code to the following:

var transform = {"tag":"div","children":function(){
        return( json2html.transform(this.orders,orderTransform) );
}};

Be sure to change this to this.orders, as the main transform is referencing the {orders: [...]} object. Therefore, it is necessary to specify the new array being used as the data within the children function.

The initial code was passing this as the data for the children, causing json2html to try and render the {orders: [...]} object with the orderTransform, which is incorrect. It's crucial to explicitly state that the data for that transform lies within the orders field.

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 causing my HTML script tag to not recognize my JS file in Next.js?

Just starting out with Next.js and trying to access the web cam for a project I'm working on, but encountering an issue when writing in the "script" tag. Below is the simple code for page.js: export default function Live(){ return( <html> ...

Issue with React Form failing to connect to Express Server

I am currently facing an issue with my express backend. It is supposed to make a GET request to the OpenWeather API when a POST request is made from a React form. However, I believe the problem lies in the backend POST route as it is not being called corre ...

Microsoft Edge browser incorrectly calculates range.endOffset value

This particular problem is specific to the Microsoft Edge browser. I am attempting to apply a CSS style to a selected word using Range API's, but I am encountering an issue with the range.endOffset functionality in Edge. Below is the code snippet I am ...

When the pagecontainer is displayed

I am in the process of developing a JQM app. It is designed as a multiple page application that utilizes jquery mobile page divs to manage the visibility of pages during navigation. The layout of the pages looks something like this: <div data-role="p ...

Node.js MySQL REST API query fails to execute

I am developing a login/sign up API using nodejs, express, and mysql. Despite receiving the "Successful Sign Up!" message without any errors during testing, the user table in the database remains empty. Below is the query I am attempting to execute: con. ...

Unusual behavior observed in Angular.js using ng-pattern

I've been working on creating a form that can accept Twitter parameters like # and @ to display a Twitter feed. Initially, I intended to use the ng-pattern directive in Angular.js to validate the input before saving. However, the validation process i ...

Option list malfunctioning in Safari, Mozilla, as well as Chrome browsers

I encountered some issues while working on the front-end of my web app. Here are a few problems: I need to truncate text if it's too long, so I use the following CSS: .suggestion-box-text { white-space: nowrap; overflow: hidden; text-overfl ...

What is the process for activating the appropriate image when clicking on the accordion?

I currently have three accordions on the left side and three images on the right side, but this may grow in the future. My goal is to have the first accordion open and display the first image when the page loads. When the user clicks on the second accordio ...

Discovering the hidden href tags using the "::before" selector in selenium

I am attempting to retrieve a URL from a PLP and visit each element to extract specific keywords from the PDP, then store them in a JSON file. However, I am only getting one data back from the list, leading me to believe that the website may be blocking th ...

Issue TS2349 occurs when attempting to use a combination of boolean and function types within union typing

In my class, there is a property called "isVisible" which can be either a boolean value or a function that returns a boolean. The code snippet below demonstrates what I am currently using. It works fine and achieves the desired result, but during compilat ...

JavaScript generic type for the superclass

Is it possible to create an extendable parent class with a generic type in JavaScript, similar to how it's done in Java? public class Parent<T> { private T t; public T get() { return t; } ... If so, what would the child class l ...

Is there a way to identify a change in the URL using JQuery?

My goal is to clear the localStorage when a user navigates to a different page. For instance, if I am currently on . When the user goes to the URL, , I want to clear the localStorage. This is my script using JQuery. $(window).unload(function(){ if ...

Creating a Cubic Bezier Curve connecting two points within a 3D sphere using three.js

I'm currently working on a project where the user can click on two points on a sphere and I want to connect these points with a line along the surface of the sphere, following the great circle path. I have managed to obtain the coordinates of the sele ...

Looking for assistance in troubleshooting a JSON response issue

I am struggling to send the response in the desired format {"files":[{"webViewLink":""},{"webViewLink":""}]} However, I'm receiving a response that looks like this {"files":[{"webViewLi ...

The component "SafeAreaViewRN" could not be located within the UIManager

Upon attempting to open a bundle on my Android device, I encountered the following error message: A warning was displayed stating that the app was accessing a hidden field in android's view accessibility delegate. Additionally, an Invariant Violati ...

Transforming a List of Items into a Hierarchical Tree Structure

Seeking assistance with constructing a hierarchical tree structure from a flat list that contains categories and names. Various approaches have been attempted, including the function presented below. The original flat list looks as follows: var input = [ ...

Utilizing JSON with AJAX to dynamically fetch data on a separate webpage results in the page reloading despite implementing the e.preventDefault() method

Looking to retrieve a value on a new page and navigate without refreshing? I'm utilizing the POST method here along with JSON to fetch values. Still learning the ropes of this Ajax code! My goal is to move from the team.php controller page to team_d ...

Encountering a problem when trying to reference socket.io

I've been working on building an express app with chat functionality, but I've run into an issue with socket.io. It's not working properly and is throwing an "uncaught reference" error when I try to run the server. You can see a screenshot o ...

Calculate the total of all the numerical data within a collection of objects stored in an array

I have an array of objects structured like this: data: [ a:[ {keyone:'a', keytwo: 'anna', keythree: 23, keyfour: 15}, {keyone:'a', keytwo: 'anna', keythree: 23, keyfour: 15}, ...

Is there a way to modify the output Json during serialization on a broader scale, even if the class definitions are not modifiable?

Suppose the following: You have a collection of Java objects that need to be passed to an API You are unable or unwilling to modify the declaration of these objects Unfortunately, the API requires additional information not present in the objects For in ...