jam-py / jam-py

Jam.py is an event-driven framework for the development of database applications with a Web interface.
http://jam-py.com
BSD 3-Clause "New" or "Revised" License
451 stars 85 forks source link

Computed fields #133

Closed KellerKev closed 4 years ago

KellerKev commented 4 years ago

Is there a way I can create my own computed fields?

Would you have an example?

I. E. Deviding 2 values by 2 and the multiply by 10.

platipusica commented 4 years ago

Hi there,

That is how Demo Invoices work. The column Total is calculated by TAX, amount, etc.

Have look at Invoices Client Module, if remember well.

Regards

jam-py commented 4 years ago

Hello,

There are no special calculated fields now. But you can change the way fields are displayed in controls by using such event handlers as https://jam-py.com/docs/refs/client/item/on_get_field_text.html https://jam-py.com/docs/refs/client/item/on_field_get_html.html

Regards, Andrew Yushev

ср, 15 янв. 2020 г. в 16:33, platipusica notifications@github.com:

Hi there,

That is how Demo Invoices work. The column Total is calculated by TAX, amount, etc.

Have look at Invoices Client Module, if remember well.

Regards

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jam-py/jam-py/issues/133?email_source=notifications&email_token=ACJ2AGOQMMRGIRLKENZITW3Q54GDDA5CNFSM4KHDLX4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJAKE5Q#issuecomment-574661238, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACJ2AGLBHDAHYRL5QWC4TTLQ54GDDANCNFSM4KHDLX4A .

KellerKev commented 4 years ago

Thanks you both! But how can I then save the computed values to the database in case I want to pick those computed values up in another view?

I am also happy for workarounds i.e. run a scheduled script

platipusica commented 4 years ago

Not sure if we are on the right page, but if you look at Demo Invoices table, the value Total is saved in the database, even though it is calculated on the fly: CREATE TABLE IF NOT EXISTS "DEMO_INVOICE_TABLE" ( "ID" INTEGER PRIMARY KEY, "DELETED" INTEGER, "MASTER_ID" INTEGER, "MASTER_REC_ID" INTEGER, "TRACK" INTEGER, "QUANTITY" INTEGER, "UNITPRICE" REAL, "AMOUNT" REAL, "TAX" REAL, "TOTAL" REAL, "INVOICE_DATE" TEXT, "CUSTOMER" INTEGER);

Here I've added id for both Invoices Tables (master and Detail), and enabled sorting for id. Now, I update Invoice with Record ID =1 (sort it by Record id first) and voila:

Before update "Balls to the wall" quantity: sqlite> select * from DEMO_INVOICE_TABLE where id=1; 1|0|16|1|2|1|0.99|0.99|0.05|1.04|2014-01-01|2

After update "Balls to the wall" quantity to 10: sqlite> select * from DEMO_INVOICE_TABLE where id=1; 1|0|16|1|2|10|0.99|9.9|0.5|10.4|2014-01-01|2

So 10.4 is calculated on the fly. demo_1.5.29_5.4.82_2020-01-16_09-18-21.zip

platipusica commented 4 years ago

Screenshot from 2020-01-16 09-22-14

rrad0812 commented 4 years ago

Hi, all Strictly speaking, computational fields should be a feature of the Field class, such as type or default value. Also, they should not be stored in the database as they are calculated.

In some old systems, a functional call could be made to calculate the default value. Jam.py doesn't have that. I think the author opted for such a solution because of its speed of execution.

Pre and post triggers can get your job done before placing it in the base, or as in the @platipusica example. To run, you have the triggers mentioned by @Andrew in a previous post.

Generally, it would not be bad to have them, but there is a way to overcome the problem.

Regards, rrad

KellerKev commented 4 years ago

Thanks everyone! This issue can be closed now I think.

KellerKev commented 4 years ago

Would you have a quick tutorial or is there a sample in one of your videos how to enable a computed field please?

I will review the invoices app but i thought in case there is already sometbibg like this out there, it wouldn't hurt to point me there.

Thanks!

platipusica commented 4 years ago

Hi, I do not know of any unfortunately. The only thing Jam.py can do is to calculate some values automatically. Like on pic, bottom row.

The rest you see is case by case basis, depending on what your App does. Like dividing 2 values by 2 and the multiply by 10 is pure JavaScript programming which is covered in invoices, as you've mentioned. We can spin up a small App covering this, but one would still need to know JS and apply to other cases.

Here for the start with my limited JS knowledge adding value1 to value2 on the FORM:

Screenshot from 2020-02-07 12-33-47

