Howdy,
First, I had to move away from wix-dataset because when I’d call setSort on my read+write dataset, just changing the sort would save data in my repeater. I have a button in the repeater for saving data, so having sort save data instead of just sorting would be a confusing user experience.
Now, on to my question/issue: I’m using SelectionTags to allow the user to switch sorting of the repeater data. Here is a video of me using them. First issue you may notice is that the initial sort is not by orderNum, which is odd. Next issue is that sometimes clicking on a sort button doesn’t change the sort. Sometimes it takes 1, 2 or 3 (more?) clicks to get the sort to show. You can watch the console log output to see the message printing out even though the repeater data can stay the same.
Here is the relevant code:
import wixData from 'wix-data';
import wixUsers from 'wix-users';
var current_sort = "orderNum";
$w.onReady(async function () {
refresh();
})
async function refresh() {
current_sort = await $w('#sortBy').value[0]; // value is an array, so get just first element of the array for the sort value
await console.log(`Sort: ${current_sort}`);
const results = await wixData.query('purchases')
.isNotEmpty('macAddress')
.eq('prodType', 'lic')
.ascending("${current_sort}")
.find()
// .eq('_owner', wixUsers.currentUser.id)
$w('#listRepeater').data = await results.items;
$w('#listRepeater').onItemReady(($item, data) => {
$item('#orderNum').text = String(data.orderNum);
const actDate = String(data.activationDate);
$item('#actvDate').text = actDate.split(' ').splice(1, 2).join(' ') + ', ' + actDate.split(' ')[3];
$item('#title').text = String(data.description);
$item('#numDevicesLabel').text = String(data.fieldName);
$item('#numDevices').text = String(data.numDevices);
$item('#macAddress').text = data.macAddress;
$item('#userDescription').value = data.userDescription;
if (data.axzezRelease) {
$item('#releaseLabel').show();
$item('#releaseVersion').text = data.axzezRelease;
$item('#releaseVersion').show();
}
})
await console.log("post onItemReady");
}
export async function sortBy_change(event) {
if (!event.target.value || event.target.value.length === 0) {
event.target.value = [current_sort]; // If user picks same tag, keep it highlighted and reverse sort
} else {
event.target.value = await event.target.value.filter(x => x !== current_sort);
}
refresh();
}
For anyone that may have access, here is a link to the dev console.
I appreciate any help you can offer. btw, I’m using async/await because I eventually want to support ascending and descending sort. And btw, I had this working just fine (except for the save when changing sort) with wix-dataset + setSort(). I wish setSort didn’t save and just set.