Please help with wix-data change sort

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.

  1. You have a lot of extra unnecessary "await"s. It’s recommended to keep your code clean without extra awaits.

  2. These line are wrong:


 event.target.value =[current_sort];

 event.target.value =await event.target.value.filter(x=> x !== current_sort);

It should be:

 $w("#" + event.target.id).value = [current_sort];

 $w("#" + event.target.id).value  = event.target.value.filter(x => x !== current_sort);

(fixed)

@jonatandor35 I’ll give it a try, sir. Thank you. I had the awaits because I was would not yet be returned in time for use, but I think I’m getting them mixed up. The awaits are only needed when a promise is returned, right?

I cleaned up what @jonatandor35 had suggested and it made things “better”, but did not fix the underlying issue. I’m pretty sure this is the bug that was causing the problems:

.ascending("${current_sort}")

should have been:

.ascending(current_sort)

Or I could have even swapped out the double quotes for `` (I verified that too). I let my shell scripting thinking leak into my javascript. Once this change is in place, it’s working beautifully. Still a bit slower than I’d like to re-sort, but I’m still testing in preview. Thank you @J. D. for your help.

Update: once Published, it was much much faster.