编辑最后一行时自动向表添加新行
当用户开始在最后一行中输入时,我试图在表的末尾添加一个新行。 viewmodel 看起来像这样:
function tableRow(number, ownerViewModel) {
this.number = ko.observable(number);
this.remove = function () { ownerViewModel.tableRows.destroy(this); }
ko.dependentObservable(function () {
var curval = this.number();
var rows = ownerViewModel.tableRows();
if (rows.length > 0) {
if (curval != '' && this == rows[rows.length - 1]) {
ownerViewModel.addNewRow();
}
}
}, this);
}
function tableRowsViewModel() {
var that = this;
this.tableRows = ko.observableArray([]);
this.addNewRow = function () {
this.tableRows.push(new tableRow('', that));
}
this.addNewRow();
}
$(document).ready(function () {
ko.applyBindings(new tableRowsViewModel());
});
这是 html:
<table>
<thead>
<tr>
<th>
Number
</th>
<th>
</th>
</tr>
</thead>
<tbody data-bind="template:{name:'tableRow', foreach: tableRows}">
</tbody>
<tfoot>
<tr>
<td colspan="4">
<button type="button" data-bind="click: addNewRow">
add row
</button>
</td>
</tr>
</tfoot>
</table>
<script id="tableRow" type="text/html">
<tr>
<td>
<input type="text" style="width:40px;" data-bind="value: number, valueUpdate: 'keyup'" />
</td>
<td>
<button type="button" data-bind="click: function(){ $data.remove(); }">
delete
</button>
</td>
</tr>
</script>
我还在 tableRow ko.dependentObservable 函数中插入了 alert()
:
ko.dependentObservable(function () {
alert('test');
var curval = this.number();...
当 tableRows 数组包含 2 个元素时,这个函数似乎被触发了 5 次,当数组中有 3 个元素时为 6 次,依此类推。
我这样做对吗?
I'm trying to add a new row to the end of the table when user starts typing in last row.
The viewmodel looks like this one:
function tableRow(number, ownerViewModel) {
this.number = ko.observable(number);
this.remove = function () { ownerViewModel.tableRows.destroy(this); }
ko.dependentObservable(function () {
var curval = this.number();
var rows = ownerViewModel.tableRows();
if (rows.length > 0) {
if (curval != '' && this == rows[rows.length - 1]) {
ownerViewModel.addNewRow();
}
}
}, this);
}
function tableRowsViewModel() {
var that = this;
this.tableRows = ko.observableArray([]);
this.addNewRow = function () {
this.tableRows.push(new tableRow('', that));
}
this.addNewRow();
}
$(document).ready(function () {
ko.applyBindings(new tableRowsViewModel());
});
And here is html:
<table>
<thead>
<tr>
<th>
Number
</th>
<th>
</th>
</tr>
</thead>
<tbody data-bind="template:{name:'tableRow', foreach: tableRows}">
</tbody>
<tfoot>
<tr>
<td colspan="4">
<button type="button" data-bind="click: addNewRow">
add row
</button>
</td>
</tr>
</tfoot>
</table>
<script id="tableRow" type="text/html">
<tr>
<td>
<input type="text" style="width:40px;" data-bind="value: number, valueUpdate: 'keyup'" />
</td>
<td>
<button type="button" data-bind="click: function(){ $data.remove(); }">
delete
</button>
</td>
</tr>
</script>
I've also inserted alert()
in tableRow ko.dependentObservable function:
ko.dependentObservable(function () {
alert('test');
var curval = this.number();...
It seems like this function is fired 5 times when tableRows array contain 2 elements, 6 times when there are 3 elements in array and so on.
Am I doing this right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次添加行时,您添加到每个行对象的 dependentObservable 都会触发,因为它们依赖于
tableRows
。因此,每个人都在做一些工作来确定它们是否是最后一行。如果是最后一行,则添加新行。另一种方法是使用单个 dependentObservable 来表示最后一行的值。然后,您可以订阅该 dependentObservable 的更改,检查它是否有值,并在必要时添加一行。它看起来像: http://jsfiddle.net/rniemeyer/F5F8S/
另外,这里是我从 KO 论坛获得的示例显示添加一行,如果最后两行为空,还删除最后一行,以防有帮助:http://jsfiddle.net/rniemeyer/MzGDr/
The dependentObservable that you are adding to each row object is firing each time that a row is added, because they depend on
tableRows
. So, each one is doing some work to determine if they are the last row. If it is the last row, then a new row is added.An alternative is to use a single dependentObservable that represents the last row's value. Then, you can subscribe to changes to that dependentObservable, check if it has a value, and add a row when necessary. It would look something like: http://jsfiddle.net/rniemeyer/F5F8S/
Also, here is a sample that I had from the KO forums that shows adding a row and also removing the last row if the last two are empty in case it helps: http://jsfiddle.net/rniemeyer/MzGDr/