How to : Computed field

How to : Computed field

VAILLY OlivierVAILLY Olivier Posts: 19Questions: 0Answers: 0

Hi everybody,

  • I've a dataTable mapped on a postgresql table, with 2 columns defined (i.e. Product_qty and Product_price)
  • i've an Editor mapped on the dataTable object and it works fine

Now i need to add a third column which will be a computed column with Product_qty * Product_price formula
and when user change (inline) a value in, say, Product_price, the formula is recomputed !

I'm searching for such function and find no solution !

Coould someone help me ?

Thanks
Oliver

Replies

  • allanallan Posts: 61,893Questions: 1Answers: 10,145 Site admin

    Hi Oliver,

    The columns.render option is the way to do this. For example you might use:

    {
      data: null,
      render: function ( data, type, row ) {
        return row.Product_qty * row.Product_price;
      }
    }
    

    Allan

  • VAILLY OlivierVAILLY Olivier Posts: 19Questions: 0Answers: 0

    Hi Allan, thanks for your response

    After your confirmation ( i was near the good result ....) of the code to use,
    i retry and i find my mistake !

    my syntax to access field in the render method was not good, because of the join of multiple tables in datasource !

    for those who have same issue :

    render : function (data, type, row) {
    return row.my_join_table.my_field * row.same_or_other_table.other_field;
    }

    now it works fine !

    thanks for all :)

  • VAILLY OlivierVAILLY Olivier Posts: 19Questions: 0Answers: 0

    Come back !

    Now my field is well computed but my problem is a little bit more complicated that i thought ..... :

    • the value in Column "CA" must be computed with the Formula Product_qty * Product_price -> ok with render option
    • this value must be sended to database to be store in the same field that is displayed in table .... :(

    How to do this ?
    Thanks

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    this value must be sended to database to be store in the same field that is displayed in table .... :(
    

    Pretty sure support for datatables ends after the data displays successfully, But what you need to do is call the cell contents from the DOM and then send it to the database using php or whatever server side language you're using.

    Easier said than done.

  • allanallan Posts: 61,893Questions: 1Answers: 10,145 Site admin
    edited November 2015

    I don't quite understand why you want to store a value in the database that is computed on the client-side. Surely you could just compute it in the database as well? In fact, if it is a computed value, why store it at all?

    Allan

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2

    Good point, Why aren't you storing it on the server before sending it to the client?

    Never trust the client. Never, ever.

  • VAILLY OlivierVAILLY Olivier Posts: 19Questions: 0Answers: 0

    Hi,

    I want to store this computed value because it will be used by others applications in other systems !!!!!!!

  • VAILLY OlivierVAILLY Olivier Posts: 19Questions: 0Answers: 0

    heuu ... i'm lost at this point :(

    to resume :

    • the displayed computed value will come from the database
    • editor send only the 2 source values
    • the computing is done server side and store before the displaying cycle restart .....

    But i don't see where put the code for computing ...

    in php file called by DataTable ?

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    Store the computed value server side for use by other applications before you even do anything with datatables.

    You shouldn't be computing sourced data on the client and then sending it back to the server, that is double handling.

    You shouldn't be relying on any response from the users client because it can be spoofed to drop all your tables etc unless you sanitize properly.

    Your problem at this point isn't datatables related and this could probably be closed.

  • VAILLY OlivierVAILLY Olivier Posts: 19Questions: 0Answers: 0

    Ok Samuel i agree but the script where i should compute the value rely on DataTable no ?
    it is called by DataTable and i don't see where place my code because ( and thanks to that) DataTable "machinery" mask all sql stuff !

  • allanallan Posts: 61,893Questions: 1Answers: 10,145 Site admin

    I'd say don't store it at all - just compute it. You've asked DataTables to compute it - why not just compute it else where as well? I don't understand that point.

    However, if you want to store the computed value in the database, I would suggest you use a server-side event to do so. Have a function that computes the value and uses that to store.

    Allan

This discussion has been closed.