Hey all! I’ve been stuck trying to figure out how to loop through a dataset. My “Payroll” dataset has one field (column) containing the “worked hours”( hoursfield ) of my staff, and another field (column) containing a “value” for each of those “worked hours” ( valuefield ). What I’m trying to accomplish is to multiply the number of “worked hours” and the “value” fields for each of the items in my dataset. To write my code, I took as reference this other post, however, I haven’t had any luck so far. Please help!
let hoursTemp= 0 ;
let valueTemp= 0 ;
let toBePaidTemp= 0 ;
//first, let’s filter the dataset with the employee name and payroll period
//loop through each result to multiply hours and value for ( let i= 0 ; i < count ; i++)
{
hoursTemp= results.items[i].hours;
valueTemp= results.items[i].value;
toBePaidTemp= toBePaidTemp + (hoursTemp*valueTemp)
}
$w( ‘#paycheckAmount’ ).value = toBePaidTemp;
Try to work with DATA-COLLECTION instead of DATASET…
import wixData from 'wix-data';
var toBePaidTemp
$w.onReady(function () { myFunction() });
function myFunction () {
wixData.query("myCollection") //<------- enter here the database-name
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0];
for (var i = 0; i < results.items.length; i++) {
let hoursTemp= results.items[i].hours;
let valueTemp= results.items[i].value;
toBePaidTemp= toBePaidTemp + (hoursTemp*valueTemp)
}
$w('#paycheckAmount').value = toBePaidTemp;
} else {
// handle case where no matching items found
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}