Hey,
I have a repeater and a list that I use as a menu to select category of product.
As for each category there are several products I would like to remove dublicated category.
Obviosly it is not an issue to create both in a repeater with buttons and a list to show all the categories.
I would like nopw to try and remove the dublicates.
I used this code for a dropdown and it worked but I cant make it work for the repeater and list
//Krenelerat galler dropdown
$w.onReady( function () {
wixData.query(“SPGIndustri”)
// Get the max possible results from the query
.eq(“huvudgrupp”, “Krenelerat galler”)
.ascending(“Kvalitet”)
.find()
.then(results => {
// Call the function that creates a list of unique “kvalitet”
const uniquekvalitet = getUniquekvalitet(results.items);
// Call the function that builds the options list from the unique “kvalitet”
$w(“#KreneleratGallerDD”).options = buildOptions(uniquekvalitet);
});
// Builds an array from the “kvalitet” field only from each item in
// the collection and then removes the duplicates
function getUniquekvalitet(items) {
// We use the map method to create the kvalitetOnly object containing all the kvalitet` from the query results
const kvalitetOnly = items.map(item => item.kvalitet);
// Return an array with a list of unique kvalitet
return [… new Set(kvalitetOnly)];
}
// Creates an array of objects in the form {label: “label”, value: “value”} from the array of kvalitet
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
// We use the map method to build the options list in the format {label:uniquekvalitet, value:uniquekvalitet}
return {label:curr, value:curr};
});
}
});
Thanks,
Benny
You can use the techniques described in the forum post Remove duplicates from connected dropdown options .
The tecniques don’t translate directly from dropdowns to repeaters. The .options property is for dropdowns - for repeaters you need .data . Plus, you will need to implement the onItemReady() function. Refer to the Repeater API for full details.
Good luck,
Yisrael
Hey,
Sorry, no go.
Here is what I did…
$w.onReady( function () {
$w.onItemReady( ($w, itemData, index) => {
$w(‘#repeater1’).onItemReady( ($w, itemData, index) => {
$w(“button202”).text = itemData.kvalitet;
} );
wixData.query(“SPGIndustri”)
// Get the max possible results from the query
.eq(“huvudgrupp”, “Krenelerat galler”)
.ascending(“Kvalitet”)
.find()
.then(results => {
// Call the function that creates a list of unique “kvalitet”
const uniquekvalitet = getUniquekvalitet(results.items);
// Call the function that builds the options list from the unique “kvalitet”
$w(‘#repeater1’).data = buildData(uniquekvalitet);
});
// Builds an array from the “kvalitet” field only from each item in the collection and then removes the duplicates
function getUniquekvalitet(items) {
// We use the map method to create the kvalitetOnly object containing all the kvalitet` from the query results
const kvalitetOnly = items.map(item => item.kvalitet);
// Return an array with a list of unique kvalitet
return [… new Set(kvalitetOnly)];
}
// Creates an array of objects in the form {label: “label”, value: “value”} from the array of kvalitet
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
// We use the map method to build the options list in the format {label:uniquekvalitet, value:uniquekvalitet}
return {label:curr, value:curr};
});
}
});
Your logic is incorrect. The query doesn’t go inside the onItemReady() function. Rather, you perform the query and assign the results to the Repeater’s data property. Then, you need to have an onItemReady() function that the Repeater calls after it gets the data .
Refer to the Repeater API for more information.
Good luck,
Yisrael
Hey,
Your statment “your logic is incorrect” is theg story of my life
Back to business:
I rewrite the code for getting the info for the table
Part one works great
Part two does not remove the dublicates - I did try to add ontemReady but no go
//part one
$w.onReady( function () {
console.log(“The dataset is ready”);
$w(‘#KreneleratMenu’).columns = [
{
“id”: “col1”,
“dataPath”: “kvalitet”, // matches field key from collection
“label”: “kvalitet”,
“width”: 150,
“visible”: true ,
“type”: “text”,
}
];
wixData.query(‘SPGIndustri’)
.find()
.then(result => {
let tableRows = result.items;
$w(‘#KreneleratMenu’).rows = tableRows;
});
})
//end part one
//part two
// remove duplicates
wixData.query(‘SPGIndustri’)
.eq(“huvudgrupp”, “Krenelerat galler”)
.ascending(“Kvalitet”)
.find()
.then(result => {
const uniqueKvalitet = getuniqueKvalitet(result.items);
console.log(uniqueKvalitet);
$w(‘#KreneleratMenu’).rows = buildrows(uniqueKvalitet)
});
function getuniqueKvalitet (items) {
const kvalitetOnly = items.map(item => items.kvalitet);
return [… new Set(kvalitetOnly)]
}
function buildrows (uniqueData) {
return uniqueData.map(curr => {
return {‘kavalitet’ : curr}
})
}
The program logic is still not correct:
-
You are not populating a repeater - rather you are populating a table. So, you don’t need an onItemReady() function.
-
The page onReady() function only runs when the page is ready, not when the database it ready. It may work now, but it can’t be guaranteed. You should also use a dataset.onReady() function to the page’s onReady() function to ensure that the dataset is ready before you try to use it.
-
The removal of duplicates isn’t done in two parts. It works by doing a query, and then removing the duplicates from the results of the query as detailed in the forum post Remove duplicates from connected dropdown options .
We are unable to provide full code solutions. You might want to consider checking out the WixArena - it’s a hub where you can look for Wix Code (and other) experts for hire.
Good luck,
Yisrael
Hi All,
I have successfully removed the duplicates from my dropdown list!
For those that need help, you can refer to this example .
Good luck!
Ben