Generating dynamic filter string

I responded to a filter question on reddit, which I knew was not ideal.

Creating a field search : r/TiddlyWiki5 (reddit.com)

Please review. Can you provide a better solution (I know you can) that follows the label/text “Tiddlers with these topics:”?

Thanks,

Craig

Hi Craig,
I saw the solution and it seems fine.

Look the below code taken from your solution


<$select field=selected_field multiple 4>
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
<option>Six</option>
</$select>

<$list filter="[all[current]search:content_field:words{!!selected_field}]"/>

Here when you select multiple choices, the search returns a result if all choices existed in the topic field.

In my above example I used two fields for content/search in a single filter only for illusteration

2 Likes

Here’s another approach. It generates the filter string and then uses subfilter to execute that filter string (change contains:topics to contains:tags to test it out on tiddlywiki.com):

<$edit-text tiddler="$:/generated-list-topic-state" field="topics"/>
<$let 
  lbrace="[" rbrace="]"
  filter={{{ [{$:/generated-list-topic-state!!topics}split[,]addprefix<lbrace>addprefix[contains:topics]addsuffix<rbrace>join[]addprefix<lbrace>addsuffix<rbrace>] }}}>

filter = <$text text=<<filter>>/>

{{{ [subfilter<filter>] }}}

</$let>
3 Likes

@Mohammad This certainly simplifies the solution. Sometimes I just overthink the problem. Thanks.

1 Like

On the subject of the Topic I just wanted to add that with 5.3.x we now have functions which are filters that can be used in many ways. You can use them to break down complex filters into simpler pieces for reuse and easy to read large filters. The can also be used to create new filter operators, output text strings or used as parameter values.

And much much more…

This solution actually generates a dynamic filter.

would be better to have trim[] e.g. split[,]trim[] to handle input like one, two, three