title | page_title | description |
---|---|---|
ObservableArray |
API Reference for ObservableArray wrap in Kendo MVVM and Kendo DataSource |
How to create a new ObservableArray, explanations how to use kendo.observable methods, fields and events. |
The ObservableArray
wraps an existing Array object with change tracking capabilities. It is used by Kendo MVVM and
the kendo.data.DataSource. Inherits from kendo.Observable.
To create a new ObservableArray
, use its constructor or the kendo.observable
method.
<script>
var array = new kendo.data.ObservableArray([
{ name: "John Doe" },
{ name: "Jane Doe" }
]);
console.log(array[0].name); // outputs "John Doe"
console.log(array.length); // outputs "2"
</script>
<script>
var observable = kendo.observable({
people: [
{ name: "John Doe" },
{ name: "Jane Doe" }
]
});
console.log(observable.people instanceof kendo.data.ObservableArray); // outputs "true"
</script>
Important: The
kendo.data.ObservableArray
wraps its items kendo.data.ObservableObject instances (unless the items are of primitive type).
<script>
var complex = new kendo.data.ObservableArray([
{ name: "John Doe" },
{ name: "Jane Doe" }
]);
console.log(complex[0] instanceof kendo.data.ObservableObject); // outputs "true"
var primitive = new kendo.data.ObservableArray([
"John Doe", "Jane Doe"
]);
console.log(typeof (primitive[0]) ); // outputs "string"
</script>
An unsigned, 32-bit integer that specifies the number of items in an ObservableArray
.
<script>
var array = new kendo.data.ObservableArray([1, 2, 3]);
for (var i = 0; i < array.length; i++) {
console.log(array[i]); // outputs the current item
}
</script>
Attaches an event handler for the specified event.
<script>
var array = new kendo.data.ObservableArray([1, 2]);
array.bind("change", function(e) {
console.log("changed");
});
array.push(3); // raises the "change" event and the handler outputs "changed"
</script>
The name of the event.
The function which will be invoked when the event is raised.
Joins all items of an ObservableArray
into a string. Equivalent of
Array.prototype.join.
Specifies the string to separate each item of the array. If omitted the array items are
separated with a comma (,
)
<script>
var array = new kendo.data.ObservableArray([1, 2, 3]);
console.log(array.join("-")); // outputs "1-2-3"
</script>
Gets the parent of the array if such parent exists.
kendo.data.ObservableObject
the parent of the array; undefined
if the array is not nested and doesn't have a parent.
<script>
var array = new kendo.data.ObservableArray([1, 2]);
console.log(array.parent()); // outputs "undefined"
var observable = kendo.observable({ numbers: [1, 2] });
var numbers = observable.get("numbers");
console.log(numbersperson.parent() === observable); // outputs "true"
</script>
Removes the last item from an array and returns that item. Equivalent of Array.prototype.pop.
Important: The
pop
method raises the change event. Theaction
field of the event argument is set to"remove"
. Theitems
field of the event argument is the array that contains the removed item.
Object
the item which was removed.
<script>
var array = new kendo.data.ObservableArray([{ name: "John Doe" }]);
var result = array.pop();
console.log(array.length); // outputs "0"
console.log(result.get("name")); // outputs "John Doe"
</script>
Appends the given items to the array and returns the new length of the array. Equivalent of Array.prototype.push.
The new items are wrapped as ObservableObject
if they are complex objects.
Important: The
push
method raises the change event. Theaction
field of the event argument is set to"add"
. Theitems
field of the event argument is the array that contains the appended items.
Number
the new length of the array.
The item(s) to append to the array.
<script>
var array = new kendo.data.ObservableArray([{ name: "John Doe" }]);
var length = array.push({ name: "Jane Doe" });
console.log(length); // outputs "2"
console.log(array[1] instanceof kendo.data.ObservableObject); // outputs "true"
console.log(array[1].get("name")); // outputs "Jane Doe"
</script>
<script>
var array = new kendo.data.ObservableArray([ 1 ]);
var length = array.push(2, 3);
console.log(length); // outputs "3"
console.log(array[1]); // outputs "2"
</script>
Returns a one-level deep copy of a portion of an array. Equivalent of
Array.prototype.slice.
The result of the slice
method is not an instance of ObvservableArray
. It is a regular JavaScript Array object.
Important: The
slice
method does not modify the originalObservableArray
.
Zero-based index at which to begin extraction.
Zero-based index at which to end extraction. If end
is omitted, slice
extracts to the
end of the sequence.
<script>
var array = new kendo.data.ObservableArray([1, 2, 3]);
var firstAndSecond = array.slice(0, 2);
console.log(firstAndSecond); // outputs [1, 2]
</script>
Changes an ObservableArray
, by adding new items while removing old items. Equivalent of
Array.prototype.splice
Important: The
splice
method raises the change event once or twice depending on the change. Theaction
field of the event argument is set to"add"
(if items are added) or"remove
(if items are removed). Theitems
field of the event argument is the array that contains the appended items or removed items. In the above example thechange
event will be raised two times - first because "baseball" is removed and second because "tennis" and "hockey" are added.
Array
containing the removed items. The result of the splice
method is not an instance of ObvservableArray
.
Index at which to start changing the array. If negative, will begin that many elements from the end.
An integer indicating the number of items to remove. If set to 0, no items are removed. In this case, you should specify at least one new item.
The items to add to the array. If you don't specify any items, splice
simply removes items from the array.
<script>
var sports = new kendo.data.ObservableArray(["football", "basketball", "volleyball"]);
var removed = sports.splice(1, 1, "tennis", "hockey");
console.log(removed); // outputs ["basketball"]
console.log(sports); // outputs ["football", "tennis", "hockey", "volleyball"]
</script>
Removes the first item from an ObvservableArray
and returns that item. Equivalent of Array.prototype.shift.
Important: The
shift
method raises thechange
event. Theaction
field of the event argument is set to"remove"
. Theitems
field of the event argument is an array that contains the removed item.
Object
the item which was removed.
<script>
var array = new kendo.data.ObservableArray([1, 2, 3]);
var removed = array.shift();
console.log(removed); // outputs "1"
console.log(array.length); // outputs "2"
</script>
Returns a JavaScript Array which represents the contents of the ObservableArray
.
<script>
var people = new kendo.data.ObservableArray([
{ name: "John Doe" },
{ name: "Jane Doe" }
]);
var json = people.toJSON();
console.log(JSON.stringify(json)); // outputs [{"name":"John Doe"},{"name":"Jane Doe"}]
</script>
Adds one or more items to the beginning of an ObservableArray
and returns the new length. Equivalent of Array.prototype.unshift.
Important: The
unshift
method raises the change event. Theaction
field of the event argument is set to"add"
. Theitems
field of the event argument is an array that contains the new items.
Number
the new length of the array.
The items to add to the beginning of the ObservableArray
.
<script>
var array = new kendo.data.ObservableArray([2, 3]);
var result = array.unshift(0, 1);
console.log(result); // outputs "4"
console.log(array); // outputs [0, 1, 2, 3]
</script>
Fired when the items of the ObservableArray
change in some way.
Specifies the type of change. Possible values are: "add"
- items are added to the ObservableArray
, "itemchange"
- a field of an item changed, "remove"
- items are removed from the ObservableArray
.
The index at which items are removed or added. Set to undefined
if e.action
is "itemchange"
.
The items which were changed.
The name of the field of an item that changed. Available only when e.action
is "itemchange"
.
<script>
var array = new kendo.data.ObservableArray([1, 2, 3]);
array.bind("change", function(e) {
console.log(e.action, e.index, e.items);
});
array.push(4, 5); // outputs "add", 3, [4, 5]
array.pop(); // outputs "remove", 4, [5]
var people = new kendo.data.ObservableArray([{ name: "John Doe" }]);
people.bind("change", function(e) {
console.log(e.action, e.field, e.items[0].get("name"));
});
people[0].set("name", "Jane Doe"); // outputs "itemchange", "name", "Jane Doe"
</script>