Editor set "computed" column

Editor set "computed" column

joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2
edited March 2018 in Editor

Hey folks. I have a "computed" column in my table which is computed from the values of two actual boolean columns in the DB, which is defined something like the following. Is there some way to use the editor to edit this computed column, by selecting a value from a drop down, and then set the source columns (Hidden and Approved) appropriately? I realize this is kind of a strange case. Thanks. If it helps, I'm using the C# DataTables editor on the back end to handle requests.

                {
                    data: null,
                    render: function (data, type, row, meta) {
                        if (data["Hidden"] == true) {
                            return "Hidden";
                        }
                        else if (data["Approved"] == true) {
                            return "Active";
                        }
                        else {
                            return "Pending";
                        }
                    } 
                }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Do you want to store the computed value in the database? If so, a formatter is probably the best way to go.

    Allan

  • joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2

    Allan, thanks. That looks good. I'll try it. I should probably just create a new column to hold this status and run a DB script to set it based on the other columns and begin using the new column instead. It has the additional complication that they want one of the statuses to be based on a separate date column. So, when a row is "Active" but has a modified date > 2 years ago, make its status "Review". But this may be a read-only status that can just be set in a render function or something or using a Formatter like you said. We'll see.

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    If you don't want it stored in the database (which is absolutely fine if the value can be computed) you could use colums.render as you have done to display it in the DataTable. Is the problem with that, that you want to also show it as plain text in the Editor form?

    Allan

  • joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2

    Correct. I wanted to show it as text in the Editor form as well, and have a drop-down to select the value.
    I ended up creating a new integer column in the table which is a foreign key into a code table which contains the descriptions "Hidden", "Pending", "Active" (may also contain "Review" at some point). I'm using LeftJoin to join to that table, and in the editor, I can display it as a Select with the different options, and I use the value from the joined table to give the text for the table. It works fine. The one complication is, I'd like to rely on a separate column, which is a DateTime, to determine if "Active" rows should appear as "Review". This happens if this date column is older than two years ago. Perhaps I can use a Formatter for this, so when it reads in, it bases its value on the column itself and also the date, and when it writes back out, it converts "Review" to just be "Active". Something like this. It's a strange requirement.

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    One option to have it displayed in the Editor form is to use a plug-in such as the display only one and then compute the value when any dependencies change and update that field. dependent() can be used for that.

    The upside is that you don't need to mess around with storing it in the database at all.

    The downside is that you need to have the logic for the calculation in the column renderer and in the field view (although the basic logic could be shared between them with a function).

    Does that sound like a valid approach for that you are looking for?

    Allan

  • joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2

    Hi Allan, I think I have it working well enough now. It uses a formatter to display the items with Active status as having Review status based on their modified date. It worked fine to store the status column in the database. I'm using it as a replacement for the other columns. Thanks for the suggestions!

This discussion has been closed.