field content as Number

85 views
Skip to first unread message

sini-Kit

unread,
Nov 28, 2014, 12:32:54 PM11/28/14
to tiddl...@googlegroups.com
I have two fields in my tiddler
field1 = 3
field2 = 2
In my macros I want to get sum of fields
var sum='<$view field="field1"/>' + '<$view field="field2"/>' 

I want macros to show result "5" but TW5 get fields only as strings.
 

Mark S.

unread,
Nov 28, 2014, 1:44:22 PM11/28/14
to tiddl...@googlegroups.com
I don't believe you can currently add things in TW5

Here's a widget I put together in a few minutes based on an existing widget. Be sure to back up and save your data before using. There shouldn't be anything dangerous here since it doesn't modify the store ... but who knows?

Use like

   <$addup val1="20" val2="80" />

You can have up to 5 valx attributes

To import this code into your own TW, just find an existing plugin (e.g. $:/core/modules/widgets/button.js), clone it, rename the clone to $:/core/modules/widgets/addup.js and replace the code with the code that follows or if that doesn't work, with the code in the attached file. Reload the TW to activate the plugin.

 


/*\
title: $:/core/modules/widgets/addup.js
type: application/javascript
module-type: widget

Sum up to 5 variables widget

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

var Widget = require("$:/core/modules/widgets/widget.js").widget;

var AddUpWidget = function(parseTreeNode,options) {
    this.initialise(parseTreeNode,options);
};

/*
Inherit from the base widget class
*/
AddUpWidget.prototype = new Widget();

/*
Render this widget into the DOM
*/
AddUpWidget.prototype.render = function(parent,nextSibling) {
    this.parentDomNode = parent;
    this.computeAttributes();
    this.execute();
    var textNode = this.document.createTextNode(this.currentSum);
    parent.insertBefore(textNode,nextSibling);
    this.domNodes.push(textNode);
};

/*
Compute the internal state of the widget
*/
AddUpWidget.prototype.execute = function() {
    // Get parameters from our attributes
    //this.filter = this.getAttribute("filter");
    this.val1 = this.getAttribute("val1","0") ;
    this.val2 = this.getAttribute("val2","0") ;
    this.val3 = this.getAttribute("val3","0") ;
    this.val4 = this.getAttribute("val4","0") ;
    this.val5 = this.getAttribute("val5","0") ;

    // Execute the math
    this.currentSum=
Number(this.val1)+Number(this.val2)+Number(this.val3)+
    Number(this.val4)+Number(this.val5) ;


};

/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
AddUpWidget.prototype.refresh = function(changedTiddlers) {
    // Re-execute the filter to get the count
    this.computeAttributes();
    var oldCount = this.currentSum;
    this.execute();
    if(this.currentSum !== oldCount) {
        // Regenerate and rerender the widget and replace the existing DOM node
        this.refreshSelf();
        return true;
    } else {
        return false;
    }

};

exports.addup = AddUpWidget;

})();

addto.js

sini-Kit

unread,
Nov 29, 2014, 11:09:13 AM11/29/14
to tiddl...@googlegroups.com
it works. Thank you.

пятница, 28 ноября 2014 г., 21:44:22 UTC+3 пользователь Mark S. написал:
Reply all
Reply to author
Forward
0 new messages