How to get unique values in $select

Hi TW-Community,

I have implemented the function to edit fields from Tiddlers if they are shown and having specific tags.
Here the tag “Department”:

tags: $:/tags/ViewTemplate
title: tryout/ViewTemplate

<%if [<currentTiddler>tag[Department]] %>
<<edit_fields "Department" "caption Full_name Location">>
<% endif %>

The procedure edit_fields is this here, what is doing more or less what I want :smiley:

code-body: yes
tags: $:/tags/Global
title: tryout/edit_fields

\whitespace trim
\procedure edit_fields(selector, fields)
<$let
  config=<<currentTiddler>>
>
Please enter the data for the &nbsp; <<selector>> &nbsp;:&nbsp; {{!!caption}}.<br>
The origin name of this Data set is:  &nbsp; <<config>>.<br>
<table>
  <$list filter="[enlist<fields>]" variable="field_of_tiddler">
   <tr>
     <th><$text text={{{ [<field_of_tiddler>] }}} /></th>
     <td>
        <$select  tiddler=<<currentTiddler>> field=<<field_of_tiddler>> default=<<field_of_tiddler>> >
            <option value="">(any)</option>
            <$list filter='[tag<selector>sort[caption]]' variable="field_of_field">
                   <option><$view tiddler=<<field_of_field>> field=<<field_of_tiddler>>/></option>
            </$list>
            <$list filter='[tag<field_of_tiddler>sort[caption]]' variable="field_of_field">
                   <option><$view tiddler=<<field_of_field>> field='caption'/></option>
            </$list>
        </$select>
     </td>
     <td>
        <$edit-text tiddler=<<field_of_field>> field=<<field_of_tiddler>>/>
     </td>
  </tr>
 </$list>
</table>
</$let>
\end

The selection will be generated with these two $list commands

        <$select  tiddler=<<currentTiddler>> field=<<field_of_tiddler>> default=<<field_of_tiddler>> >
            <option value="">(any)</option>
            <$list filter='[tag<selector>sort[caption]]' variable="field_of_field">
                   <option><$view tiddler=<<field_of_field>> field=<<field_of_tiddler>>/></option>
            </$list>
            <$list filter='[tag<field_of_tiddler>sort[caption]]' variable="field_of_field">
                   <option><$view tiddler=<<field_of_field>> field='caption'/></option>
            </$list>
        </$select>

My problem is, that in some cases I have the same entries in the fields. And than the same wording also in the selection field.

My question is:
Can I avoid these double entries in the $select command?

I thought about to save the list results to a separate cache variable as list and to use this cache variable list to fill the selection with the enlist operator but I am not familiar in generating such a list especially if elements can have spaces in there.

Other ideas are also welcome.

Stefan

If I understand correctly, then the map will sort out your first part and the other filter is a get, so you can combine them and then sort in the one step and apply the unique operator.

<$select
  tiddler=<<currentTiddler>>
  field=<<field_of_tiddler>>
  default=<<field_of_tiddler>>
>
  <option value="">(any)</option>
  <$list filter="[tag<selector>]  :map[get<currentTiddler>]
      [tag<field_of_tiddler>get[caption]] +[sort[]unique[]]"
  variable="option_value">
    <option><$text text=<<option_value>></option>
  </$list>
</$select>

Hi @VikingMage,

I tried it but it is not returning the right list.
I select in the 1st list

            <$list filter='[tag<selector>sort[caption]]' variable="field_of_field">
                   <option><$view tiddler=<<field_of_field>> field=<<field_of_tiddler>>/></option>
            </$list>

a different field field_of_tiddler than in the 2nd caption

            <$list filter='[tag<field_of_tiddler>sort[caption]]' variable="field_of_field">
                   <option><$view tiddler=<<field_of_field>> field='caption'/></option>
            </$list>

Stefan

I was getting a bit turned around by field_of_field and field_of_tiddler, so I tried rewriting your code using names that made more sense to me. Let me know if I misunderstood!

  • <<field>> = your <<field_of_tiddler>>
  • I didn’t end up using your <<field_of_field>> in the $select.
  • I’m not sure which tiddler’s field you intended the $edit-text widget to edit; <<field_of_field>> wasn’t defined in this context in your original code, only within the $list used by the $select.
\whitespace trim
\procedure edit_fields(selector, fields)
<$let
	config=<<currentTiddler>>
>
Please enter the data for the &nbsp; <<selector>> &nbsp;:&nbsp; {{!!caption}}.<br>
The origin name of this Data set is:  &nbsp; <<config>>.<br>
<table>
	<$list filter="[enlist<fields>]" variable="field">
	<tr>
		<th><<field>></th>
		<td>
			<$select field=<<field>> default=<<field>> >
			<!-- Perhaps you want default="" instead? -->
				<option value="">(any)</option>
				<$list filter="[tag<selector>sort[caption]get<field>] [tag<field>get[caption]sort[]]" variable="value">
					<option><<value>></option>
				</$list>
			</$select>
		</td>
		<td>
			?? <$edit-text tiddler=<<field_of_field>> field=<<field>>/>
		</td>
	</tr>
	</$list>
</table>
\end

<<edit_fields "Department" "caption Full_name Location">>

By combining both lists into one filter, you can take advantage of the fact that filters automatically remove duplicate results.

Your original code was sorting the $select options based on the caption of the tiddler from which they came, and I tried to duplicate that with [tag<selector>sort[caption]get<field>] [tag<field>get[caption]sort[]]. Personally, I’d be inclined to use [tag<selector>get<field>] [tag<field>get[caption]] +[sort[]] instead; that way, your options would all be presented alphabetically.

1 Like

Hi @etardiff,

thank you for your support.
I changed your code with the input from @VikingMage and get the solution.

The use case for this is to have a small Database realized in TW.
The user should have the possibility to select from different sources without knowing the sources.

  1. “defined” (tagged) data
  2. Data from other datasets but not “defined” (1)
  3. Make an own definition what will be than part of (2)

Example:

  1. The Departments “HR” and “Quality” are Tiddlers and they have the tag “Department”
  2. There are Datasets available tagged as “Organization”
  3. One (or more) of the “Organization” tagged Tiddler have the field “Department” and the content is “Sales”
  4. The user will now get the selection field with “HR”, “Quality” and “Sales”. The edit and selection field is filled the data from the dateset
  5. If the user change in the edit field maybe “Sales” to “Purchasing” this will be also possible to select in other datasets

Here the final code if someone else is interested:

\whitespace trim
\procedure edit_fields(selector, fields)
<$let
	config=<<currentTiddler>>
>
Please enter the data for the &nbsp; <<selector>> &nbsp;:&nbsp; {{!!caption}}.<br>
The origin name of this Data set is:  &nbsp; <<config>>.<br>
<table>
	<$list filter="[enlist<fields>]" variable="field">
	<tr>
		<th><<field>></th>
		<td>
			<$select field=<<field>> default=<<field>> >
				<option value="">(any)</option>
				<$list filter="[tag<selector>sort[caption]get<field>] [tag<field>get[caption]sort[]]+[unique[]]" variable="value">
					<option><<value>></option>
				</$list>
			</$select>
		</td>
		<td>
		 <$edit-text tiddler=<<field_of_field>> field=<<field>>/>
		</td>
	</tr>
	</$list>
</table>
\end

Stefan

1 Like