我是淘汰赛的新人,我正在尝试制作一张桌子,当我们点击“卸妆”时它会删除该行,但是当我点击“Adicionar”时,我无法向表中添加任何行。如果有人可以帮助我,我会很感激。
<html>
<head>
<title> Inseminações </title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id='cssmenu'>
<ul>
<li><a href="inseminações.html">Inseminações</a>
<li><a href="tratamentos.html">Tratamentos</a>
<li><a href="analises.html">Análises Clínicas</a>
</ul>
</div>
<br>
<h1> Inseminações </h1>
<table>
<thead><tr>
<th>Data</th>
<th>SIA Vaca</th>
<th>SIA Touro</th>
<th>Nome do Touro</th>
<th>Documento</th>
<th>Inseminador</th>
<th> </th>
</tr></thead>
<tr>
<tbody data-bind="foreach: dtabela">
</tr>
<tr>
<td data-bind="text: Data"></td>
<td data-bind="text: SIAV"></td>
<td data-bind="text: SIAT"></td>
<td data-bind="text: NomeT"></td>
<td data-bind="text: Doc"></td>
<td data-bind="text: Ins"></td>
<td><button data-bind='click: $root.removeDados'>Remover</button></td>
</tr>
</tbody>
<tbody>
<tr>
<td><input data-bind='value: Data'/></td>
<td><input data-bind='value: SIAV'/></td>
<td><input data-bind='value: SIAT'/></td>
<td><input data-bind='value: NomeT'/></td>
<td><input data-bind='value: Doc'/></td>
<td><input data-bind='value: Ins'/></td>
<td><button data-bind='click: $root.addDados'>Adicionar</button></td>
</tr>
</tbody>
</table>
<script type='text/javascript' src='knockout-3.0.0.js'></script>
<script type='text/javascript'>
function Dados(Data,SIAV,SIAT,NomeT,Doc,Ins) {
this.Data = ko.observable(Data);
this.SIAV = ko.observable(SIAV);
this.SIAT = ko.observable(SIAT);
this.NomeT = ko.observable(NomeT);
this.Doc = ko.observable(Doc);
this.Ins= ko.observable(Ins);
}
function DadosViewModel ()
{
var self= this
this.dtabela=ko.observableArray([
new Dados("30-01-13", 354, 564, "Touro 1", "Doc1", "Inseminador A"),
new Dados("12-05-13", 785, 344, "Touro 3", "Doc5", "Inseminador B"),
]);
this.removeDados=function(Dados) {
self.dtabela.destroy(Dados);
};
this.addDados=function(Dados) {
self.dtabela.push( new Dados);
};
};
var vm= new DadosViewModel();
ko.applyBindings(vm);
</script>
</body>
</html>
答案 0 :(得分:1)
好的,我让你的样本正常工作,你可以在这里看到一个演示:http://jsfiddle.net/ThvdF/2/
我必须做出以下修改
您没有viewmodel属性来表示新项目,我添加了一个:
function DadosViewModel ()
{
var self= this
....
this.newDados = ko.observable( new Dados(); ) // Here
您有2个tbody
代码,我将其更改为tfoot
并使用with
绑定将页脚行绑定到newDados
viewmodel属性< / p>
<tfoot data-bind="with: newDados">
将此实例传递给addDados
方法时,我重新创建了newDados
function DadosViewModel ()
{
...
this.addDados=function(d) {
self.dtabela.push( d );
self.newDados( new Dados() );
}