Hello friends.
Im stuck with this (days), i need to get the average of 3 items (dropdowns) in the same container, my code does not work, can you help me review it?
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id.id; // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true
let userRole = user.role; // "Member"
user.getEmail()
.then((email) => {
let userEmail = email; // "user@something.com""user@something.com"
console.log(userEmail);
console.log(userId);
$w("#dataset1").setFilter(wixData.filter()
.eq("mail", userEmail)
);
});
});
$w("#repeater1").onItemReady( ($item, itemData, index) => {
let sabe = $item("#dropdown1");
let quiere = $item("#dropdown2");
let puede = $item("#dropdown2");
let avgTotal = sabe + quiere + puede / 3;
$item("#input2").value = sumTotal
});
I would greatly appreciate your help
Thanks!
J.D
March 15, 2021, 1:17am
2
First of all delete this part:
$w("#repeater1").onItemReady( ($item, itemData, index) => {
let sabe = $item("#dropdown1");
let quiere = $item("#dropdown2");
let puede = $item("#dropdown2");
let avgTotal = sabe + quiere + puede / 3;
$item("#input2").value = sumTotal
});
Instead write:
$w("#dropdown1, #dropdown2, #dropdown3").onChange(event => {
let $i = $w.at(event.context);
let values = [($i("#dropdown1"), $i("#dropdown2"), $i("#dropdown3")]
.map(e => e.value).filter(e => e);
$i("#input2").value = values.reduce((a,c) => Number(a) + Number(c) ,0);
})
[TYPO FIXED]
Hello J.D.
Thanks for reply! I fix one detail in your code
let values = [($i("#dropdown1"), $i("#dropdown2"), $i("#dropdown3")]
By this “)” at the end
let values = [($i("#dropdown1"), $i("#dropdown2"), $i("#dropdown3"))]
Now i can have numbers by selecting the “dropdown3”, but it still does not perform the calculation. I’m studying what happen, if you have some idea i appreciate. Thanks again hero! jajajaja
J.D
March 15, 2021, 1:57am
4
@claudiojimenezc Your fix is wrong.
Ah! this is happening, now i delete “(” at the start and now its adding the values
J.D
March 15, 2021, 2:08am
6
Post your code + make sure the propertyIDs of the dropdowns are spelled correctly.
Working! Thanks you very much! i learn a lot today!