calc_1.0.0_5.4.89_2020-02-07_12-34-32.zip

You'll need to create new project and Import this Export.

Or, this project where you update value1 and value2 on the GRID to get the result on the screen. You'll need to add POST for the grid but you get the idea...

calc_1.0.1_5.4.89_2020-02-07_12-40-00.zip

Hope this helps.

jam-py commented 4 years ago

If you mean how to calculate field values when some event occurs then you can write on_field_changed event handler https://jam-py.com/docs/refs/client/item/on_field_changed.html

пт, 7 февр. 2020 г. в 07:35, platipusica notifications@github.com:

Hi, I do not know of any unfortunately. The only thing Jam.py can do is to calculate some values automatically. Like on above pic, bottom rows.

The rest you see is case by case basis, depending on what your App does. Like dividing 2 values by 2 and the multiply by 10 is pure JavaScript programming which is covered in invoices, as you've mentioned. We can spin up a small App covering this, but one would still need to know JS and apply to other cases.

Here for the start with my limited JS knowledge: [image: Screenshot from 2020-02-07 12-33-47] https://user-images.githubusercontent.com/9026100/74001325-25745080-49a6-11ea-9eca-c16f313e7da1.png

calc_1.0.0_5.4.89_2020-02-07_12-34-32.zip https://github.com/jam-py/jam-py/files/4168978/calc_1.0.0_5.4.89_2020-02-07_12-34-32.zip

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jam-py/jam-py/issues/133?email_source=notifications&email_token=ACJ2AGOAY7RSCMDOJM3URODRBTQHTA5CNFSM4KHDLX4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELBWDAQ#issuecomment-583229826, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACJ2AGOM243ZFESAYDPBM23RBTQHTANCNFSM4KHDLX4A .

jam-py commented 4 years ago

In the demo app this there is this event handler. It is in the invoices.invoice_table.js module. Expand Journals in the project tree select invoice_table and click Client module. You can use debugger to see what is going on: https://youtu.be/Dk9GfwigVdg

пт, 7 февр. 2020 г. в 09:55, Andrew Yushev yushevaa@gmail.com:

If you mean how to calculate field values when some event occurs then you can write on_field_changed event handler https://jam-py.com/docs/refs/client/item/on_field_changed.html

пт, 7 февр. 2020 г. в 07:35, platipusica notifications@github.com:

Hi, I do not know of any unfortunately. The only thing Jam.py can do is to calculate some values automatically. Like on above pic, bottom rows.

The rest you see is case by case basis, depending on what your App does. Like dividing 2 values by 2 and the multiply by 10 is pure JavaScript programming which is covered in invoices, as you've mentioned. We can spin up a small App covering this, but one would still need to know JS and apply to other cases.

Here for the start with my limited JS knowledge: [image: Screenshot from 2020-02-07 12-33-47] https://user-images.githubusercontent.com/9026100/74001325-25745080-49a6-11ea-9eca-c16f313e7da1.png

calc_1.0.0_5.4.89_2020-02-07_12-34-32.zip https://github.com/jam-py/jam-py/files/4168978/calc_1.0.0_5.4.89_2020-02-07_12-34-32.zip

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jam-py/jam-py/issues/133?email_source=notifications&email_token=ACJ2AGOAY7RSCMDOJM3URODRBTQHTA5CNFSM4KHDLX4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELBWDAQ#issuecomment-583229826, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACJ2AGOM243ZFESAYDPBM23RBTQHTANCNFSM4KHDLX4A .

platipusica commented 4 years ago

Here you go,

the grid calculations and form calculations, all saved in the DB:

function calc(item) { item.result.value = (((item.value1.value + item.value2.value) * item.multiplied_by.value) / item.division_by.value); }

calc_1.0.2_5.4.89_2020-02-10_16-26-58.zip

jam-py commented 4 years ago

I think the problem is that you divide by 0. Try, please

function calc(item) { if (item.division_by.value) { item.result.value = (((item.value1.value + item.value2.value) * item.multiplied_by.value) / item.division_by.value); } else { item.result.value = 0; } }

пн, 10 февр. 2020 г. в 11:29, platipusica notifications@github.com:

Here you go,

the grid calculations and form calculations, all saved in the DB:

function calc(item) { item.result.value = (((item.value1.value + item.value2.value) * item.multiplied_by.value) / item.division_by.value); }

