Hellođ. I was wondering if it would be possible to filter a dataset with code. I know I can do this without code, but I made a search bar to search the dataset and I canât filter the dataset without code. Here is my code.
import wixData from 'wix-data';
export function button36_click(event, $w) {
let searchValue = $w("#input1").value;
$w("#dataset1").setFilter(wixData.filter().contains('name', searchValue)
.or(wixData.filter().contains('title', searchValue)))
}
I have a section in my database that has the label âPublishedâ. I really only want the items with the âPublishedâ label showing up in my dataset when someone searches with my search bar.
Could someone please give me a link to an article?
Regards
Arthur
Hello Arthur,
what is exactly your issue?
Your code is not working?
You say you are looking for âPublishedâ <â this is the column name-ID or label? Make sure that you use the real name-ID of the column which you want to be filtered and not the label of the choosen column in your data-collection.
Here you will see a simple example of how to do a filter-function using datasetâŚ
https://russian-dima.wixsite.com/meinewebsite
No, I made a search bar to filter my dataset. I wanted to only show items in my dataset with the label âPublishedâ in the database when someone searches an item.
Ok, just for correct understanding.
- You made your own searchbar, right?
- In your case you search for 2 values (user-search-value + âpublishedâ-query), right?
Hello Arthur 
You just need to set the filter to only show the published ones in both cases:
let filter = wixData.filter()
.contains('name', searchValue)
.eq('Published', 'Published')
.or(
wixData.filter()
.contains('title', searchValue)
.eq('Published', 'Published')
)
$w("#dataset1").setFilter(filter);
Hope that helped~!
Ahmad
Thank you so much!
It actually worked! You are amazing. You really are! Could I just ask you 1 more question? Is it possible to let people search an image on the homepage and show the results on the page I just created? Also, could it be possible to show the search value someone searched on the homepage search bar on the results page search bar I just created?
You are truly amazing! Thank you so much! I canât say thank you enuff! Thank you!
Arthur 
Oh! I just found out that the filter for only showing the images with the âPublishedâ works when the search bar isnât empty. Is it possible to use the filter even when the search bar is empty?
~Thanks~
Arthur
Thanks for the compliment.
Yes itâs possible by storing the value of the search using the session API, and then get it on this page and apply the search or the filter automatically.
Regarding your next reply, I really donât know and I canât tell whatâs going on without examining the code and the actual page.
Hello!
Thanks for helping me out!
Here is my code for the page where I have all my images.
import wixData from 'wix-data';
$w.onReady( function() {
})
export function button36_click(event, $w) {
let searchValue = $w("#input1").value;
let filter = wixData.filter()
$w("#dataset1").setFilter(wixData.filter().contains('name', searchValue)
.eq('stat', 'Published')
.or(wixData.filter().contains('title', searchValue)))
.eq('stat', 'Published')
$w("#dataset1").setFilter(filter);
}
(âstatâ is the property for the âPublishedâ lapel in the database)
The link to my website: https://arthurvalatin.wixsite.com/free-images
Arthur
Youâre mixing my code above with russian-dimaâs code, youâre filtering the dataset correctly, then youâre clearing the dataset.
Youâre setting a variable called filter in line 9 .
Then youâre filtering the dataset correctly according my code.
BUT, youâre clearing the filters that youâve already applied (lines: 10-13) with the filter() command in line 14 , causing the filter to be reset.
Please pay attention to the code you copy, and make sure that it doesnât conflict with other code you already have.
For further questions, please open a new thread (post).
Hello. Yes, I will ask a question on a new post. I just wanted to tell you that I did use your code(not dimiaâs code) and you did include that 14th line in your comment marked as best answer at the top.
~Thank you~
Arthur
The variable filter that Iâve defined, equals to a bunch of filters, while your code defines a blank filter (a reset filter), and then applying my filters, which is okay, but the problem is with the next line where you pass the blank filter to the filter() method.
I hope that you understood whatâs going on.
Do you mean I need a .eq( âstatâ , âPublishedâ ) inside $w( â#dataset1â ).setFilter(filter); ?
(I got an error message: Unexpected token. on the on the 15th line of code: .eq( âstatâ , âPublishedâ ).
import wixData from 'wix-data';
$w.onReady( function() {
})
export function button36_click(event, $w) {
let searchValue = $w("#input1").value;
let filter = wixData.filter()
$w("#dataset1").setFilter(wixData.filter().contains('name', searchValue)
.eq('stat', 'Published')
.or(wixData.filter().contains('title', searchValue)))
.eq('stat', 'Published')
$w("#dataset1").setFilter(filter);
.eq('stat', 'Published')
}
I tried it but I got the error.
Which bit do I need to change?
~Thanks~
Arthurđ
Replace the âbutton36â onClick() event handler body with this, but before you do so, compare it with your code.
export function button36_click(event) {
let filter = wixData.filter()
.contains('name', searchValue)
.eq('Published', 'Published')
.or(
wixData.filter()
.contains('title', searchValue)
.eq('Published', 'Published')
)
$w("#dataset1").setFilter(filter);
}
Hello Ahmadđ!
Thank you so much! It worked! You are amazing!
~Thank you!~
Arthur 