I am trying to create a stock tracking app with knockoutjs. I have an observable array of products and I need the user to be able to add an n number of days that holds the stock count for each day.
This is my current code:
// Class to represent a row in the product grid
function product(title, category, day) {
var self = this;
self.title = title;
self.category = category;
self.day = ko.observableArray([day]);
}
// Class to represent a day column
function day(day, total, cost) {
var self = this;
self.day = day;
self.total = ko.observable(total);
self.cost = ko.observable(cost);
}
var viewModel = {
products: ko.observableArray([
new product("Name", "Category", new day('2', 10, 50)),
]),
addProduct: function(param){
//console.log(param);
this.products.push(new product("Name", "Category", new day('1', 10, 50)));
},
removeProduct: function(product) {
this.products.remove(product);
}
};
this displays the data correctly but now I want to create a function that will allow the user to add another day column to the table.
I am not sure that my approach using an observable array within another observable array is correct, or maybe there is a better solution? If this is the correct approach how would I build a function to add more day columns?
TIA!
self.addDay = function(date){elf.day.push(date);};?