Setting filter by URL Path

I have a really good looking page that lists images and stuff from a certain Data Collection. Now I want to copy this page and make the other page get stuff from Data Collection but with other data.

I have a page called Ideas and one page called Projects. At the top I list images that are stored in my dataset. I connect the gallery to my dataset.


Now comes the magic with WIX Code, I now check what is the path of the page? mysite.com/ideas/ and mysite.com/projects/. So how can I just duplicate the page and make stuff on the page get set by data instead or hardcoded?

I get the path, I check if there is stuff in data collection tagged with this path, if it is I filter the dataset just displaying the images / galleri records that matches the path. This way I can use simple pages but insert and make some dynamic stuff as well.

import location from 'wix-location';
import wixData from 'wix-data';

$w.onReady(function () {
	let path = location.path;
	let filter = wixData.filter;
	let categoryName = path[0];
	let searchFieldName = "categoryName";
	let dataSet = "#ideas";
	let dataCollection = "Ideas";
	
	console.log("getting page records within category " + categoryName);
	 wixData.query(dataCollection).eq(searchFieldName, categoryName).find().then((res) => {
	 console.log(res);
	 if (res.length > 0) {
	 	console.log("result found");
	    $w("#ideas").setFilter( filter() 
			.eq(searchFieldName, categoryName) 
		);
		$w(dataSet).refresh();
	}
	else {
		console.log("no records found"); // maybe set some default images?
	}
	});

});
1 Like

thank you for sharing the code, i was trying the same thing for almost an hour, but getting error,
then i search the forum and then solved by your code