Recently, I added a hook afterquery to calculate many values depending on others from my database. I was wondering why the new values are not sorting right in the dataset when i try to sort them, for example, High to Low.
How can I fix this ?
Thanks!
Thereās my code in the afterquery data.js file:
export function VestiaireMixte_afterQuery(item, context) { var today = new Date(); var birthdate = new Date(item.dateDeNaissance); var msPerDay = 364 * 24 * 60 * 60 * 1000; var age = (today.getTime() - birthdate.getTime()) / msPerDay; var age = Math.round(age);
item.age = age.valueOf();
var totalmatch = item.victoire + item.defaite;
item.nbMatchJoues = totalmatch.valueOf();
var pourcentvictoire = (item.victoire / item.nbMatchJoues) * 100; var pourcentvictoire = Math.round(pourcentvictoire);
item.pourcentageVictoire = pourcentvictoire.valueOf();
For starters you have only pasted up your data.js file that shows your afterQuery data hook.
So we can assume that you do have a query in your page code for this to work - just checking
Sorry, doing my best to understand but itās my first time coding.
With the code I linked in the data.js, iām able to fill all the empty value in the datasheet exactly like I want (by the way, I use that code in data.js only to calculate different values into the datasheet).
But when I try to sort those ānew calculated itemsā (example here from the datasheet itself), it doesnāt sort them.
Also, if I remove the code into data.js, the calculated values are gone and itās going blank again.
On the main page, I use dataset to sort and show only the highest number returned but it always return a nowhere value.
I tried to figure out what is the problem, but iām kind of lost.
@apocalyptus Itās not possible to sort the collection by afterQuery values. The values are not stored in the collection theyāre just presented there (and only in the sandbox).
@jonatandor35 Well, If I sort value that I type myself into the datasheet, the sorting system works fine. If I use afterQuery to fill the datasheet, the sorting system do not work.
Now, is there any way to calculate, then fill my datasheet without using afterQuery, the way Iāll be able to use the sorting system?
@apocalyptus instead of afterQuery, you can use beforeInsert and beforeUpdate to calculate the value and to update the record (letās say you add a filed key ācalculatedValā.
So in the data.js, you can do:
function calculateValue(item) {
//make the calculation...
return value;//value is the calculated value
}
export function VestiaireMixte_beforeInsert(item, context) {
item.calculatedVal = calculateValue(item);
return item;
}
export function VestiaireMixte_beforeUpadte(item, context) {
item.calculatedVal = calculateValue(item);
return item;
}
@apocalyptus It only triggers when you add a new record or update an existing one.
It wonāt run on records that already there if you wonāt update them.
To solve that, you can create a page for 1-time update, add a button to the page and do something like: