Connect JSON data to a table using knockoutjs

Looking for help on binding my JSON object with knockoutjs. Here's the JSON data:

 "{\"Sport\":[{\"Name\":\"nana\",\"Description\":\"lmkmsdqd\",\"EndDate\":\"2012-07-22T00:00:00\"},
              {\"Name\":\"sfqsdffqf\",\"Description\":\"lkqjskdlqsd\",\"EndDate\":\"2012-07-22T00:00:00\"}],
    \"Music\":[{\,\"Name\":\"nana\",\"Description\":\"lmkmsdqd\",\"EndDate\":\"2012-07-22T00:00:00\"},
               {\"Name\":\"sfqsdffqf\",\"Description\":\"lkqjskdlqsd\",\"EndDate\":\"2012-07-22T00:00:00\"}]}"

Any suggestions on how to bind this properly?

Answer №1

Hey there, I've gone ahead and created a basic demo to showcase some binding concepts and how ViewModel construction works. I made some adjustments to your JSON data in order to make it work smoothly. This demo includes examples of template, foreach, and text binding. If you're new to this, I suggest checking out the knockout site tutorials.

Below are the HTML bindings:

Sports
<ul data-bind="template: { name: 'listingTemplate', foreach: sports}"></ul>
</br>
Music
<ul data-bind="template: { name: 'listingTemplate', foreach: music}"></ul>

<script type="text/html" id="listingTemplate">
<li>
    <span data-bind="text: name"></span></br>
    <span data-bind="text: description"></span></br>
    <span data-bind="text: endDate"></span></br></br>
</li>
</script>​

And here are the viewmodels:

var Listing = function(data) {
    this.name = ko.observable(data.Name || '');
    this.description = ko.observable(data.Description|| '');
    this.endDate = ko.observable(data.EndDate|| '');
};

var ViewModel = function(data) {
    this.sports = ko.observableArray(
        ko.utils.arrayMap(data.Sport, function(i) {return new Listing(i);})
    );
    this.music = ko.observableArray(
        ko.utils.arrayMap(data.Music, function(i) {return new Listing(i);})
    );
};

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

Getting the selected value from a dropdown menu in ReactJS

I am working on creating a form that resembles the following structure: var BasicTaskForm = React.createClass({ getInitialState: function() { return { taskName: '', description: '', emp ...

Navigating through JSON arrays can be achieved by utilizing iteration techniques

I'm having trouble with a script and can't figure out how to make it display in the designated div. It may have something to do with how I'm handling the response. In Chrome devtools, the response looks like this: { "[\"record one& ...

A method in JQuery to replace an input with a select element is by using the replace

I have multiple input fields with pre-existing values. My goal is to replace these inputs with select dropdowns that have the same options while retaining the original values. Here is the current HTML structure: <span id="span1"> <input type=" ...

Is it possible for me to transform this code into a useful helper function?

I am looking to optimize this conditional code by converting it into a helper function using a switch statement instead of multiple if statements. How can I achieve this in a separate file and then import it back into my component seamlessly? import { ...

View content from an external file within an iframe

My goal is to showcase a text file within an iframe that updates automatically every second. To achieve this, I have created a simple function: function refresh() { $("#derek").attr('src', $("#derek").attr('src')); }; The automati ...

Determining if a component is nested within itself in Angular 6 is a crucial task

My goal is to develop a custom Angular component for a nested navigation menu with multiple levels. Below is an example of how the menu structure looks: app.component.html <nav-menu> <nav-menu-item>Section 1</nav-menu-item> <nav- ...

Capture the contents of a table cell and store it in the clipboard

Is there a way to create a hover copy button in a table cell without actually placing the button inside the <td> tags? The goal is to allow users to easily copy text from a specific column cell by hovering over it. function myFunction() { var c ...

Transferring information from a client-side JavaScript to a Node.js Express server and receiving back

I'm currently working on implementing an upvote and downvote system in nodejs. The functionality for upvoting and downvoting will be handled by client-side JavaScript, which will then send the data to the server for storage. I'm curious to know ...

What is the quickest way to redirect a URL in WordPress?

Is it possible to redirect a URL instantly in WordPress using this script code? JavaScript Code: jQuery(document).ready(function() { var x = window.location.href; if (x == 'http://example.com/') { window.location.hr ...

Deciphering decimal numbers from a text

Is there a way to extract the floating point numbers from an array like this: var array = "-51.2132,0.3100"; I attempted to use match(/\d+/g) but I'm looking to specifically extract floating point numbers Any suggestions on the appropriate reg ...

Is there a significant distinction between value and defaultValue in React JS?

React's homepage features the final illustration (A Component Using External Plugins) with a textarea: <textarea id="markdown-content" onChange={this.handleChange} defaultValue={this.state.value} /> While typing, the ...

Prevented: Techniques for providing extra cushioning for a button, but with the condition that it contains an external icon

How can I apply padding to a button only if it contains an external icon? If the button has an external icon, I want to give it padding-right: 30px (example). However, if there is no external icon present, then the button should not have the 30px padding. ...

What is the most efficient way to retrieve multiple data simultaneously with ajax and json?

I am attempting to retrieve multiple data from a database and pass it to jQuery using AJAX and JSON, but I am facing issues as it is not working properly. Can someone assist me in resolving this? Below is the code I am currently using. Jquery $('.h ...

Adjust the class of a div element located close to its ancestor using JavaScript

I'm trying to change the class of the element #sidePanel from .compact to .expanded dynamically in this code snippet: <div id="sidePanel" class="compact"></div> <div id="topbar"> <div id="buttonContainer"> <div ...

Navigating through embedded arrays in Angular

JSON Object const users = [{ "name":"Mark", "age":30, "isActive" : true, "cars":{ Owned : ["Ford", "BMW", "Fiat"], Rented : ["Ford", "BMW", "Fiat" ...

Optimizing PHP and Mysql data for jQuery Datatable through Ajax calls

Can anyone assist me in creating a table similar to the one shown here https://datatables.net/examples/data_sources/ajax.html I have a table with close to half a million rows in MySQL. I would like to load about 10,000 rows into a jQuery Datatable using a ...

Why is the parameter declared if its value is never even used? Seems redundant

When trying to send an email, I've encountered an issue with my controller method: const send = function (subject, template, to, options) { // While VSC points out that "subject" is declared but its value is never read, // it does not signal ...

Is there a way to specify the login response details when making an Angular API request?

I am currently using Angular to connect to my backend login service. However, I am facing an issue with setting the popup message when the username or password is incorrect. I want to display the detailed message from the API on my login page when any erro ...

Converting an AngularJS Array to Firebase JSON Format

Currently, in my application, I am working on creating an array using JS. $scope.items = []; As I add items to this array, I retrieve information from a Firebase Reference and populate the items array with it. es.addItem = function(item){ var produc ...

Utilize JavaScript to seamlessly play a Spotify track within the Spotify client without disrupting your current

A little while back, I recall the simplicity of clicking play on a song within a website and having it instantly start playing on my computer without any additional steps. It was seamless and effortless. My goal for my website is to have music start playi ...