TWc sorting custom field question

88 views
Skip to first unread message

Daniel

unread,
Sep 24, 2013, 9:00:10 AM9/24/13
to tiddl...@googlegroups.com
I want to sort tiddlers after a custom date field and format results in a table so I tried

<<tiddler ListaTB14Email##out with: {{
    var now = new Date();
    var thismonth = now.getMonth();
    var out=["!!!Email TB14\n|sortable|k\n|Datum|Mail|h"];
    var fdatum = "fdatum";
    var tids=store.getTaggedTiddlers("kundkort");
    for (var i=0; i<tids.length; i++) {
        var t=tids[i].title;
        var bearbetningsdatum = new Date(store.getValue(t,"fdatum"));
        var year = bearbetningsdatum.getFullYear();
        if ( year < 10 ) year = '0' + year;
        var month = bearbetningsdatum.getMonth()+1;
        if ( month < 10 ) month = '0' + month;
        var day = bearbetningsdatum.getDate();
        if ( day < 10 ) day = '0' + day;
        var date = year + '-' + month + '-' + day;
        if (month == thismonth || month == thismonth + 1 || month == thismonth + 2) {
            var email=store.getValue(t,"email");
            var email2=store.getValue(t,"email2");
            out.push("|%0|%1,%2|".format([date,email,email2]));
        }

    }
    out.join("\n");
}}>>

But sorting the results is missing. So I wonder how to do that? Also if this is overly complicated way of getting the result I'm happy to hear suggestions on how to simplify it.

Daniel

Tobias Beer

unread,
Sep 24, 2013, 2:39:01 PM9/24/13
to tiddl...@googlegroups.com
Something like...

var tids = store.sortTiddlers(store.getTaggedTiddlers("kundkort"), 'fdatum');

Ah, here's your transclusion assuming you want the last 60 days...

<<tiddler ListaTB14Email##out with: {{
var now = new Date(),
out=['!!!Email TB14\n|sortable|k\n|Datum|Mail|h'];
field = 'fdatum',
tag = 'kundkort';

store.sortTiddlers(
store.getTaggedTiddlers(tag),
field
).map(function(t){
var d = new Date(store.getValue(t,field));
if((now - d) < 2*30*24*60*60*1000)
out.push(
"|%0|%1,%2|".format([
d.formatString('YYYY-0MM-0DD'),
store.getValue(t,"email"),
store.getValue(t,"email2")
])
);
});
out.join('\n');
}}>>

- tobias

Daniel

unread,
Sep 25, 2013, 5:52:37 AM9/25/13
to tiddl...@googlegroups.com
Thank you Tobias! That looks very good. Unfortunately it doesn't produce what I want and I suspect it has something to do with my "fdatum"-field. It doesn't contain a date object, but a string like yyyy-mm-dd. The result is something like this (which isn't in date order):

DatumMail
2013-10-21  ,som...@address.com
2013-09-10  som...@address.com,
2013-09-18  som...@address.com,som...@address.com
2013-10-10  som...@address.com,
2014-02-01  
som...@address.com,som...@address.com
2013-08-20 
som...@address.com,

Tobias Beer

unread,
Sep 25, 2013, 7:42:19 AM9/25/13
to tiddl...@googlegroups.com

Thank you Tobias! That looks very good. Unfortunately it doesn't produce what I want and I suspect it has something to do with my "fdatum"-field. It doesn't contain a date object, but a string like yyyy-mm-dd. The result is something like this (which isn't in date order):

Not sure I follow. The following yields a proper date object in my console...

new Date('2013-09-18')

So, I can't quite say why sorting fails ...considering how your date field should be sortable as strings. Testing that locally works for me.

- tobias

Tobias Beer

unread,
Sep 25, 2013, 7:46:04 AM9/25/13
to tiddl...@googlegroups.com
Hi Daniel,
 
So, I can't quite say why sorting fails ...considering how your date field should be sortable as strings. Testing that locally works for me.
 

Daniel

unread,
Sep 25, 2013, 8:01:04 AM9/25/13
to tiddl...@googlegroups.com
Tobias,

You are right. I have to try and disable some plugins that might interfere and see if I can find why my tw isn't behaving.

Daniel

Tobias Beer

unread,
Sep 25, 2013, 9:18:07 AM9/25/13
to tiddl...@googlegroups.com
What version are you running?

Daniel

unread,
Sep 25, 2013, 9:39:07 AM9/25/13
to tiddl...@googlegroups.com
2.7.1

On Wednesday, September 25, 2013 3:18:07 PM UTC+2, Tobias Beer wrote:
What version are you running?

Daniel

unread,
Sep 25, 2013, 11:54:57 AM9/25/13
to tiddl...@googlegroups.com
The data in the field was inconsistent. Some dates were in different format. Thanks again Tobias for providing the solution on how to do the transclusion.

Daniel
Reply all
Reply to author
Forward
0 new messages