Javascript for beginners...

186 views
Skip to first unread message

Pierdonato Inglese

unread,
Jan 15, 2018, 12:18:13 PM1/15/18
to mementodatabase
Hi, sorry my bad english ...

I have 2 tables (libraries), "orders" and "order details".

"orders" has the "order number" field.

"order details" has the fields "order number" (one-to-many relationship with "orders"), "quantity" and "price".

How do I get the general total (sum(quantity * price)) from "order details" in the table (library) "orders"? It's possible? The example specified in the wiki does not work for me.

Example: Order containing items
The library Orders contains orders and the library Items contains items. Orders contains a link Items to library Items. Items contains the fields Quantity and Price. To calculate the cost of the order, the JavaScript field Cost can be computed as follows:

var items = field('Items'); // CHANGE Items to your link field name
var cost = 0;
for (var i in items)
cost += items[i].field('Quantity') * items[i].field('Price');

cost; // The result is the COMPUTED COST


roberto dal maso

unread,
Jan 16, 2018, 6:58:24 AM1/16/18
to mementodatabase
Hi,
maybe this page of the wiki can help you http://wiki.mementodatabase.com/index.php/Calculation_field

Pierdonato Inglese

unread,
Jan 16, 2018, 8:06:29 AM1/16/18
to mementodatabase
Hi,
thanks for the indication but I already tried. My situation is simple:

Order (lib)
Number order (field, txt)
Total order (Javascript, sum(Quantity)*(Price) in Detail order lib)

Detail order (lib)
Number order (field for link 1 to many)
Quantity (field, real)
Price (field, real)

how do I calculate the Total order (field in Order lib)?

Pierdonato Inglese

unread,
Jan 16, 2018, 11:24:06 AM1/16/18
to mementodatabase
I tried this other solution but it does not even work.

In the Detail order library I calculated the field "total" (quantity * price), then I did as the guide says

Example: Sum of linked amount

var amountField = "total";
var linkedEntries = field("detail order");
var numLE = linkedEntries.length;
for (var lex = 0, result = 0; lex < numLE; lex++)
result += linkedEntries[lex].field(amountField);
result;

Not found. This is because the variable "numLE". This returns me a strange number that varies with the name of the indicated linked library in linkedEntries (23 if "detail order", 22 if "detail orde", 21 if "detail ord")...

Somebody help me, please? I refer to the paid version for Android.

Bill Crews

unread,
Jan 17, 2018, 6:31:01 AM1/17/18
to Pierdonato Inglese, mementodatabase
Two choices: Triggers in order details or action in orders. I suggest the triggers.

1. Create a field in order details called "Previous subtotal" initial value of 0.

2. Create a Creating a new entry Before saving trigger in orders called "Update order total".

Use a script like the following (could have errors, though I try for none)...

var e = entry();
var order = e.field("orderLink")[0];

order.set("total", order.field("total" + (e.field("quantity") * e.field("price")));

3. Create an Updating an entry Before saving trigger in order details called "Adjust order total"

var e = entry();
var order = e.field("orderLink")[0];

order.set("total", order.field("total" + (e.field("quantity") * e.field("price")) - e.field("previous subtotal");

4. Create an Updating an entry Opening the edit card trigger in order details called "Save previous subtotal".

var e = entry();

e.set("Previous subtotal", (e.field("quantity") * e.field("price")) - e.field("Previous subtotal"));

Actually, if the initial value of Previous subtotal is really 0, you could use script 3 in place of script 2.

If you choose the action option, you'd calculate the totals every time you open the orders library, which would cause an initial delay.


--
You received this message because you are subscribed to the Google Groups "mementodatabase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mementodatabase+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Pierdonato Inglese

unread,
Jan 18, 2018, 5:21:10 PM1/18/18
to mementodatabase
Thanks, it took me a while to understand but it works. I'm not a programmer...
You have been very kind and helpful.

I was wondering if you can set "readonly" the fields used for calculations. Editable only with calculations, not manually.

Memento is very useful

Bill Crews

unread,
Jan 18, 2018, 5:32:24 PM1/18/18
to Pierdonato Inglese, mementodatabase
Well, a JavaScript field is read-only. If you want it to contain a number, like 3.14, just use the script...

3.14

Pierdonato Inglese

unread,
Jan 18, 2018, 5:51:48 PM1/18/18
to mementodatabase
Can I perform calculations with triggers in Javascript fields? I've tried but the calculations work only by setting the fields as numbers.
Sorry, I can not explain ... My English is Google Translator
:-(

Bill Crews

unread,
Jan 18, 2018, 6:48:56 PM1/18/18
to Pierdonato Inglese, mementodatabase
I'm sorry. A trigger is one thing. A JavaScript field is another.  Both employ JavaScript. Anything that is calculable can be calculated in JavaScript. I don't know what else to say.

Please post in your language, as I do. The rest of us may use tools to translate. I think it will work better.

Pierdonato Inglese

unread,
Jan 18, 2018, 7:14:06 PM1/18/18
to mementodatabase
For the calculations you suggested I used numeric fields. The result is excellent but I would like these fields to be blocked to avoid manual changes, that is to avoid that you can type some number in the fields by mistake. It can be done?

Another thing:
I've also set up calculations for deleting items, not just for adding and editing data. It works well if I delete a record after I see it by clicking on it. It does not work, however, if I delete the records by selecting them from the main window of the library because the fields specified in the triggers are not found. Can this problem be solved in any way?

Tony G

unread,
Jan 27, 2018, 6:47:25 PM1/27/18
to mementodatabase
Per piaccere, crei una nuova discussione per questa domanda.
Grazie.
Reply all
Reply to author
Forward
0 new messages