Specify option label based on value

Hi Folks,

I’m trying to determine the best method for specifying the label of an option in a grid’s select box before data is returned to the client. Is there any way to change the label of an option? Would this be something better done on the client side?

I presently have an OptionsConnector loading some specific options into the field, and the value coming from the GridConnector’s render_sql won’t be in the list of previously loaded options, so I want to specify that value’s label.

Is there any way to do this?

Thanks!

Andy

There are 2 main strategies

a) load options through options connector

You are using key field in the grid’s connector and use set_options to define list of options.
In such case grid will work with ids internaly, and convert them to text during rendering only.
You can use beforeRender of options connector to pre-format labels in necessary way

b) do not use options connector

In such case grid may contain key or directly labels.
You can form list of options by any custom logic, store in array and assign it to column through set_options command.

I’m presently using the OptionsConnector to load some of the value,label pairs. I had intended to set the label for the key field directly, but I can’t seem to figure out how to access that from either the client or server side. Here’s a little more info about what I need.

The OptionsConnector is going to load the same values for each row’s select box, which is a list of “default” profiles that can be selected as options. The GridConnector is going to load from the same table the id of the current profile being used by the entity in question for each row. Sometimes, it will indeed be one of the default profiles, in which case, the resultant row looks very nice in the grid, showing the correct value,label pair. However, if the profile id for the row isn’t one of the default profiles, I want it to simply display something akin to “Custom Profile” as the label for that profile id.

All non-default profile id’s will be different, so I had wanted a way to just set the label in a “beforeRender” event… but I can’t see how to access that data in order to set it.

I hope that makes a some sense. Do you have any suggestions?

Thanks in advance,

Andy

Now its more clear, thank for provided details.
You really can set the custom label instead of profile id in beforeRender of grid, due to grid’s mechanic, client side grid will show such label when it will not be able to locate related record in the select list.

$grid->event->attach("beforeRender", function($action){ if (some_check) $action->set_value("profile_id", "Custom Profile"); });
The some_check in above code may be a problematic piece, as at moment of beforeRender call for the grid connector, list of options is not fetched yet. So it is not possible is current item in list or not.

You may fallback to custom sql query for options selection, which can be called before connector render, so you will have list of options to check against and you can use already fetched array of options records for the set_options command.