Inline script for grouping and listing on multiple fields

100 views
Skip to first unread message

passingby

unread,
Dec 5, 2012, 12:34:55 PM12/5/12
to tiddl...@googlegroups.com
Hello friends,
I am creating a simple TW to note down the events of my personal life. Each tiddler tagged as 'Event' has following fields:
event_what, event_when, event_why, event_who, event_how
These event tiddlers show as an html input form when in edit mode and tabular form when in view mode(with help of TaggedTemplateTweakPlugin).

Now what I need is to create a timeline in which all event tiddlers are listed after being grouped into years, then months and then date wise. So my list should look like this:
!2011
!!1 (January)
 *[[abc_event_in_jan]]
 *[[xyz_event_in_jan]]
!!5 (May)
 *[[jkjhkj_event]]
 *[[vdfvdf]]
!2012
!!4 (April)
  *[[some_event_in_April]]
  *[[another-event-in-April]]

So in order to accomplish this I am trying to use inline script something like this:
<script>
    var years={};
    var events= store.getTaggedTiddlers("Event");

    for(var e=0;e<events.length;e++){
        var y= events[e].fields.event_year;
        var m=events[e].fields.event_month;
         if(years[y]===undefined) years[y]={};
        if(years[y][m]===undefined) years[y][m]=[]
        years[y][m].push(events[e]);
    }
</script>

So now I have an object which contains properties according to month numbers which are associated with arrays which contain the event tiddlers. Now what I want is to print out the years (asc/desc) and then months (asc/desc) and then a list of event tiddlers in that month sorted on date. I'd be grateful if you could provide a little idea how this can be done.

whatever

unread,
Dec 6, 2012, 10:05:31 AM12/6/12
to TiddlyWiki
Hi!

Try this:
<script>
var years = [];
var e, f;
var eventsAll = store.getTaggedTiddlers("Event");
for (e = 0; e < eventsAll.length; e++) {
years.pushUnique(eventsAll[e].fields.event_year);
}
years.sort();
for (f = 0; f < years.length; f++) {
wikify("!" + years[f] + "\n", place);
for (e = 0; e < eventsAll.length; e++) {
if (eventsAll[e].fields.event_year === years[f]) {
wikify("!!" + eventsAll[e].fields.event_month + "\n*" +
eventsAll[e].title + "\n", place);
}
}
}
</script>

It probably needs some tweaking to match your needs. Years are sorted,
but months and events aren't.

w

passingby

unread,
Dec 10, 2012, 10:08:19 AM12/10/12
to tiddl...@googlegroups.com
After a lot of tinkering and experimenting with javascript snippets found online I have finally decided that the simplest way is the best way. 
I decided to do away with event_year, event_month, event_date fields. Instead just kept a field event_when. And in the associated textbox I take care to enter the value as yyyy,mm,dd so that this string when retrieved can be directly used to create a javascript Date object using new Date(DateString) syntax.

So I have put this code in a tiddler and tagged it with systemConfig:

var sortNestedFieldDate = function (arr) {
    arr.sort(function (a, b) {
        var valueA = new Date(a.fields.event_when);
        var valueB = new Date(b.fields.event_when);

        if (valueA < valueB) {
            return -1;
        } else if (valueA > valueB) {
            return 1;
        } else {
            return 0;
        }
    });
};

And then in a separate tiddler I call this code as inline script (within script tags):

<script>
var events= store.getTaggedTiddlers("Event");
sortNestedFieldDate(events);
events.sort();
var out=[];
for  (var i=0;i<events.length;i++){
 out.push(events[i].fields.event_when.toString()+":   [["+events[i].title+"]]");
}

return out.join("\n");
</script>

This prints out a list of tiddler sorted on date (field event_when) of the event. Hope this helps some future TW user.
As a side note I was a little bit disappointed that there is no DatePicker plugin available in TW world. There is one interwined in mGSD TW but for me it is near impossible to create a decent workable independent code out of it. 

Message has been deleted

passingby

unread,
Dec 11, 2012, 7:46:28 AM12/11/12
to tiddl...@googlegroups.com
wow. now that I am using just a single field to store date, I find out that I can easily use MatchTagsPlugin to produce a list in chronological order:
<<matchTags inline "[[%0]]" "\n" sort:event_when event>>
Reply all
Reply to author
Forward
0 new messages