Why would a filter work properly in AdvancedSearch but not in a LetWidget?

I have a filter that’s working fine in, say, AdvancedSearch, but which doesn’t work if I put it in a template. I can’t figure out why.

If I do this:

[tag[Voter]] :filter[get[first-name]uppercase[]match[FRED]] :filter[get[last-name]uppercase[]match[FLINTSTONE]]

I get the expected result of Voter/28.

But if i do the following in a tiddler tagged Donation:

<$let 
  voter={{{ [tag[Voter]] :filter[get[first-name]uppercase[]match{!!contributor-first-name}] :filter[get[last-name]uppercase[]match{!!contributor-last-name}] }}}
>

<$text text= {{{ [{!!contribution-receipt-date}search-replace:g:regexp[\D],[]addsuffix[000]format:date[MMM DD, YYYY]] }}} />

<% if [<voter>!match[]] %>
    <$link to=<<voter>> >{{!!contributor-first-name}} {{!!contributor-last-name}} </$link>
  <% else %>
    {{!!contributor-first-name}} {{!!contributor-last-name}}
<% endif %>

</$let>

I always get the second branch, without links.

You can see this by downloading the following and dragging it onto the main page.

DonationsTest.json (31.8 KB)

If you open Voter/28, you can expand Donations at the bottom and visit one of the donations.

That donation should have a link taking me back to the voter who made the donation. It doesn’t.

The reason for the two branches is that older donations may have been made by someone no longer resident in town.

Maybe it’s just my sleepy brains, but I can’t see why it would work in one place and not the other.

Any suggestions?

For me nothing is returned from AdvancedSearch. I also checked you tiddlers tagged Voter.

When you use the let to assign the resulting list to the variable voter, the filtered transclusion assignes only the first title. Try adding at the end of the filter +[format:titlelist[]join[ ]] but then when you use that variable you may need to use the enlist operator to turn it back into a list.

  • I suggest using your filter in a list widget first before you start making its use more complex as in storing within a variable.
    • Which is what I belive the advanced search filter tab is doing with the filter you provide, ie uses $list
  • If you put the filter into a function as long as you use it the right way (inside a filter) it will return the list of titles.

I notice that in your $:/AdvancedSearch filter, you are using literal strings [FRED] and [FLINTSTONE], but in your template code, you are using {!!contributor-first-name} and {!!contributor-last-name}.

Note that, as documented here: Filter Filter Run Prefix

Perhaps you meant to use the field values from the tiddler containing the template?

If so, then you could write something like this:

<$let firstname={{!!contributor-first-name}} lastname={{!!contributor-last-name}}
  voter={{{ [tag[Voter]] :filter[get[first-name]uppercase[]match<firstname>] :filter[get[last-name]uppercase[]match<last-name>] }}}
>

Hope this helps,
-e

1 Like

I too often do this stuff late at night.

Even though I was tired, I spent the 15 minutes anonymizing my data before posting it. And then I posted the unanonymized version. D’oh! (This is all data in the public record, but I still didn’t like posting it.)

I’ve fixed that in the attachment above, and the FRED FLINTSTONE test should now work in Advanced Search > Filter.

[tag[Voter]] :filter[get[first-name]uppercase[]match[FRED]] :filter[get[last-name]uppercase[]match[FLINTSTONE]]

Sorry about that.

Thank you Tony and Eric!

I don’t think I explained very well. I was too tired and should have waited to post until today. I apologize.

I do expect there to be at most one matching result, and if by chance there’s more, I can live with matching the first one. My trouble is that it’s matching none.

Well the literal version for Advanced Search was just to show that the filter does do what I want it to when I pass the right data. I often develop filters in Advanced Search before moving them where I want and replacing the literals with whatever dynamic version I have. I was surprised when this didn’t work.

I will try your version when I’m home again. But I don’t see how it would be different. from the original.

I forgot to mention in the original that the code in question is in the bottom section of the ViewTemplate $:/_/avl/templates/Voter

Update: I just fixed the title, "in a LetWidget", not "in a ListWidget". Double-d’oh!

1 Like

I think Eric is spot on here unless as I am guessing its unlikely the field contributor-first-name is in the tiddler tagged Voter, but instead its in the donation tiddler?

Thank you. Somehow I didn’t understand it when Eric suggested it, even though it’s crystal-clear on rereading. But this made it clear to me. I’m sure that’s it. I will check this evening.

1 Like

Ah, thank you. It works just fine when I remove a spurious hyphen. (The final word should be lastname, not last-name.)

On a side not, I do like being able to format this readably:

<$let 
  firstname={{!!contributor-first-name}}
  lastname={{!!contributor-last-name}}
  voter={{{ 
    [tag[Voter]] 
    :filter[get[first-name]uppercase[]match<firstname>] 
    :filter[get[last-name]uppercase[]match<lastname>] 
  }}}
>

I guess we always could add breaks between filter runs, but I’m used to writing unreadably long lines.

Thank you very much @EricShulman. And thank you @saqimtiaz for making me reread.