In my html view I bind a html table with knockout viewmodel, that contains 3 observableArrays, as following:
function myViewModel() {
var self = this;
self.RowIds = ko.observableArray();
self.ColIds = ko.observableArray();
self.allCellsList = ko.observableArray();
self.calculateText = function (r_id, c_id) {
console.log('calculateText');
var item = ko.utils.arrayFirst(self.allCellsList(), function (i) {
return i.rowId() == r_id && i.colId() == c_id;
});
return item.text();
}
self.loadData = function (rowIds, colIds, allCellsList) {
//remove old data (not necessary, only for testing , to see how long it takes)
self.allCellsList.removeAll();
//populate new data:
self.RowIds(ko.mapping.fromJS(rowIds));
self.ColIds(ko.mapping.fromJS(colIds));
self.allCellsList(ko.mapping.fromJS(allCellsList));
}
}
RowIds contains list of ids for each row in the table, CoIds contains list of ids for each column in table. allCellsList is the main table that holds list of objects, where each of them contain two identifiers: rowId and colId. The function calculateText is called in each cell in table , and calculates the matching data in allCellsList by these 2 fields. As following:
<table>
<tbody data-bind="foreach:RowIds">
<tr data-bind="foreach:$root.ColIds()">
<td>
<div data-bind="text:$root.calculateText($data , $parent)">
</div>
</td>
</tr>
</tbody>
</table>
When I initialize the allCellsList with new data in loadData function, by removing the old data and fetching the new data (or by directly fetching the new data, without using the removeAll function) – the calculateText function is called, For each item in the old list , and for each item in the new list. If the old list contains a huge amount of records – the clear array operation takes too long. (sometimes 3-4 seconds)
My question is how can I populate the allCellsList in that case or in other , so that the reloading operation will be faster? Is there a way to avoid calling the function calculateText , when allCellsList is being cleared?
Thank you.