[Solved] Filter dataset

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.

  1. You made your own searchbar, right?
  2. In your case you search for 2 values (user-search-value + “published”-query), right?

Hello Arthur :raised_hand_with_fingers_splayed:

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!:wink:
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 :blush:

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 :laughing: