Search in specified field

A seemingly simple use case that I am having a lot of trouble trying to get working.

I would like a dropdown and a text input field. The dropdown can select a field name from a pre-set list, and a search term can be typed into the entry field. Either while you type, or via the use of a “Search” button, this tool will search through all tiddlers for that specified value in the specified field.

I think the main sticking point for me is that I am not sure if it is possible to use a variable for the suffix of a filter parameter ([search:field[term]] in this case). Can I make the field suffix variable, and if not, is there another way to do what I am trying to do?

Thanks!
Scribs

As you’ve noted, it’s not obvious how to use a variable for the suffix of a filter operator.

Fortunately, there is a workaround that uses a macro to contruct the desired filter syntax, like this:

\define searchfilter() [has<where>search:$(where)${!!find}]

<!-- GET THE INPUTS -->
search in:
<$select field="where">
<$list filter="[fields[]sort[]]"><option><<currentTiddler>></option></$list>
</$select>
match: <$edit-text field="find"/>

<!-- DO THE SEARCH -->
<$let where={{!!where}}>
   <$list filter=<<searchfilter>>>
      <div><<currentTiddler>> (<<where>>={{{ [<currentTiddler>get<where>] }}})</div>
   </$list>
</$let>

Notes:

  • First, the $select list stores the desired fieldname in the where field
  • Next, the stored value is retrieved as a variable by using
    <$let where={{!!where}}>
  • Then, the searchfilter() macro definition uses a reference to $(where)$ to insert the selected fieldname into the filter syntax
  • Note that the filter uses has<where> so that only tiddlers that actually have a non-blank field value will be searched.

enjoy,
-e

1 Like

Eric,
This is perfect, thanks so much! I am always uneasy when having to combine different syntaxes (filters vs macros vs widgets)… constructions like this are going to be very useful for some other things I have been working on too!
Best,
Scribs

Just want to pitch in that I am encountering the same difficulty with defining field value for search using variable, and this solves it perfectly!

Looking at the model answer, I would never arrive at it myself :exploding_head: , and actually is still none the wiser despite it. So thanks!!! It’s remarkable how often I’m struck with some wikitext syntax issue, and with some random browsing in this forum, found the exact answer in an ongoing discussion :grinning_face_with_smiling_eyes:

1 Like