calc_1.0.2_5.4.89_2020-02-10_16-26-58.zip https://github.com/jam-py/jam-py/files/4179676/calc_1.0.2_5.4.89_2020-02-10_16-26-58.zip

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jam-py/jam-py/issues/133?email_source=notifications&email_token=ACJ2AGPQ4IELYCQRYXQURVTRCEF6TA5CNFSM4KHDLX4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELHULZQ#issuecomment-584009190, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACJ2AGMA4KFEJF7JDBTMTP3RCEF6TANCNFSM4KHDLX4A .

KellerKev commented 4 years ago

Thanks guys!

Sorry I need this amount of handholding, but I was traveling again all week and just got back to it.

I think I am pretty much there. I also just watched the Debugger Youtube video.

Thing is if there would be just a simple 4 step tutorial, because the code alone doesnt help me from my jam.py knowledge context. While I am happy to invest more time I think I just need to get this sample going to show to my peers.

Something like this:

Step 1. Create sample catalog: LemonadePrices Field1: Text Lemonade name Field2: Integer: ListPrice Field3: Integer: Margin Field4: Integer: Result

Step 2. Add Computation in Client Field4 (Result field) Integer: Computate --> ListPrice+Margin / (devided by 2)

How would I go about those steps. Step1 is clear.

But how do I add the computation on the client side.. the code you just showed me here, to get the computation for this simple example done.

Sorry for my basic question. But once field computation are understood, I can persuade my colleagues and stakeholders and our coders to get interesed in jam.py, but it seems i am not able to present it currently as my jam.py knowledge is still basic

KellerKev commented 4 years ago

I also see your zip files and tried to unzip them into a fresh jam.py project, but I couldnt access any of the samples. I suppose more steps are required to get your code working in my tables.

KellerKev commented 4 years ago

So you use an event function. Then assign a field of the catalog to "item" and then do the item calculation during that event.

KellerKev commented 4 years ago

var item = field.owner; in your zip file. Can I use the field4 i.e. reuslt field here in my case instead of the owner field? Then I am think I agetting there.

On change -> Calc functin -> Calc function calcs value for field4 -> values gets posted into db.

KellerKev commented 4 years ago

If I i.e. change the calculation just to test things out in demo.js in the demo app with VI from my Linux terminal in reverts back to its state before but within jam.py.. in the web editor I can not access the calc function

rrad0812 commented 4 years ago

Hi, In my previous email I tried to explain the situation with the calculated fields in jam.py. Unfortunately, I overlooked to write the fact directly: Calculated fields, with calculation and display only on the client, by default, do not exist in Jam.py. There is, but not only for display, but also for entry into the database, therefore, just like any other
field, however, you count its value in one of the mentioned events.

Maybe we can it, but with much more than basic knowledge, you could try changing the template for an item, adding an html field, then shooting it in the html field, etc ... Or maybe a variant with a virtual table, so displaying the results as lookup fields from a virtual table ...

These are just ideas.

ps. If I may notice, the purpose of this section is for version errors, while this discussion should be in the Jam.py google group.

rrad

KellerKev commented 4 years ago

I managed to chance the calculation in the invices.js vie the jam.py builder interface, but the calculations are still being done in the invoice detail form.

Happy to move to the google group.

Will close this for now .

Thanks again

platipusica commented 4 years ago

If I i.e. change the calculation just to test things out in demo.js in the demo app with VI from my Linux terminal in reverts back to its state before but within jam.py.. in the web editor I can not access the calc function

Not sure why would you use "vi" editor at all :) Jam has a code control in the sqlite db, overwriting the flat files after saved in the UI Builder. It is like Django Admin but on steroids :)

No need to unzip the Exports. It literally takes 30 seconds to create a new Project and Import the Export zip, and review there.

D.

platipusica commented 4 years ago

Here:

https://youtu.be/owJFQIPTRsk

rrad0812 commented 4 years ago

Hi, I just assume, You haven't opened all the other files in the editor, when you go to web editor the file with calc function remains open in you editor, etc ...

Listen to @platipusica, use the Jam.py editor.

Once most of Jam.py is clearer to you, get back to your editor. By the way, it would not be a bad idea to write how to make the environment outside, let the rest of us learn something new.

I tried everything and it didn't work out, client-server environment, with dibgers on is a bit of a harsh story, unfortunately ...

rrad

On Fri, Feb 14, 2020 at 2:45 AM platipusica notifications@github.com wrote:

Here:

https://youtu.be/owJFQIPTRsk

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jam-py/jam-py/issues/133?email_source=notifications&email_token=AF5U4IUF4YKBYNFBQ3ITBLTRCXZUTA5CNFSM4KHDLX4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELXIPUY#issuecomment-586057683, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF5U4IV5E2666MFBWM4ZINTRCXZUTANCNFSM4KHDLX4A .