The informations from https://www.wix.com/corvid/forum/community-discussion/setting-custom-table-values could help you.
I adapt it to your question and also make the code better.
To only update current view it could/should be something like(untested):
import {getStockInfo} from 'backend/serviceModule';
//TODO: dynamicaly update this value(on page load)
var dateToRead = "2020-07-24";
function getValueFromResponse(currencyInfo){
return parseInt(currencyInfo["Time Series (Daily)"][dateToRead]["4. close"], 10).toFixed(2);
}
$w.onReady(function () {
$w("#dynamicDataset").onReady(()=>{
$w("#table1").rows.forEach((row, index) => {
//TODO: change row["ticker"] to real fieldname on table
getStockInfo(row["ticker"]).then(currencyInfo => {
row["todays_change"] = getValueFromResponse(currencyInfo);
});
});
});
});
Or if you want to update the database (saved values also) by button click, something like (again untested):
import {getStockInfo} from 'backend/serviceModule';
$w.onReady(function () {
//make sure button is only pressed if dataset is ready
$w("#button1").disable();
$w("#dynamicDataset").onReady(()=>{
$w("#button1").enable();
});
});
//TODO: dynamicaly update this value(on page load), or get/set it from a input field inside of button1_click_1
var dateToRead = "2020-07-24";
function getValueFromResponse(currencyInfo){
return parseInt(currencyInfo["Time Series (Daily)"][dateToRead]["4. close"], 10).toFixed(2);
}
//TODO: change all occurense of "ticker" with reald DB fieldname (if it's not "ticker")
//async recoursive recursivefunction
function updateEntry(tickerToUpdate){
getStockInfo(tickerToUpdate).then(currencyInfo => {
//verify same item is still active (otherwise value is saved incorrect)
var activeTicker = $w("#dynamicDataset").getCurrentItem()["ticker"];
if (activeTicker == tickerToUpdate){
//update value
$w("#dynamicDataset").setFieldValue("todays_change", getValueFromResponse(currencyInfo));
if ($w("#dynamicDataset").hasNext()){
//next call save internal
$w("#dynamicDataset").next()
.then(() => {
//call function for next entry (recoursion)
updateEntry($w("#dynamicDataset").getCurrentItem()["ticker"]);
})
.catch(() => {
console.log("Error on update to next entry, abort update.");
//reactivate button
$w("#button1").enable();
});
} else {
//save current/last item and reactivate button
$w("#dynamicDataset").save()
.then(() => {$w("#button1").enable();})
.catch(() => {
console.log("Error saving change on last entry");
$w("#button1").enable();
});
}
} else {
//some other logic changed active item while wait on response from getStockInfo...
console.log("Error: active entry changed by other logic, was:" + ticker + " is now: " + activeTicker + ", try to update this one");
updateEntry(activeTicker);
}
}
export function button1_click_1(event, $w) {
//make sure button is not pressed again while update still active
$w("#button1").disable();
//TODO: if you wan update db on page load, the following lines could be moved to inside $w("#dynamicDataset").onReady(... above
//make sure first item is active, so all entries are updated
$w("#dynamicDataset").setCurrentItemIndex(0)
.then(() => {
//call function for first entry, this recursively call for other entries
updateEntry($w("#dynamicDataset").getCurrentItem()["ticker"]);
})
.catch(() => {
console.log("Change to first item failed, abort update");
//reactivate button
$w("#button1").enable();
});
}
was fun writing this (initially search for help for my own problem…)