Knockout JS: Create Json from ViewModel Observable Array


kaka1234

I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it to my mvc controller in json format.

The table settings are all ok. My only problem is creating json from observable array. Here is my fiddle:

https://jsfiddle.net/4djn2zee/1/

If you click Add one or more rows, then enter data in both rows. If you press the save button, you can observe the following:

self.valuesData()

Now, if you see this value in the console, the result is:

(2) [ValuesData, ValuesData]

To expand further:

(2) [ValuesData, ValuesData]
0:ValuesData {ID: "1", Co1: "2", Col2: "3", Col3: "4", Col4: "5", …}
1:ValuesData {ID: ƒ, Co1: ƒ, Col2: ƒ, Col3: ƒ, Col4: ƒ, …}
length:2
__proto__:Array(0)

Since I added 2 rows, I can see 2 arrays.

Now if you see above, I can see my data in the first row id, col1, col2, etc. The problem I'm having is how to get the data from the observable and construct my json.

I expect my json to look like this:

{
"ID": "1",
"Co1": "2",
"Col2": "3",
"Col3": "4",
"Col4": "5",
"Col5": "6",
"Comment": "7"
},
{
"ID": "8",
"Co1": "9",
"Col2": "10",
"Col3": "11",
"Col4": "12",
"Col5": "13",
"Comment": "14"
}

renew:

I tried as follows:

    var dataToSave = $.map(self.valuesData(), function(item) {
    var jsonToSend = {};



    return jsonToSend;
  });

Again as stated on the first row, I can see the data, but not sure how to get the data from the second row and beyond.

Here is my updated fiddle:

https://jsfiddle.net/4djn2zee/3/

thomas music

Try this:

var dataToSave = ko.toJSON(self.valuesData);

Related


Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Knockout JS: Create Json from ViewModel Observable Array

kaka1234 I have an HTML table whose columns are dynamically created. There are Add Row buttons to add rows to the table and Delete Row buttons to delete rows. Finally, there is a "save" button where I want to get all the data from the input table and send it t

Create observable knockout array from JSON response

Zonus C#/ MVC / Knockout / JSON I have the following javascript: function Feed(data) { this.ID = ko.observable(data.ID); this.RSSName = ko.observable(data.RSSName); alert(data.RSSName + " " + data.ID); } function ViewModel() { self = this;

Knockout.js cannot map observable array from json

last name Unable to map json to observable array. I used the code from the tutorial ( http://learn.knockoutjs.com/ ). function Movie(data) { this.name = ko.observable(data.name); this.description = ko.observable(data.description); this.duration = k

Knockout.js cannot map observable array from json

last name Unable to map json to observable array. I used the code from the tutorial ( http://learn.knockoutjs.com/ ). function Movie(data) { this.name = ko.observable(data.name); this.description = ko.observable(data.description); this.duration = k

Create pdf from Knockout JS view & viewModel

Andrew Scenario: In our application, a user can create an invoice by filling in certain fields in the knockout view. This invoice can be previewed through another "Knockout" page. I want to use the preview url in our PDF creator (EVOPdf), so we can serve the P

Knockout associates an observable array with another ViewModel

fluid change I'm building a simple chat application with 2 main components: Conversation (Session) and User. User has the following properties: id, name,status Convos have the following properties: id, name,users In convos, it usersis observableArraysaid id"Pa

Mapping JSON to Knockout observable array

Mohsin JK First Web Service: (User) [{id: 1, username: "Jhon"}] Second web service: (message) [{id: 34, message: "This is a message 1", UserId: 1}, {id: 35, message: "This is a message 2", UserId: 1} How can I map above the two ko.obervableArrays so that I can

Mapping JSON to Knockout observable array

Mohsin JK First Web Service: (User) [{id: 1, username: "Jhon"}] Second web service: (message) [{id: 34, message: "This is a message 1", UserId: 1}, {id: 35, message: "This is a message 2", UserId: 1} How can I map above the two ko.obervableArrays so that I can

Create a new Observable from array in JSON response

John Cartwright I'm using Angular 7 and have an httpclient request that returns a JSON response containing a nested array. I'm having trouble returning an array element as an Observable instead of a single object. The code below extracts the array and returns

Create a new Observable from array in JSON response

John Cartwright I'm using Angular 7 and have an httpclient request that returns a JSON response containing a nested array. I'm having trouble returning an array element as an Observable instead of a single object. The code below extracts the array and returns

Knockout JS: Pass observable array as parameter

trunk I have a 2d observable array called textFields: var t1 = ko.observableArray([{val: 0}, {val:0}]) self.textFields = ko.observableArray([t1]); I have a function called Solve that should take one parameter: self.solve = function(arr){ console.log(arr); }

Knockout JS: Passing observable array as argument

tomet I have a 2d observable array called textFields: var t1 = ko.observableArray([{val: 0}, {val:0}]) self.textFields = ko.observableArray([t1]); And I have a function called solve that is supposed to take an argument: self.solve = function(arr){ console.log

Create table from Json response using Knockout.js

Neural Networks I am trying to create table using Json response and Knockout.js 用JavaScript $(document).ready(function() { $.ajax({ method : "POST", url : "devTestServlet", success : function(data) { ko.applyBindings({

Bind Asp.Net MVC json result to knockout.js observable array

Farhad Jameel I have some hardcoded values in my asp.net mvc controller. The GetPlanList()return JsonResultthis is to read in the viewmodel.js file and assign it to the ko.observableArray()data and then bind it to the table. The problem I'm having is how do I

Bind Asp.Net MVC json result to knockout.js observable array

Farhad Jameel I have some hardcoded values in my asp.net mvc controller. The GetPlanList()return JsonResultthis is to read in the viewmodel.js file and assign it to the ko.observableArray()data and then bind it to the table. The problem I'm having is how do I

Knockout Observable Array of Observable Array

Craig I have passed a model from .Net to my view model which is an object with several fields and a list, each field has another list. So, an object with a list of lists. In my view, I represent this as some data, with a list of tabs (the first list), and then

Knockout mapping Json Array to Observable Array getting error

Andreas Klumpe I'm having trouble mapping a json array to an observable array. I get an error "Cannot read property 'fromJSON' of undefined" Here is the knockout script I linked: Knockout 3.2.0.js Knockout.mapping-latest.js Knockout.mapping-latest.debug.js My