title | page_title | description | res_type |
---|---|---|---|
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. |
api |
The ObservableArray
wraps an existing Array
object with change-tracking capabilities. It is used by Kendo UI MVVM design pattern 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" }
]);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array[0].name); // outputs "John Doe"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array.length); // outputs "2"
</script>
<script>
var observable = kendo.observable({
people: [
{ name: "John Doe" },
{ name: "Jane Doe" }
]
});
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(observable.people instanceof kendo.data.ObservableArray); // outputs "true"
</script>
The
kendo.data.ObservableArray
wraps its items askendo.data.ObservableObject
instances unless the items are of the primitive type.
<script>
var complex = new kendo.data.ObservableArray([
{ name: "John Doe" },
{ name: "Jane Doe" }
]);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(complex[0] instanceof kendo.data.ObservableObject); // outputs "true"
var primitive = new kendo.data.ObservableArray([
"John Doe", "Jane Doe"
]);
/* The result can be observed in the DevTools(F12) console of the browser. */
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++) {
/* The result can be observed in the DevTools(F12) console of the browser. */
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) {
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log("changed");
});
array.push(3); // triggers the "change" event and the handler outputs "changed"
</script>
The name of the event.
The function which will be invoked when the event is fired.
Empties the array.
<script>
var arr = new kendo.data.ObservableArray([10, 15, 20, 25, 30]);
arr.empty()
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
</script>
Executes a callback function for every single item in the array and returns true
or false
depending if all the items in the array pass the condition. An equivalent of Array.prototype.every
.
The function that will be executed for every item.
Boolean
—Returns true
if all items pass the test function condition. Otherwise, returns false
.
<script>
var arr = new kendo.data.ObservableArray([10, 15, 20, 25, 30]);
var result = arr.every((item) => {return item > 20})
var result2 = arr.every((item) => {return item < 40})
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(result)
console.log(result2)
</script>
Executes a callback function for every single item in the array and returns only those items that pass the filter condition. An equivalent of Array.prototype.filter
.
The function that will be executed for every item.
Array
—A new array with items that pass the filter condition.
<script>
var arr = new kendo.data.ObservableArray([100, 10, 20, 30]);
var result = arr.filter((item) => {return item > 20})
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(result)
</script>
Find the first elements in an array that passes the callback testing function. An equivalent of Array.prototype.find
.
The testing function.
Object
—The first item that passes the search condition.
<script>
var arr = new kendo.data.ObservableArray([10, 15, 20, 25, 30]);
var result = arr.find((item) => {return item > 20})
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(result)
</script>
The method executes the callback function for every single item in the array. An equivalent of Array.prototype.forEach
.
The function that will be executed for every item.
<script>
var array = new kendo.data.ObservableArray([
{ id: 10, name: 'Apple', count: 5},
{ id: 20, name: 'Orange', count: 10},
{ id: 30, name: 'Milk', count: 12},
{ id: 40, name: 'Juice', count: 7},
{ id: 50, name: 'Melon', count: 20}
]);
array.forEach((item) => { item.count = item.count*3})
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array)
</script>
Returns the index in the Array at which the item can be found. An equivalent of Array.prototype.indexOf
.
The searched item.
Number
—The index of the specified data item. Returns -1
if the data item is not found.
<script>
var array = new kendo.data.ObservableArray(["Apple", "Orange", "Berries", "Melon", "Grape", "Pear"])
var result = array.indexOf("Grape");
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(result); // outputs "4"
</script>
Joins all items of an ObservableArray
into a string. An 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]);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array.join("-")); // outputs "1-2-3"
</script>
The method executes the callback function for every single item in the array and returns a new array as a result. An equivalent of Array.prototype.map
.
The function that will be executed for every item.
Array
—A new array with the results from the executed callback.
<script>
var arr = new kendo.data.ObservableArray([
{ id: 10, name: 'Apple', count: 5},
{ id: 20, name: 'Orange', count: 10},
{ id: 30, name: 'Milk', count: 12},
{ id: 40, name: 'Juice', count: 7},
{ id: 50, name: 'Melon', count: 20}
]);
var newArr = arr.map(item => { return item.count*3})
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(newArr)
</script>
Gets the parent of the array if such a parent exists.
kendo.data.ObservableObject
—The parent of the array. Returns undefined
if the array is not nested and does not have a parent.
<script>
var array = new kendo.data.ObservableArray([1, 2]);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array.parent()); // outputs "undefined"
var observable = kendo.observable({ numbers: [1, 2] });
var numbers = observable.get("numbers");
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(numbers.parent() === observable); // outputs "true"
</script>
Removes the last item from an array and returns that item. An equivalent of Array.prototype.pop
.
The
pop
method raises thechange
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();
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array.length); // outputs "0"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(result.get("name")); // outputs "John Doe"
</script>
Appends the given items to the array and returns the new length of the array. An equivalent of Array.prototype.push
. The new items are wrapped as an ObservableObject
if they are complex objects.
The
push
method raises thechange
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 or items that will be appended to the array.
<script>
var array = new kendo.data.ObservableArray([{ name: "John Doe" }]);
var length = array.push({ name: "Jane Doe" });
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(length); // outputs "2"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array[1] instanceof kendo.data.ObservableObject); // outputs "true"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array[1].get("name")); // outputs "Jane Doe"
</script>
<script>
var array = new kendo.data.ObservableArray([ 1 ]);
var length = array.push(2, 3);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(length); // outputs "3"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array[1]); // outputs "2"
</script>
Executes a callback function for every single item in the array and returns the accumulated result. Iterates the items left to right.
The function that will be executed for every item.
Number
—The accumulated result.
<script>
var arr = new kendo.data.ObservableArray([100, 10, 20, 30]);
var result = arr.reduce((totalCount, item) => totalCount - item)
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(result)
</script>
Executes a callback function for every single item in the array and returns the accumulated result. Iterates the items right to left.
The function that will be executed for every item.
Number
—The accumulated result.
<script>
var arr = new kendo.data.ObservableArray([100, 10, 20, 30]);
var result = arr.reduceRight((totalCount, item) => totalCount - item)
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(result)
</script>
Removes the specified item from an array.
The item that will be removed.
<script>
var array = new kendo.data.ObservableArray(["Apple", "Orange", "Berries", "Melon", "Grape", "Pear"])
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array.length);
array.remove("Grape");
console.log(array);
console.log(array.length);
</script>
Returns a single-level deep copy of a portion of an array. An equivalent of Array.prototype.slice
. The result of the slice
method is not an instance of ObvservableArray
—it is a regular JavaScript Array
object.
The
slice
method does not modify the originalObservableArray
.
A zero-based index at which the extraction will start.
A zero-based index at which the extraction will end. 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);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(firstAndSecond); // outputs [1, 2]
</script>
Executes a callback function for every single item in the array and returns true
or false
depending if any of the items in the array passes the condition. An equivalent of Array.prototype.some
.
The function that will be executed for every item.
Boolean
—Returns true
if any of the items passes the test function condition. Otherwise, returns false
.
<script>
var arr = new kendo.data.ObservableArray([10, 15, 20, 25, 30]);
var result = arr.every((item) => {return item > 20})
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(arr)
console.log(result)
</script>
Changes an ObservableArray
by adding new items while removing old items. An equivalent of Array.prototype.splice
.
The
splice
method raises thechange
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 previous example, thechange
event will be triggered two times—the first one, becausebaseball
is removed and, the second one, becausetennis
andhockey
are added.
Array
—Contains the removed items. The result of the splice
method is not an instance of ObvservableArray
.
An index at which the changing of the array will start.
An integer which indicates the number of the items for removal. If set to 0
, no items will be removed. In this case, you have to specify at least one new item.
The items that will be added to the array. If you do not specify any items, splice
removes the items from the array.
<script>
var sports = new kendo.data.ObservableArray(["football", "basketball", "volleyball"]);
var removed = sports.splice(1, 1, "tennis", "hockey");
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(removed); // outputs ["basketball"]
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(sports); // outputs ["football", "tennis", "hockey", "volleyball"]
</script>
Removes the first item from an ObvservableArray
and returns that item. An equivalent of Array.prototype.shift
.
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();
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(removed); // outputs "1"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array.length); // outputs "2"
</script>
Returns a JavaScript Array
object which represents the contents of the ObservableArray
.
<script>
var people = new kendo.data.ObservableArray([
{ name: "John Doe" },
{ name: "Jane Doe" }
]);
var json = people.toJSON();
/* The result can be observed in the DevTools(F12) console of the browser. */
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. An equivalent of Array.prototype.unshift
.
The
unshift
method raises thechange
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 that will be added to the beginning of the ObservableArray
.
<script>
var array = new kendo.data.ObservableArray([2, 3]);
var result = array.unshift(0, 1);
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(result); // outputs "4"
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(array); // outputs [0, 1, 2, 3]
</script>
Fires when the items of the ObservableArray
change.
Specifies the type of change.
The possible values are:
"add"
- Items are added to theObservableArray
."itemchange"
- A field of an item changed."remove"
- Items are removed from theObservableArray
.
The index at which items are removed or added. Set to undefined
if e.action
is "itemchange"
.
The items which were changed.
The field name 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) {
/* The result can be observed in the DevTools(F12) console of the browser. */
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) {
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log(e.action, e.field, e.items[0].get("name"));
});
people[0].set("name", "Jane Doe"); // outputs "itemchange", "name", "Jane Doe"
</script>