Hi,
Ive been trying this before posting for a long time now and i can’t get my code to work.
I have a collection wich include all sort of information about abandoned buildings (name, year of construction, pictures, age of the building, number of years abandonned, status etc)
One of the page on my site is an index, with a repeater connected to the ‘locations’ collection where all the infos about the buldings are, in a list format.
SO, what i want to acheive is the that the age of the buildings (and year of abandon too) will be calculated automatically, from the construction date wich is just the year, like 1934.
( but if i need it to be a in a date yyyy/mm/dd format i can change it) but would rather just use the year.
I have tried folowing this :
Ive so created a after querry hook from my collection and everything like stated in the how to but no luck… my reapeater end up being empty when i do the preview…
I tried this other option wich worked, but its not meant for a repeater so the same year just repeat itselft:
Here is my code in both of the .js file and the code on my index page.
data.js :
import {ageFromcons} from ‘backend/calculations’;
export function Locations_afterQuery(item, context) {
item.age = ageFromcons(item.cons);
return item;
}
calculation.js:
export function ageFromcons(cons) {
let now = new Date();
let age = now.getFullYear() - cons.getFullYear();
let months = now.getMonth() - cons.getMonth();
let days = now.getDate() - cons.getDate();
if (months < 0 || (months === 0 && days < 0)) {
age–;
}
return age.toString();
}
Code from my index page:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
$w.onReady(function () {
//TODO: write your page related code here…
});
$w.onReady(function () {
$w(“#repeater1”).onItemReady( ($w, itemData, index) => {
$w(“#text6”).text = itemData.age;
} );
} );
export function line3_viewportEnter(event, $w) {
$w(“#StickyMenu”).hide();
}
export function line3_viewportLeave(event, $w) {
$w(“#StickyMenu”).show();
}
import wixData from ‘wix-data’;
// …
export function button7_click(event, $w) {
$w(“#IndexDataset”).setSort(
wixData.sort()
.ascending(“construction”)
);
}
export function button26_click(event, $w) {
$w(“#IndexDataset”).setSort(
wixData.sort()
.ascending(“nom”)
);
}
I would probably need another code for sorting the age column too, because i dont know how i can sort it since it will not be part of the database schema.
My code for sorting my column are example that, (button26 being example ‘construction’ button)
export function button26_click(event, $w) {
$w(“#IndexDataset”).setSort(
wixData.sort()
.ascending(“nom”)
);
}
Sorry for the long tread! haha
Thanks to whoever can help me
Guillaume