K2 Smartforms Only Execute Action if Sub View/Form Not Open

1.2k Views Asked by At

I have a K2 SmartForm (v4.7) with a list view, and a timer that refreshes the data in that view. However when a subform (or subview) is open on top of the base form and the timer ticks, the "loading" indicator and white box covers up the entry form while the list is being refreshed.

Is there a way to either have the loading panel display under the subform, or a way to apply a rule to the timer's tick event to check if a subform is open?

I realize it's possible to have a rule stop the timer when the subform opens, and start it again when it closes, but there are multiple subforms and views that can be displayed in this app and it would be much easier to simply have the timer tick event check if anything is open.

2

There are 2 best solutions below

0
On

You can set rules on subview and subform events such as Initialize and also when they close. You could do your tracking with those. But you'll still have to do it for every subview/subform.

Other than that, I think you'd have to do some JavaScript magic.

1
On

Ok, so here's some JavaScript magic, @nigel-whatling:

I've added a checkbox control to the form called ckIsSubOpen. Then on the timer's tick event I'm passing some javascript into a data label (literal) which will call a function (set on the view initialized event) to set the control value. Then the next action in the rule checks if that checkbox is checked, and if its value is false, will run the refresh command.

This got tricky because you can't simply manipulate DOM elements and have K2 pick up the value change. According to this thread there's an XML document stored as a page variable, along with a handy function to manipulate the values:

function customUpdateSfControl(controlName, value) {
    var myTextBoxXPath = 'Controllers/Controller/Controls/Control[@Name=\"' + controlName + '\"]';
    var windowToUse = window;
    if (!checkExists(windowToUse.viewControllerDefinition)) {
        windowToUse.viewControllerDefinition = $xml(windowToUse.__runtimeControllersDefinition);
    }
    var myControl = windowToUse.$sn(windowToUse.viewControllerDefinition, myTextBoxXPath);
    var controlInfoObj = new windowToUse.PopulateObject(null, value, myControl.getAttribute('ID'));
    windowToUse.executeControlFunction(myControl, 'SetValue', controlInfoObj);
    windowToUse.raiseEvent(myControl.getAttribute('ID'), 'Control', 'OnChange');
}

The script which sets the field value checks for any divs that have the sub-form or sub-view class:

customUpdateSfControl('ckIsSubOpen', ($('div.sub-view, div.sub-form').length > 0));

This seems to work for my needs.