Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Last active July 5, 2020 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanwixcode/39d6eda05159936a941d8dd26ec8b6e7 to your computer and use it in GitHub Desktop.
Save shanwixcode/39d6eda05159936a941d8dd26ec8b6e7 to your computer and use it in GitHub Desktop.
import wixData from 'wix-data';
$w.onReady(function () {
});
export function repeater1_itemReady($item, itemData, index) {
$item("#price").text = itemData.price + ' ' + itemData.currency;
}
export async function name_keyPress(event) {
defaultSort = await false;
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w("#name").value, collectionName);
}, 200);
}
export async function collection_change(event) {
defaultSort = await false;
filter(filterName, $w("#collection").value);
}
export async function sortBy_change(event) {
defaultSort = await false;
if($w("#sortBy").value === 'A-Z') {
$w("#dataset1").setSort(wixData.sort()
.ascending("name")
);
} else if($w("#sortBy").value === 'Z-A') {
$w("#dataset1").setSort(wixData.sort()
.descending("name")
);
} else if($w("#sortBy").value === 'New to Old') {
$w("#dataset1").setSort(wixData.sort()
.ascending("_updatedDate")
);
} else if($w("#sortBy").value === 'Old to New') {
$w("#dataset1").setSort(wixData.sort()
.descending("_updatedDate")
);
} else if($w("#sortBy").value === 'Price High to Low') {
$w("#dataset1").setSort(wixData.sort()
.descending("price")
);
} else if($w("#sortBy").value === 'Price Low to High') {
$w("#dataset1").setSort(wixData.sort()
.ascending("price")
);
}
}
let defaultSort = false;
export async function reset_click(event) {
$w("#name").value = await undefined;
$w("#collection").value = await undefined;
$w("#sortBy").value = await undefined;
defaultSort = await true;
filter();
}
export function dataset1_ready() {
let count = $w("#dataset1").getTotalCount();
$w("#count").text = 'Total Products: (' + count + ')';
}
let filterName;
let collectionName;
let debounceTimer;
async function filter(name, collection) {
if(filterName !== name || collectionName !== collection) {
let newFilter = wixData.filter();
if(name)
newFilter = newFilter.contains('name', name);
if(collection)
newFilter = newFilter.hasSome('collections', [collection]);
await $w("#dataset1").setFilter(newFilter);
if(defaultSort === true) {
$w("#dataset1").setSort(wixData.sort()
.ascending("name")
);
}
let count = $w("#dataset1").getTotalCount();
$w("#count").text = 'Total Products: (' + count + ')';
filterName = name;
collectionName = collection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment