dependent upon 4 variables

dependent upon 4 variables

cpshartcpshart Posts: 243Questions: 48Answers: 5

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

  • colincolin Posts: 12,939Questions: 0Answers: 2,204
    Answer ✓

    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

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    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

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    Hi Colin

    I have written the code based upon your suggestion shown below, but I have some issues.


    function doItQ(field, val, data, callback) { console.log('806:' + field + ': ' + val); if (field === 'dm_holdings.quantity') { var quantity = Number(siteEditor.field('dm_holdings.quantity').val()); share_price = Number(siteEditor.field('dm_holdings.price').val()); var total = share_price * quantity / 100; siteEditor.set('dm_holdings.total', (total)); } callback(true); } function doItT(field, val, data, callback) { console.log('817:' + field + ': ' + val); if (field === 'dm_holdings.total') { var total = Number(siteEditor.field('dm_holdings.total').val()); share_price = Number(siteEditor.field('dm_holdings.price').val()); var quantity = 100 * total / share_price; siteEditor.set('dm_holdings.quantity', (quantity)); } callback(true); } /* section Q */ siteEditor.dependent(['dm_holdings.quantity', 'dm_holdings.price'], function(val, data, callback) { doItQ('dm_holdings.quantity', val, data, callback) },{event: 'change keyup'} ); /* section T */ siteEditor.dependent(['dm_holdings.total', 'dm_holdings.price'], function(val, data, callback) { doItT('dm_holdings.total', val, data, callback) },{event: 'change keyup'} );

    the function doItT calculates the total based upon the price and/or quantity

    the function doItQ calculates the quantity based upon the price and/or total

    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 calculating quantity

    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

  • colincolin Posts: 12,939Questions: 0Answers: 2,204
    Answer ✓

    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

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    Hi colin

    Good advice, I will try and replicate the problem using your example and get back to you later.

    Thanks Colin

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    Hi colin

    I have created some code as suggested

    http://live.datatables.net/bewicawo/1/edit

    to calculate the age dependent upon salary and experience

    and/or

    calculate the salary dependent upon age and experience

    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

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    Hi colin

    please ignore that last call entry, I may have made some progress, will update you soon.

    thanks Colin

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    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 and total to 0, as required

    If you change the quantity it should set the total as

    total = quantity x price / 100
    

    If you change the total it should set the quantity as

    quantity = total x 100 / price
    

    If I uncomment all functions it fails, causing an endless loop, but each function works as expected individually, except the default 0 when changing the price assigned to quantity and total remains on screen when entering the value.

    any help much appreciated.

    Thanks Colin

  • allanallan Posts: 54,909Questions: 1Answers: 8,606 Site admin
    Answer ✓

    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 what dependent() does with a simple editor.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

  • cpshartcpshart Posts: 243Questions: 48Answers: 5

    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

Sign In or Register to comment.