Filtering on Fields

Am I the only person who finds the TW documentation inpenetrable at times? Seems I either get hundreds of results or none at all!

If I have a Field with different values on many Tiddlers I want to create a (hyperlink) list of each field variant to the relevant Tiddler.

Field: field1
Values: val1, val 2, val3

Output would look something like this:

val1
   Tiddler2
   Tiddler7
   Tiddler8
val2
   Tiddler1
   Tiddler3
val3
   Tiddler4
   Tiddler5
   Tiddler6

How do I achieve this?

Thanks.

I believe this should answer your question:

<$list filter="[each:list-item[field1]]">  

!!<$link/>  

<<list-links "[{!!title}listed[field1]]">>  

</$list>

Demo (the demo use a field named location and list all the countries ever listed in that field, then for each country list all the relevant tiddlers): https://demos.tiddlyhost.com/#/find-listed-links

It takes a bit of time to learn how to find relevant info in the doc, for sure. For filters specifically, you can refer to this tiddler: https://tiddlywiki.com/#Filter%20Operators

Generally you can find what you want quickly in this table.

Note that tiddlywiki is open source so if you know how to improve the doc, feel free to create a Pull Request !

See tw5-docs-pr-maker.

2 Likes

Thanks. Of course, that worked perfectly.

The link to the demos is very helpful too, thanks.

1 Like

@telumire
I probably responded too soon.

In my testing I have some fields where the “value” is two words sepparted by a space. The filter treats them as two separate values, rather that the one they are intended to be. How can I fix this?

For this filter to work you cant have words with white-spaces as they are treated as separate filter runs. The filter expect a title list, see https://tiddlywiki.com/#Title%20List. You need to either wrap your words in brackets, like a wikitext link, e.g :

[[this is only one term]] [[another one]]

Or use " "/' '.

You can test it with this code :

{{{ "one two three" +[count[]] }}} = 1
{{{ [[one two]] three +[count[]] }}} = 2
{{{ one two three +[count[]] }}} = 3

If this is an issue then you could define a list separator and treat the field as one text string to split using the separator you defined, but this will increase the complexity of the filter.

If your field contains only one title with spaces you need NOT format it to titles with square brackets.

If you are storing more than one title in a field there is now the format:titlecase operator and when manipulating lists (other than tags) we use the listops operators and listops action.

The format:titlelist (I suppose since I dont find the format:titlecase) operator would work for a field with a single title, but for multiple titles in the same field brackets or “” are needed, otherwise tiddlywiki has no way of knowing when a title begins or stop.

Oops - yes format:titlelist

If you use [[tiddler names]] I believe only space delimited is necessary (maybe not even that) but it all depends on how you are “enlisting” the list. One way to think of it is a list can be a filter, only a filter with a list of tiddlernames.

I must revisit the different list manipulating parts of tiddlywiki core to check which are the key methods used, and if more than one, I think have seen comma delimited as well.

Personally I like to code fields containing titles in a way that they are also valid as filters because then you can include a filter eg; [[title 1]] title2 [tag[mytag]]

1 Like

Using the following for my tests:
Tiddler1: Field1: “first last”

I found that wrapping the field in " " or ’ ’ did not work at all. It outputs as:

“first
   Tiddler1
last”
   Tiddler1

and the same with single quotes

Now wrapping the field in double brackets does work
Tiddler1: Field1: [[first last]]

gives

first last
   Tiddler1

I could not work out the syntax for format:titlelist or where to add a suggested space delimiter in the syntax.

So only this works, when the fields are wrapped with [[ ]]

<$list filter="[each:list-item[Field1]]">
!!<$link/>
<<list-links “[{!!title}listed[Field1]]”>>
</$list>

Advice please.

I think that this is because the operator

each:list-item[field]

expect a title list, and while using “” works in a filter run, a title list need to be formatted with brackets … this filter will accept fields containing single values without brackets, or list of values with brackets :

<$let start="[[" end="]]">

<$list filter="[get[field1]split<start>split<end>!is[blank]!match[ ]unique[]]">

!!<$link/>
<<list-links "[search:field1{!!title}]">>
</$list>

</$let>

However it’s less efficient than my previous filter so I encourage you to use the first version and always add [[brackets]] around your values. Using a properly formatted title list will improve compatibility if you need to manipulate your fields later on, and performance thanks to the list-specific filters that tiddlywiki provide.

@telumire you have convinced me. Thanks.