Computed Values for Table in Editor

Computed Values for Table in Editor

belubelu Posts: 36Questions: 14Answers: 0

He there,

I am using Editor and would like to add a computed value to the JSON string for being displayed in the table. It just needs to be displayed in the table - there is no need for saving it in the Database (there is no corresponding field in the Database ;-)).

How can I add such a computed value to the JSON string with Editor?

Thanks in advance!

Answers

  • colincolin Posts: 7,698Questions: 0Answers: 1,298

    When you say "add a computed value to the JSON" - is this coming from the server? Or going to the server? Or just something you want to calculate on the browser and display there?

    Colin

  • belubelu Posts: 36Questions: 14Answers: 0

    I mean coming from the server to be displayed in the table. Not calculated in the browser - it is a sum calculated in the .php.

  • colincolin Posts: 7,698Questions: 0Answers: 1,298

    I think I understand. If you want to just show it in the table (as you there's no point in the edit form as it's been calculated externally) then you can do something like this - here the updated_date isn't shown in the form.

    If you want it in the form but readonly, you can use readonly.

    Hope that helps,

    Colin

  • belubelu Posts: 36Questions: 14Answers: 0

    Hi Colin,

    the thing is, that there is no column for this value in the table. The Value is generated and not stored in the Database. So in other words I would like to add a column to the shown DataTable with this generated value - but there is no column with this value in the mysql table. Hope that describes it better.

  • colincolin Posts: 7,698Questions: 0Answers: 1,298

    Yep, so that first example I posted above would do that. Get the server to return the value in the JSON, define the column in the table like the others, but don't mention it in the Editor initialisation. That way the value will be displayed, but never returned to the server.

    Colin

  • belubelu Posts: 36Questions: 14Answers: 0

    Hi Colin, thanks for your answer!

    unfortunately it does not work like in the example, because there is no column in the with the name "locations" in the table - because it is a generated (computed) value, that does not exist in the table - so the following error:
    DataTables warning: table id=table - An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'locations' in 'field list'

    I try to explain it more clearly:
    I would like to add a value to the DataTable, that has no own column in the mysql table. So this value is calculated in the server script with a function, but there is no corresponding column in the sql - table. How can I get such a computed value into my JSON string with editor?

    Thx!

  • allanallan Posts: 51,272Questions: 1Answers: 7,724 Site admin

    Rather than adding it to the JSON, I'd actually recommend you calculate the value client-side. That can be done with a renderer - see the "computed values" section of the documentation here.

    Allan

  • belubelu Posts: 36Questions: 14Answers: 0

    I am using some kind of a "hack" to solve my problem now:
    If anyone has the same problem, here is a solution that works fine for me.

    I created a "dummy" - column in the database table and use the
    -) $editor->getFormatter() - function to calculate the value.
    -) To avoid the value being written to the dummy_column in the table, I use $editor->set(Field::setNone)

Sign In or Register to comment.