Hello, I have a page with a “dataset1” (it has a sort of Category A-Z), the repeater is called"repeater2" and database called “Categories”( it is sorted by category alphabetically). The field I am trying to display has a key name of “category”. I have tried many approaches and have read all of the articles I could find on the subject.
The code is as follows: import { local } from ‘wix-storage’; import wixData from ‘wix-data’; import wixLocation from ‘wix-location’;
$w.onReady(() => {
});
export function dataset1_ready_1() {
wixData.query(“Categories”)
.limit(300)
.find()
.then(results => {
$w(“#repeater2”).data = Array. from (results.items.reduceRight((m, t) => m.set(t.category, t), new Map()).values());
})
}
The result are as follows: You will notice it is sorted until it reaches the 25th display on ???
It;s hard to understand what you’re trying to do. You have a dataset but you don’t use it, and instead you pull the data directly from the collection and then you process it in a complicated way that is not clear to outsiders.
Anyway, if you want the data to be sorted alphabetically, then add to your query:
J.D. Thank you for the quick response. I will try your suggestion.
Let me explain further. I am eliminating duplication of Categories with the code . I have Categories with many subcategories in the database.
For example: a Category is Bathroom has the Subcategories of Bathroom design, Bathroom installation, Electrical, Plumbing, Cabinets, Tiling, Sinks, Toilets and Bathtubs. One Category with 9 Sub Categories. I then want to display the repeater Categories alphabetically.
//code... query
.then(res => {
let items = res.items;
let categories = items.filter((o, index) => items.findIndex(e => e.category === o.category) === index);//"category" here is the field key of the parent category in your collection
$w("#repeater2").data = categories;
})
OK, I don’t see any problem wit the code you posted (except for the lack of closing } at the end of the function which you probably just didn’t copied to here).
Anyway as long as your first code works, it’s alright.