Turns out the REAL underlying problem is how I was trying to remove fields (columns) from the cache.
view.Cache.Fields is a PXFieldCollection, which inherits List. This means that Clear() is an inherited method. The PXFieldCollection defines an override for Add(item) and Remove(item) but not Clear(). I was using Clear() in an effort to remove all the fields I had previously added, when that’s not at all what it does. it was having an effect, as the columns disappeared, but view.Cache.Fields.Contains(item) still returned ‘true’. Instead, I had to use view.Cache.Remove(item) on each field. Now the fields are correctly being removed, then correctly re-added, and no longer disappearing.
@Dmitrii Naumov - I recommend an override for Clear() be added or somehow indicate this is not what the method does
I believe I’ve narrowed down the issue to the dynamic FieldSelecting event handler. It seems to only run one time after being initialized in an action.
Edit: I think this is due to the fact that the fields are being removed from View.Cache.Fields for some reason. I really wish there was documentation about how this works.
dynamic columns added when the graph is initialized remain until they are removed
dynamic columns added pretty much any other time will be removed automatically (I think during RowSelected
If you need to add truly dynamic columns (ie: they may be removed / readded during the lifecycle of a graph), they need to be re-added in a RowSelected event handler. However, simply storing a list of columns was not enough, as the list would be emptied for no apparent reason right before the RowSelected event handler ran. I had to add a virtual view to store the field definitions, which is not emptied until I ask it to be.
Additionally, though the columns show correctly when run the first time, subsequent runs required me to set ‘generateColumnsAfterSelect: true’ in the gridConfig.
Turns out the REAL underlying problem is how I was trying to remove fields (columns) from the cache.
view.Cache.Fields is a PXFieldCollection, which inherits List. This means that Clear() is an inherited method. The PXFieldCollection defines an override for Add(item) and Remove(item) but not Clear(). I was using Clear() in an effort to remove all the fields I had previously added, when that’s not at all what it does. it was having an effect, as the columns disappeared, but view.Cache.Fields.Contains(item) still returned ‘true’. Instead, I had to use view.Cache.Remove(item) on each field. Now the fields are correctly being removed, then correctly re-added, and no longer disappearing.
@Dmitrii Naumov - I recommend an override for Clear() be added or somehow indicate this is not what the method does