Questions on OptionField and accessing Table column data

Hello All,
Some more basic questions and I can’t see anything in the docs or SDK so I assume the lack of any info means it doesn’t exist yet or isn’t possible due to the (almost) stateless nature of VIKTOR (I’ve got no CS background so just repeating what ive read in other questions :smiley: ).

Back to my questions:

  1. OptionField - Is there a way to run a method “on select” of an option? My use case is i have loaded a file containing geotech test data. I have extracted a list of soil samples which had a specific test which populates the optionfield. Then i want to run a method to do something with that selection rather than clicking a button to run the method.
  2. Table - Is there a way to reference a list of values from a column in the table? Obviously i can loop through the table dictionaries for each row, but I wasn’t sure if i was missing something obvious like a method.

Thanks in advance :+1:

Hi again Ian,

  1. This depends on what you would like this method to do exactly. If you would like to use the chosen option in the generation of a certain View you could choose to have this view have a duration_guess of 3 or less which will cause the view to regenerate every time something changes in the Parametrization. If you’d like to do something else outside of the view you could do so within a callback function (described in the docs here: User input, fields and buttons - Options & selections | VIKTOR Documentation).
    In short: please explain a little more in detail what you would like to do with the choice a user makes in the OptionField.

  2. There is no specific method to do this. The output of the Table is simply a list of dictionaries. So I would say the most direct way of accessing all data in a single column would be to go about it something like this:

class Parametrization(ViktorParametrization):
    table = Table('Input table')
    table.name = TextField('Planet')
    table.period = NumberField('Orbital period', suffix='years')
    table.eccentricity = NumberField('Orbital eccentricity', num_decimals=3)

class Controller(ViktorController):
    label = 'My Entity Type'
    parametrization = Parametrization

    @staticmethod
    def some_method(params, **kwargs):
        name_column = [row["name"] for row in params.table]
        period_column = [row["period'] for row in params.table]
        ...

Hope that helps! if you have more questions, let me know.

Thanks for the reply Daniel.
On point 1, I was wanting a Table to be populated with data depending on the option chosen. The table is an input table not a view, currently I use a SetParamsButton which need to be manually clicked.
I was hoping for something along the lines of an argument for on_selection which take a string reference to a function. Currently when an option is selected there is the “reload” (sorry forget the term from the docs) which as you say refreshes the views but needs to be repeated if i which to set parameters etc. Hopefully my explanation makes sense.

For 2 I had thought as much and was using the approach you recommended. It seemed possibly a common enough need to access a column that there was a method I’d missed in the documentation.

Again thank you very much Daniel for taking the time to respond.

Hi Ian,

Thanks for the further explanation. On that point of filling a table in the Parametrization based on the choice in an OptionField, That sounds an awful lot like having dynamic defaults on fields. Which is an often discussed topic in our product (see Function lookup - set default values optionfield or Change table content when changing dropdown menu selection for example). However as of now, this is not an option.
One option (which you’ve already figured out) is obviously to use the SetParamsButton. Depending on your specific use-case another could be to prepare several different tables in advance and have them all at the ready in you Parametrization (mind you, if the tables are very large this could lead to slow params calls) and adjusting the visible flag of those fields based on the choice made in the aforementioned OptionField.

Would that work in your case?