i'm using jquery quicksearch to search a table that is being populated by knockout foreach loop. the quicksearch element needs to be initiate after the foreach finishes. I've tried several approaches, but was unsuccessful so far.
I've tries using 'afterRender', but was unable to determine if the current item is the last item on the collection, i've also tried using a bindingHandlers, but then i got a collection of length 0 instead of length 2005.
so:
- what's the best approach to finding the last element in a foreach loop ?
- what's the best way to implement it in this specific scenario?
here's my view:
<tbody data-bind="foreach: containers">
<tr>
<td><span data-bind="text: code"></span></td>
<td><span data-bind="text: typeName"></span></td>
<td><span data-bind="text: parentClient"></span></td>
<td><span data-bind="text: client"></span></td>
<td>
<a data-bind="attr: { onclick: deleteUrl }">
<i class="icon-trash"></i>@delete </a>
|
<a data-bind="attr: { href: editUrl }">
<i class="icon-edit"></i>@edit</a>
|
<a data-bind="attr: { href: qrUrl }" target="blank">
<i class="icon-qrcode"></i>
@printQr
</a>
</td>
</tr>
</tbody>
and here's my knockout code:
function Container(data) {
var self = this;
self.id = ko.observable(data.Id);
self.code = ko.observable(data.Code);
self.typeName = ko.observable(data.TypeName);
self.parentClient = ko.observable(data.ParentClient);
self.client = ko.observable(data.Client);
self.deleteUrl = ko.computed(function () {
return "GetModal('/Containers/Delete/" + data.Id + "','containerModal');";
});
self.editUrl = ko.computed(function () {
return '/Containers/Edit/' + data.Id;
});
self.qrUrl = ko.computed(function () {
return '/Qr/Index/10?entity=' + data.Id;
});
}
function ContainersViewModel() {
var self = this;
self.containers = ko.observableArray([]);
self.counter = ko.computed(function () {
return self.containers().length;
});
$.getJSON("/Containers/Json", function (data) {
var containers = $.map(data, function (item) {
return new Container(item);
});
self.containers(containers);
});
};
ko.applyBindings(new ContainersViewModel());
Thanks a lot !
Nir