Edit server response data on postGet

Edit server response data on postGet

andrew21111andrew21111 Posts: 2Questions: 1Answers: 0

I need to populate a tables with calculated properties.
The dataSource and the calculated properties source are different, so that's what I tried:

I tried to modify the data returned from the php script, the data are correctly added but client-side are not returned

 ->on( 'postGet', function ( $e, $data, $id ){

        foreach ($data as $key => $value ){
            $id_pdr = $value["pdr"]["id_pdr"];
            $khw_price = $value["pdr"]["kwh_price"];
            $result = getCalculatedProperty($id_pdr, $khw_price);
            $data[$key]['calculated'] = $result;
        }
        

    })
    ->process( $_POST )
    ->json();

Otherwise, instead of use the function getCalculatedProperty, I can retrieve the data with an ajax call client-side and get JSON back.
I tried to update table with the new data, without success.

How can I do ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Try:

    ->on( 'postGet', function ( $e, &$data, $id ){
    

    PHP uses a "copy on write" method for variables, so if you were to write to $data it would copy the old array and create a new one with the modification, which would be lost when the function ends.

    Using the & will tell PHP that you want it to use the reference to the original $data and allow you to modify it.

    Allan

  • andrew21111andrew21111 Posts: 2Questions: 1Answers: 0

    You completely saved my day !
    Changing the signature in this way

    ->on( 'postGet', function ( $e, &$data, $id )
    

    Did the trick, Thank you so much !

This discussion has been closed.