So I have a website made with Wix and I’m struggling with some Velo code. This is the test website. I have 2 sections called Papildus Moduļi. The 1st has a repeater which contains 6 containers each with 6 switches. The 2nd section doesn’t have a repeater, but it looks the same. Only the switches in the 2nd section (no repeater) behave the way I want them to.
Issues: When Wix repeats elements, they are given the same exact element name. This is causing issues. I want each group of 6 switches to be independent. In this test version, in the repeater, I can manually select each #switch43 (1st switch of the section), but if I select #switch44 (right below), of any row, every previously selected #switch43 is unselected.
Also, I need each of these switches to have a different value, just as the free switches of the 2nd section.
My goal: I want to know how to make the switches in the 1st section’s repeater have the same behavior as the 2nd section. OR, I want to know if it’s impossible. I don’t know where to go from here.
More info: The repeater is called #repeater4 and each container is called #container4. If I mention the repeater in the code, all functionality/logic of the switches is removed from this section. In the 1st section, the boxes with 0 in them are named #textbox489-#textbox494 going left to right, top to bottom. They are not yet functional, but they will do the same as the boxes in the next section.
There is a bit more separate code left out which impacts the color of the rows in a different section. This is responsible for the console messages.
Thank you for helping me learn 🙂
$w.onReady(() => {
// Attach event listeners to the dropdown
$w("#employeeDD").onChange(updateTotals);
$w("#employeeDD").onChange(updateSwitchTotal);
// Define sections for the 2nd section switches
const oldSections = [
{ col1: ["switch1", "switch4"], col2: ["switch2", "switch5"], col3: ["switch3", "switch6"] },
{ col1: ["switch7", "switch10"], col2: ["switch8", "switch11"], col3: ["switch9", "switch12"] },
{ col1: ["switch13", "switch16"], col2: ["switch14", "switch17"], col3: ["switch15", "switch18"] },
{ col1: ["switch19", "switch22"], col2: ["switch20", "switch23"], col3: ["switch21", "switch24"] },
{ col1: ["switch25", "switch28"], col2: ["switch26", "switch29"], col3: ["switch27", "switch30"] },
{ col1: ["switch31", "switch34"], col2: ["switch32", "switch35"], col3: ["switch33", "switch36"] }
];
// Attach event listeners to 2nd section switches
oldSections.forEach(section => {
Object.values(section).flat().forEach(switchId => {
$w(`#${switchId}`).onChange(() => handleSwitchSelection(switchId, section));
});
});
// Attach event listeners to the additional switches
["switch37", "switch38", "switch39", "switch40", "switch41", "switch42"].forEach(switchId => {
$w(`#${switchId}`).onChange(updateSwitchTotal);
});
// Attach event listeners to 1st section switches using data attributes
const newSwitchGroups = [
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] }
];
newSwitchGroups.forEach((group, groupIndex) => {
Object.values(group).flat().forEach(switchId => {
$w(`#${switchId}`).onChange(() => handleNewSwitchSelection(switchId, group, groupIndex));
});
});
});
// Lookup table for total values
const totalsMap = {
3: { a: 270, b: 314, c: 373 },
7: { a: 245, b: 282, c: 329 },
20: { a: 233, b: 268, c: 313 }
};
// Function to update totals based on dropdown
const updateTotals = () => {
const selectedValue = Number($w("#employeeDD").value);
const { a = 0, b = 0, c = 0 } = totalsMap[selectedValue] || {};
$w("#aTotal").text = formatCurrency(a);
$w("#bTotal").text = formatCurrency(b);
$w("#cTotal").text = formatCurrency(c);
};
// Handles switch selection logic for 2nd section switches
const handleSwitchSelection = (selectedSwitchId, section) => {
const selectedColumn = Object.values(section).find(col => col.includes(selectedSwitchId));
selectedColumn?.forEach(switchId => {
if (switchId !== selectedSwitchId) {
$w(`#${switchId}`).checked = false;
}
});
updateSwitchTotal();
};
// Handles switch selection logic for 1st section switches
const handleNewSwitchSelection = (selectedSwitchId, group, groupIndex) => {
const selectedColumn = Object.values(group).find(col => col.includes(selectedSwitchId));
selectedColumn?.forEach(switchId => {
if (switchId !== selectedSwitchId) {
$w(`#${switchId}`).checked = false;
}
});
updateSwitchTotal();
};
// Function to update switch totals & final quotes
const updateSwitchTotal = () => {
let totalA = 0, totalB = 0, totalC = 0;
const selectedValue = Number($w("#employeeDD").value);
const rowPrices = {
3: [15, 20, 126, 155, 60, 86, 39, 78, 65, 87, 153, 182, 15, 10],
7: [13, 17, 110, 135, 52, 75, 36, 61, 56, 76, 134, 159, 13, 8],
20: [11, 16, 103, 127, 49, 71, 34, 57, 53, 72, 126, 149, 12, 7]
};
const priceList = rowPrices[selectedValue] || Array(14).fill(0);
// Column definitions for 2nd section switches
const oldColumns = {
col1: ["switch1", "switch4", "switch7", "switch10", "switch13", "switch16", "switch19", "switch22", "switch25", "switch28", "switch31", "switch34", "switch37", "switch40"],
col2: ["switch2", "switch5", "switch8", "switch11", "switch14", "switch17", "switch20", "switch23", "switch26", "switch29", "switch32", "switch35", "switch38", "switch41"],
col3: ["switch3", "switch6", "switch9", "switch12", "switch15", "switch18", "switch21", "switch24", "switch27", "switch30", "switch33", "switch36", "switch39", "switch42"]
};
// Calculate totals for 2nd section switches
Object.entries(oldColumns).forEach(([col, switches], colIndex) => {
switches.forEach((switchId, index) => {
if ($w(`#${switchId}`).checked) {
if (colIndex === 0) totalA += priceList[index];
if (colIndex === 1) totalB += priceList[index];
if (colIndex === 2) totalC += priceList[index];
}
});
});
// Calculate totals for 1st section switches
const newSwitchGroups = [
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] },
{ col1: ["switch43", "switch44"], col2: ["switch45", "switch46"], col3: ["switch47", "switch48"] }
];
newSwitchGroups.forEach((group, groupIndex) => {
Object.entries(group).forEach(([col, switches], colIndex) => {
switches.forEach((switchId, switchIndex) => {
if ($w(`#${switchId}`).checked) {
if (colIndex === 0) totalA += priceList[switchIndex];
if (colIndex === 1) totalB += priceList[switchIndex];
if (colIndex === 2) totalC += priceList[switchIndex];
}
});
});
});
// Update switch totals
$w("#switchTotal").text = formatCurrency(totalA);
$w("#switchTotalb").text = formatCurrency(totalB);
$w("#switchTotalc").text = formatCurrency(totalC);
// Calculate final quotes
const finalTotalA = parseCurrency($w("#aTotal").text) + totalA;
const finalTotalB = parseCurrency($w("#bTotal").text) + totalB;
const finalTotalC = parseCurrency($w("#cTotal").text) + totalC;
$w("#totalQuoteA").text = formatCurrency(finalTotalA);
$w("#totalQuoteB").text = formatCurrency(finalTotalB);
$w("#totalQuoteC").text = formatCurrency(finalTotalC);
};
// Format currency using modern API
const formatCurrency = (value) => new Intl.NumberFormat('lv-LV', { style: 'currency', currency: 'EUR', minimumFractionDigits: 0, maximumFractionDigits: 0 }).format(value);
// Parse currency text back into number
const parseCurrency = (value) => Number(value.replace(/[^d.-]/g, '')) || 0;