How to add all values to observableArray
in one time? Adding values in loop works very slow in my case. Here is jsfiddle example.
jsfiddle
If you want to detect and respond to changes of a collection of things, use an observableArray . This is useful in many scenarios where you're displaying or editing multiple values and need repeated sections of UI to appear and disappear as items are added and removed.
Description. The KnockoutJS Observable sort() method sorts all items in the array. By default, items are sorted in an ascending order. For sorting an array in a descending order, use reverse() method on sorted array.
You should look at defining the object structure for each element of your array and then add elements of that type to your observable array. Once this is done, you will be able to do something like item. productQuantity(20) and the UI will update itself immediately. EDIT Added the function provided by the OP :).
var myArray = ko.observableArray([]);
var valuesToInsert = [1,2,3];
myArray.push.apply(myArray, valuesToInsert);
that's it
Since you're clearing out the entire observable array, one way you can accomplish this is:
var viewModel = {
name: "base",
addingValue:new ko.observable(),
someArr: new ko.observableArray(["123","432","sdafasd","xrere"]),
add: function()
{
this.someArr.push(this.addingValue());
},
updateSomeArr:function()
{
var temp = [];
for(var i=0;i<5;i++)
{
temp.push("555565");
}
this.someArr(temp);
}
}
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