[英]How do i push elements to javascript object in knockout
I am trying to learn knockout.I am trying to learn foreach binding in knockout . 我正在尝试学习淘汰赛。我试图学习foreach在淘汰赛中的约束力。
First I declared a javascript object 首先,我声明了一个javascript对象
var people = function()
{
var self = this;
this.firstname = ko.observable("")
this.lastname = ko.observable("")
}
I tried to push elements into the javacript object 我试图将元素推入javacript对象
var x = new people(); var x = new people();
x.push({firstName: "bob" ,lastName:'gill'});
I added viewmodel 我添加了viewmodel
function TestViewModel()
{
var self = this;
this.person = ko.observableArray({people});
}
created an instance of viewmodel and pushed the elements 创建了一个viewmodel实例并推送了元素
var viewmodel = new TestViewModel();
viewmodel.person.push(x);
Finally binded the elements 最终绑定元素
ko.applyBindings(viewmodel);
Here is the JSFIDDLE I trying to achieve 这是我尝试实现的JSFIDDLE
http://jsfiddle.net/GSvnh/5592/ http://jsfiddle.net/GSvnh/5592/
I want the following output 我想要以下输出
firstname lastname
bob marley
tom brady
George clooney
You have a couple issues to address in your JSFiddle: 您需要在JSFiddle中解决几个问题:
thead
HTML is missing a closing >
thead
HTML缺少结尾>
firstname
vs firstName
) firstname
vs firstName
) Here's an updated JSFiddle that implements a basic foreach example: http://jsfiddle.net/GSvnh/5593/ 这是一个更新的JSFiddle,它实现了一个基本的foreach示例: http : //jsfiddle.net/GSvnh/5593/
To answer it, follow an example, which I have an amount of information into a base of data and I will push it into a data table in my front-end. 要回答这个问题,请遵循一个示例,该示例将大量信息存储到数据基中,并将其推入前端的数据表中。
function tableData(){
<cfoutput>
var newDados = #serializeJSON(mySQLData)#;
var dataABC = newDados.ABC;
var dataXYZ = newDados.XYZ;
</cfoutput>
//cfoutput is a Cold Fusion parameter to receive data from SQL.
In this case I serialize newDados to an object. 在这种情况下,我将newDados序列化为一个对象。 ABC and XYZ are objects sons of newDados.
ABC和XYZ是newDados的对象。
In this case, I want to mix and push both objects child in a single object to work these parameters together. 在这种情况下,我想将两个对象的子对象混合并推入一个对象中,以一起使用这些参数。 So I use a
forEach
function: 所以我使用了
forEach
函数:
allData = new Array();
dadosABC.forEach(function(obj){
allData.push(obj);
});
XYZ.forEach(function(obj){
allData.push(obj);
});
//Here allData have already receiving data of both newDados object child
allDataSize = allData.length;
for (var i = 0; i < sizePreenc; i++) {
allData[i] = new Array();
tableData[i].push(allData[i]);
tableData[i].push(allData[i].firstName);
tableData[i].push(allData[i].lastName);
tableData[i].push(allData[i].age);
tableData[i].push(allData[i].gender);
};
drawGrid('');
};
This way, I push my datas to table, or whatever I want to. 这样,我将数据推送到表或任何我想要的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.