KnockoutJS Appending To the Beginning of observableArray
I have a ViewModel
where I retrieve data from the server and that new data is concatenated with the data I already have. The problem is, the new data goes after the old data and I want it the other way around so the fresh data shows at the top of the view .
This is what I have:
self.serverData(self.serverData().concat(newData));
and the view is displayed as:
Prepend observable arrays using the unshift function. It will modify the underlying array and tell the observable it was updated.
var x = ko.observableArray([1,2,3]);
x.unshift(0);
// x() returns [0,1,2,3]
您可以使用不移位或拼接
function viewModel() {
var self = this;
self.x = ko.observableArray([1, 2, 3]);
self.x.unshift(0);
self.y = ko.observableArray([1, 2, 3]);
self.y.splice(0, 0, 0);
}
ko.applyBindings(new viewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div data-bind="foreach: x">
<span data-bind="text: $data"> </span>
</div>
yyyyyyy
<div data-bind="foreach: y">
<span data-bind="text: $data"> </span>
</div>
链接地址: http://www.djcxy.com/p/19372.html