dependent upon 4 variables
dependent upon 4 variables
Hi
Using datatables Editor I have succeeded in writing dependent code for a stock entry system, on selection of a stock the share price
is written to the price field
.
On entry of the quantity
the total
is evaluated for each keystroke
on entering the quantity
the total
is evaluated shown below
the function below updates the total
on any change to the share_price
and/or quantity
siteEditor.dependent( ['dm_holdings.quantity', 'dm_holdings.price'], function ( val, data, callback ) {
var stock_id = Number(siteEditor.field('dm_holdings.stock_id').val());
var quantity = Number(siteEditor.field('dm_holdings.quantity').val());
var total;
share_price = Number(siteEditor.field('dm_holdings.price').val());
total = share_price * quantity / 100;
siteEditor.set('dm_holdings.total', (total));
},{event: 'change keyup'} );
I need to allow entry of the total
resulting in the quantity
being evaluated based upon the share_price
Is there a way to check the current field being edited and then set values dependent upon the field being edited i.e.
if editing quantity field, assign total (total = share_price * quantity / 100;)
if editing total field, assign quantity (quantity = 100 * total / share_price;)
this is my attempt at the code change to the above shown below, it may require the logic above to work !!
siteEditor.dependent( ['dm_holdings.quantity', 'dm_holdings.price', 'dm_holdings.total'], function ( val, data, callback ) {
var stock_id = Number(siteEditor.field('dm_holdings.stock_id').val());
var quantity = Number(siteEditor.field('dm_holdings.quantity').val());
var total = Number(siteEditor.field('dm_holdings.total').val());
share_price = Number(siteEditor.field('dm_holdings.price').val());
quantity = 100 * total / share_price;
total = share_price * quantity / 100;
siteEditor.set('dm_holdings.quantity', (quantity));
siteEditor.set('dm_holdings.total', (total));
},{event: 'change keyup'} );
the above code prevents me entering the total
, deleting the characters as they are entered, and thus the quantity
is not evaluated based upon the value of total
and ```share_price``
any help appreciated.
as usual I can provide access to my system to test if required,
Many Thanks
Colin
This question has accepted answers - jump to:
Answers
I don't think you can easily tell with the array, as the field isn't named. You could match the value to the field, but if two fields have the same value, this would become ambiguous.
If you do something like this : http://live.datatables.net/dequdenu/1/edit : i.e. have a dependent entry for each field, which then calls a common function, that would provide a working solution.
Colin
Hi Colin
Many thanks for the quick response and solution, that looks like exactly what I need, great news. I will code it later today.
Best Regards
Colin
Hi Colin
I have written the code based upon your suggestion shown below, but I have some issues.
the function
doItT
calculates thetotal
based upon theprice
and/orquantity
the function
doItQ
calculates thequantity
based upon theprice
and/ortotal
If I comment out section Q above the code works except the Quantity is displayed as NaN when the Editor form is first displayed, but once I add portfolio and symbol the quantity is set to 0 shown below
Other than the above issue the function
doItT
works calculatingquantity
If I comment out section T above the section Q code to calculate the quantity code works 100%.
My 2nd main problem is that when the section Q and section T are uncommented, I get the following errors
I have spent many hours yesterday trying to fix the problem to no avail, so any help would be great.
Many thanks Colin
There's a lot going on there. Would you be able to update my example above to mimic that behaviour, please - it would help to understand the flow of the calculations.
Colin
Hi colin
Good advice, I will try and replicate the problem using your example and get back to you later.
Thanks Colin
Hi colin
I have created some code as suggested
http://live.datatables.net/bewicawo/1/edit
to calculate the
age
dependent uponsalary
andexperience
and/or
calculate the
salary
dependent uponage
andexperience
if both function calls are uncommented it runs the process endlessly.
if either one is uncommented it works albeit for one dependency
Many Thanks
Colin
Hi colin
please ignore that last call entry, I may have made some progress, will update you soon.
thanks Colin
Hi colin
I have created another code snippet relating exactly to my problem, can you look at this one for me, please.
http://live.datatables.net/zidefaca/1/edit
A quick explanation, if the price is changed it sets both the
quantity
andtotal
to 0, as requiredIf you change the
quantity
it should set thetotal
asIf you change the
total
it should set thequantity
asIf I uncomment all functions it fails, causing an endless loop, but each function works as expected individually, except the default
0
when changing theprice
assigned toquantity
andtotal
remains on screen when entering the value.any help much appreciated.
Thanks Colin
Here we go: http://live.datatables.net/tohegico/1/edit .
It isn't possible to do with
dependent()
unfortunately as it doesn't provide access to the original event (I'll change that). However, we can replicate whatdependent()
does with a simpleeditor.field().input().on('change', ...)
.The reason we need the original event is that when Editor triggers the
change
event it will add a data parameter with{editor:true}
(I've just checked for the data parameter at all in the example there). That allows us to tell if the change event is coming from Editor or the end user and take appropriate action.Once thing I didn't understand about your example was the
set(0)
when changing the price. I've commented that out, because it was triggering as soon as you tab into that field, but you might need to revert the logic for your use case.Allan
Hi Allan
That works exactly as required great news, I was aware of some limitations in the dependent API and alternative methods to achieve the same objective which you had pointed out to me in a previous call.
I wasn't sure if I needed to set the total and quantity to 0 on a price change because which field would it change in response to a change in the price, as it could only change one not both, but the way you have coded it works without needing to set those values to zero.
Many thanks again.
Best Colin