dependent being called on edit before a selection made

dependent being called on edit before a selection made

cpshartcpshart Posts: 246Questions: 49Answers: 5

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi

Using datatables Editor, I have a dependent function which gets the share price of a stock on selecting the stock, it passes the stock_id to the server script, which creates a JSON file of the share price which is returned to the client and populated in the editor field correctly.

the price is shown below as 14394

when I EDIT the line, the price is updated to 14435, because the dm_holdings.stock_id value is passed to the server file and the JSON returned contains 14435 is returned from dm_stocks.price for the above dm_holdings.stock_id and editor dm_holdings.price field is updated.

I only want the price changed in the editor if the user makes a selection using the Symbol select field. So how can I prevent the dependent function being called when the EDIT button is pressed, and not unless an alternative symbol selection is made.

client script extract

    siteEditor.dependent( 'dm_holdings.stock_id', function ( val, data, callback ) {
    $.ajax( {
    url: "../../" + EDITOR_DIR + "/controllers/dview-get_stock_column-1.00.php",
    datatype: 'html',
    // pass stock_id value to server php script
    data: { "stock_id": val },
    success: function (data) {  
        if (data) {
    try {
            
        if ( data.length > 1 ) {
        myJSON = JSON.parse(data);
        share_closeyest = myJSON[0].price;
        share_currency = myJSON[0].currency;
            
        switch(share_currency) {
                              case 'GBP':
                                // change the price to pence => price * 100
                                share_closeyest =  share_closeyest * 100;
                                break;
                              default:
                                // price is quoted in pence so no change
                                break;                                   
                             }  
        if (Number(siteEditor.field('dm_holdings.price').val()) === 0) {            
                siteEditor.field("dm_holdings.price").set(share_closeyest);
            }   
        }
    }
            
    catch (error) {
        console.error("Crashing while parsing ");
        throw error;
    }                   
        }
        else if (data.trim()) {
        }
    },  
    }); 
    }); 

server script

```
require( '../../wp-blog-header.php' );

global $current_user;
wp_get_current_user();

// DataTables PHP library
include( "../lib/DataTables.php" );

$stock_id = $_GET['stock_id']; // manually setting variable for testing
$stock_array = [];
$json_array = array();

// check if variable is NOT blank pass JSON back to client
if ($stock_id <> "") {

try {
$pdo = new PDO(strtolower($sql_details['type']) . ":host=" . $sql_details['host'] . ";dbname=" . $sql_details['db'], $sql_details['user'], $sql_details['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}

$result = $pdo->query("SELECT id, symbol, name, price, currency FROM dm_stocks WHERE id = $stock_id");
foreach ($result as $row) {
}
array_push( $stock_array, array( 'symbol'=>$row['symbol'], 'price'=>$row['price'], 'currency'=>$row['currency'] ) );

echo json_encode($stock_array);
}

<?php > ``` JSON file ``` [{"symbol":"VRXXB","price":"14435","currency":"GBX"}] ``` ?>

I hope this makes sense.

Many Thanks

Colin

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    What you could do, I believe, is have two dependent fields. The first, for the Symbol select, would then create the second dependency (if it hasn't done so already). Would that do it?

    Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi colin

    Thanks for the quick response, I will investigate your suggestion tonight and get back to you.

    Best Colin

This discussion has been closed.