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