Dropdowns and Tags

17 views
Skip to first unread message

David C

unread,
Oct 24, 2009, 6:02:52 PM10/24/09
to TiddlyWiki
In a form I have a field that I'd like to be a list of tiddlers based
on a tag. I know I use select to create the drop down list but can't
seem to get it to populate with the list of tiddlers.

My test tiddler look like this:
<html>
<select name=morph><<tiddler DropdownOptionList with: "Morph">></
select>
</html>

Tiddler DropdownOptionList looks like this:
<<forEachTiddler
where
'tiddler.tags.contains("$1")'
sortBy
'tiddler.title'
write '"<option>"+tiddler.title'>>

Without the html tags the test tiddler looks fine but doesn't create a
dropdown list. With them it results in a blank box. Am I missing
something critical or am I just going about this all wrong?



FND

unread,
Oct 30, 2009, 11:55:32 AM10/30/09
to tiddl...@googlegroups.com
> My test tiddler look like this:
> <html>
> <select name=morph><<tiddler DropdownOptionList with: "Morph">></
> select>
> </html>

I assume you're using a plugin to allow nesting macro calls in raw HTML
sections?

> Without the html tags the test tiddler looks fine but doesn't create a
> dropdown list. With them it results in a blank box.

You probably wanna insert the HTML and SELECT tags once in addition to
creating an OPTION element per item.
IIRC, ForEachTiddler offers begin/end parameters for that purpose.

If not, a test case would help:
http://tiddlywiki.org/wiki/Troubleshooting


-- F.

David C

unread,
Nov 4, 2009, 6:43:20 AM11/4/09
to TiddlyWiki
Never saw your reply FND.

I was not using a plugin for allowing a macro nested into an html form
and that was my big issue. Also my syntax was not xhtml compliant but
I don't think that would have helped much.

So I wrote my solution as a new macro selectfromlist creating my
dropdownlist from items of a particular tag. I created another one
called htmlify that I use to wrap the rest of my input tags with. Here
they are:

config.macros.selectfromlist = {
handler: function
(place,macroName,params,wikifier,paramString,tiddler) {
var prms = paramString.parseParams(null, null, true);
var name = getParam(prms, "name");
var list = getParam(prms, "tag");
var output;
var tiddlers = store.getTaggedTiddlers(list);
var nPlugins = tiddlers.length;
for(var i=0; i<nPlugins; i++) {
var p = getPluginInfo(tiddlers[i]);
output += "<option value=\""+p.title+"\">"+p.title+"</
option> ";
}
wikify("<html><select name="+name+"> "+output+" </select></
html>",place);
}
};

config.macros.htmlify = {
handler: function
(place,macroName,params,wikifier,paramString,tiddler) {
var parm = params.length > 0 ? params[0] : "world";
wikify("<html>"+parm+"</html>",place);
}
};

An example of using both in a tiddly:
|Faction: |<<htmlify '<input name=faction type=text />' >>|
|Morph: |<<selectfromlist name:morph tag:Morph>>|

Måns

unread,
Nov 4, 2009, 8:09:18 AM11/4/09
to TiddlyWiki
Hi David

I'm trying to test your htmlify macro - but it gives me identifier
errors....
Is it the params[0] : "world"; which cause problems? Can I replace
"world" with a relative value to get the right output?

YS Måns mårtensson

David Cooper

unread,
Nov 4, 2009, 8:29:06 AM11/4/09
to tiddl...@googlegroups.com
That line was ripped out of the helloWorld macro that is used as an
example on the tiddlywiki.org site. I never changed out the word
"world". I would change that line to read:
var parm = params.length > 0 ? params[0] : "";

You could just as easily change it to:
var parm = params[0];

Now beyond that, I've used only Firefox 3.5.4 to test and didn't run
into an issue. Let me know if either of those corrections worked for
you. I am working on getting a testing tiddler up on tiddlyspot as it
had been suggested to me.

FND

unread,
Nov 4, 2009, 9:38:37 AM11/4/09
to tiddl...@googlegroups.com
> So I wrote my solution as a new macro selectfromlist creating my
> dropdownlist from items of a particular tag.

Looks good. Instead of stitching together strings of HTML markup, you
could directly create DOM elements - but that's up to you, of course.


-- F.

David Cooper

unread,
Nov 4, 2009, 10:33:38 AM11/4/09
to tiddl...@googlegroups.com
One step at at time. ;)

That is my next area to look into. I keep hearing about DOM elements
but I know next to nothing about it. Know of any good resources?

Eric Shulman

unread,
Nov 4, 2009, 11:55:13 AM11/4/09
to TiddlyWiki
> So I wrote my solution as a new macro selectfromlist creating my
> dropdownlist from items of a particular tag.

Here's a non-plugin TW-native syntax for definining and filling an
HTML 'select list' element...

First, create a tiddler, e.g., [[MakeList]], containing:

<html><select name="$1" size=1></select></html><<tiddler {{
var list=place.lastChild.firstChild;
   var tids=store.getTaggedTiddlers("$2");
  for(var i=0;i<tids.length; i++)
list.options[list.length]=new Option(tids[i].title,tids
[i].title);
"";}}>>

Note use of "$1" and "$2"... these are 'substitution markers' used to
insert parameter values when 'transcluding' this tiddler into another
via the <<tiddler>> macro, like this:
<<tiddler MakeList with: "listname" "tagvalue">>

Also, it doesn't seem that <<htmlify>> is really needed. It doesn't
really make the syntax any cleaner, and is actually a few bytes longer
than using regular TW syntax to embed the HTML block directly.
Compare:
<<htmlify '<input name=faction type=text />' >>
vs
<html><input name=faction type=text /></html>

Thus, using the MakeList transclusion and HTML syntax above, you could
write the following to produce your desired results:
-------------------
|Faction: |<html><input name=faction type=text /></html>|
|Morph: |<<tiddler MakeList with: morph Morph>>|
-------------------

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios

David Cooper

unread,
Nov 4, 2009, 3:30:07 PM11/4/09
to tiddl...@googlegroups.com
I completely agree that htmlify is a hack that really doesn't reduce
code. At the time I wrote it, I needed a fix for a few forms. I'll
probably wind up removing it or changing it to something cleaner.
Maybe when I learn about DOM objects as FND suggested.

Your solution is elegant. As I have no DOM knowledge, your use of the
options method really cleans things up. I guess I'm going to look for
a DOM book tonight.

I am toying with the code you just posted in a clean wiki and finding
it doubles the results in the dropdown list.
It seems that this line is the culprit:
list.options[list.length]=new Option(tids[i].title,tids[i].title);

I changed it to this and it works much better:
list.options[i]=new Option(tids[i].title,tids[i].title);

Thanks for your insight!

ChrisToonet

unread,
Nov 8, 2009, 4:39:03 AM11/8/09
to TiddlyWiki
hello
I'm very satisfied with this solution !
Is it possible to tranfer as a "tag" the selected item with your
method ???
thanks

ChrisToonet

unread,
Nov 8, 2009, 7:30:50 AM11/8/09
to TiddlyWiki
hello !
an other ask !
why my item selected is not stored in my tiddler ???
is it any thing to add at it??
thanks
Reply all
Reply to author
Forward
0 new messages