Dynamic Base Filter by Property

What I’m trying to do

I want to embed a database in a note and configure the filters via YAML properties. Basically, I want to be able to specify the captured resources and areas using Meta-Bind, for example.

However, I’m now getting an error.

Things I have tried

This is my current Base what already filters properly:

views:
  - type: table
    name: Table
    filters:
      and:
        - Resource.containsAny(this["Resource"])
        - file.name.containsAny(this["search-term"])

This is my current Page:

---
search-term: Vault
Resource:
  - "[[Obsidian Knowledge]]"
  - "[[Tool Notes]]"
---

![[Bases Search with meta-bind.base]]

Errors I get

But I get an error message every time I change one of the properties:

Failed to evaluate a filter: Type error in “containsAny”, parameter “values”. Expected String, given List.

What I also like to do

I also would be glad for recommendations for a solution for an IF Statement like

If search-term isEmpty() THEN nothing ELSE filter for search-term

Happy for every tip :innocent:

2 Likes

That error sounds like some of your Resource values are formatted as text instead of list.

Try this filter instead:

list(Resource).containsAny(this.Resource)

If that stops the error, then you can keep it (quick method) or clean up the format in your notes (probably a better idea to forestall other issues).

Separately, note that if you want search-term to be a text property like in your example, then

file.name.contains(this["search-term"])

… is sufficient. You don’t need containsAny there. That didn’t cause the error, Resources probably caused it, but just FYI.

But if you want search-term to be be a list, then you can use:

this["search-term"].filter(file.name.contains(value))

Grouping your filters with checks for empty values would take care of the if statement you mentioned:

```base
views:
  - type: table
    name: Table
    filters:
      and:
        - file.path != this.file.path
        - or:
            - this["search-term"].isEmpty()
            - file.name.contains(this["search-term"])
        - or:
            - this.Resource.isEmpty()
            - list(Resource).containsAny(this.Resource)
    order:
      - file.name
      - Resource

```

1 Like

Thanks so much for sharing the community solution! That completely solved my problem.

The list(Resource).containsAny(this.Resource) approach was exactly what I needed to handle the type inconsistency, and the elegant OR logic with isEmpty() checks perfectly addresses my conditional filtering requirement. Much more robust than what I was trying initially.

Really appreciate you taking the time to share the working solution!

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.