我正在使用数组对象和淘汰赛 js。下面的一些代码使该数据可用于绑定并且一切正常。但是当我试图在数组行中推送一些数据时,我收到了这个错误消息:
未捕获的类型错误:无法调用未定义 ADMIN_gym_demo3.html:217 的方法“推送”
现在我想不通为什么那个结构不起作用。
Objects = {days: [
{day: 'Monday', row:[{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
},{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
},{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
}]},{//////////////////////////////////////////////////////////////
day: 'Tuesday', row:[{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
},{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
},{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
}]},{//////////////////////////////////////////////////////////////
day: 'Wednesday', row:[{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
},{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
},{
col1: '100', col2: '200', col3: '300', col4: '400', col5: '500'
}]}///////////////////////////////////////////////////////////////
]};
var ViewModel = function(){
var self = this;
cont = ko.mapping.fromJS(Objects);
alld = cont.days;
thisrow = self.row;
crow = alld.thisrow;
val1 = ko.observable('');
val2 = ko.observable('');
val3 = ko.observable('');
val4 = ko.observable('');
val5 = ko.observable('');
add = function(){
var vm1 = val1;
var vm2 = val2;
var vm3 = val3;
var vm4 = val4;
var vm5 = val5;
crow.push({
col1: vm1(), col2: vm2(), col3: vm3(), col4: vm4(), col5: vm5() });
};
};
ko.applyBindings(new ViewModel);
html 的样子:
<div data-bind="foreach: alld">
<p data-bind="text: day"></p>
<table class="table table-striped table-condensed" >
<thead>
<tr>
<th>time</th>
<th>training time</th>
<th>difficulty</th>
<th>coach</th>
<th>gym</th>
</tr>
</thead>
<tbody data-bind="foreach: row">
<tr >
<td data-bind="text: col1">7.00-10.00</td>
<td data-bind="text: col2">bodypump</td>
<td data-bind="text: col3">I-III</td>
<td data-bind="text: col4">Michael Ivanov</td>
<td data-bind="text: col5">01</td>
</tr>
</tbody>
</table>
<div style="width: 100%" >
<div style="display: inline; float: left; width: 20%" >
<input data-bind="value: val1" />
</div>
<div style="display: inline; float: left; width: 20%">
<input data-bind="value: val2" />
</div>
<div style="display: inline; float: left; width: 20%">
<input data-bind="value: val3" />
</div>
<div style="display: inline; float: left; width: 20%">
<input data-bind="value: val4" />
</div>
<div style="display: inline; float: left; width: 20%">
<input data-bind="value: val5" />
</div>
<a href="#" data-role="button" data-icon="star" data-iconpos="right" data-mini="true" data-bind="click: add" >Add row</a>
</div>
有人可以告诉我我的错误在哪里吗?这是我的jsfiddle:http: //jsfiddle.net/uhtshka/BWxm7/3/