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?
Select the entire row which you want to insert a blank row above, and press Shift + Ctrl + + keys together, then a blank row is inserted.
To insert a single row: Right-click the whole row above which you want to insert the new row, and then select Insert Rows. To insert multiple rows: Select the same number of rows above which you want to add new ones. Right-click the selection, and then select Insert Rows.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With