Filter dataset according to Dynamic page title

Hi!
Thank you very much for the wix code. I’m learning it slowly but can still understand and create things I couldn’t create before. It feels great!

I built few collections in my site, all of them has a field which contains tags (in text). then I built a tag collection.
Now I’m trying to create a dynamic page based on the tag collection and in this dynamic page there would be tables connected to the other collections, filtered by the dynamic page title (the tag name).

I guess I can use the code to filter it but I can’t understand how.
Will appreciate you help.
Thanks,
Sharon

1 Like

hey
look at wix.location api, you can get the url there, then you look at setfilter on the dataset api and voila

or read the value from the tags and use them in the setFilter. remember not to use braces {} around wixData in the import statement, if you do the filter wont work.

Hi Sharon,

Use the next code sample as a starting point.

import wixData from "wix-data";

$w.onReady(function () {
	$w("#dynamicDataset").onReady(()=>{
		const tagName = $w("#dynamicDataset").getCurrentItem().title;		
		$w("#dataset1").setFilter(wixData.filter().eq("tag",tagName));
	});
});

The code does the following:

  1. Waits for the main dataset of the page to be ready.

  2. Get the tag name from the current item. The code assumes the tag name is in the title field.

  3. Use the tag name to set the filter of another dataset, with the ID dataset1.
    You can create as many additional datasets as you need, filter their result by the tag name and connect them to table elements the regular way.

Read more about dataset APIs at the APIs reference site

Shay

the use of import {wixData} from “wix-data”; does make an error on filter function.

use import wixData from “wix-data”;

and the error will go away.

Shay thank you very much for you help!
I had to change eq to contains and then it works!

Andreas, there wasn’t any error, but I appreciate your willing to help me.

Sharon

I am testing out an incredibly basic script and am continually getting the error: TypeError: Cannot read property ‘map’ of undefined. What is so obvious that I am missing here?

Permissions are open to everyone, the field names are correct, the data exists. So confused!

import wixData from "wix-data";

$w.onReady(function () {
		$w("#dataset1").setFilter(wixData.filter().contains("firstName","David"));
});

Hey
You must define filter as below and use filter not wixData.filter, the docs are wrong.

import wixData from 'wix-data'; 
let filter = wixData.filter;

As…

$w.onReady(function () { 
$w("#dataset1").setFilter(filter().contains("firstName","David")); 
});

Thanks for the great comments and code examples on this thread folks, though I would appreciate a little help as I experiment with some of the examples provided. I am pretty new to the coding side of things so excuse my efforts!

I have a Settlements dynamic page that displays a table of jobs from a different data collection.
I’m simply trying to filter a list of jobs by the name of the settlement.

The jobs dataset has a field called Location that is a text string that is manually populated with settlement names.
So I want to filter the jobs displayed in the table by filtering the content by Location = settlement_name

The Dynamic Dataset is named: Demo_settlements Item
The field that the Settlement name is contained in is called: settlement_name
The Dataset I want to filter is named: Demo_jobs_contracts dataset

Here’s the code I have used:

import wixData from “wix-data”;
let filter = wixData.filter;

$w.onReady(function () { 
	//TODO: write your page related code here... 

	$w("#dynamicDataset").onReady(() => { 
		const tagName = $w("#dynamicDataset").getCurrentItem().settlement_name; 
		$w("#Demo_jobs_contracts dataset").setFilter(filter().eq("Location", tagName)); 
	}); 

	//....... last brackets here, my code above 

}); 

But I’m unable to get it to work and receive the following errors:

On the Page Code panel I get a warning message on the first line:
ESLint failed to validate this file because an error occured: unknown character at position 6

However the first line of my Page Code is:
import wixData from “wix-data”;

When I save and preview the code I get the following output:

Loading the code for the Demo_settlements (Settlement_name) page. To debug this code, open zqg9j.js in Developer Tools.
Wix code SDK Warning: The text parameter that is passed to the text method cannot be set to null or undefined.
TypeError: $w(…).setFilter is not a function

Could anyone help steer me with where or what I am doing wrong? Appreciate any help!

Andreas, the snippet you provided is equivalent to doing “wixData.filter().eq(…)”, unless I’m missing something. As far as I know there’s no issue with the docs. If indeed there is, please post a link to the issue and we’ll fix it right away :slight_smile:

David, the error you were getting was caused due to the fact that you called setFilter before the dataset was ready. This was a bug on our side and we fixed it. Please try it again and let me know if it works.

Anyone able to help or share some advice with my detailed issue above? Thanks