Hi Ian,
There is indeed no direct way of getting the tab index of a form.
Using the solutionModel like David is suggesting works but when you add (or remove) tabs at runtime using code then this method won’t give you the desired result since those tabs are not part of the blueprint of your form. The solutionModel can only access the blueprint and doesn’t see runtime objects.
So that leaves you with 2 other ways of getting the tabindex of your form.
- using controller.getFormContext()
- using .getTabFormNameAt(index)
So lets look at option 1.
When using the controller.getFormContext function you get a DataSet with your form and all the parent forms (all the way to the main form of your window). And this dataset also has the tabIndex of the tabpanel your form lives in so this is a very easy way to get this information.
So you could use the following (global) method like so:
var _nTabIndex = globals.getTabIndexOfParentForm(controller.getName())
/**
* @param {String} _sForm the form name you are looking for
* @return {Number}
*/
function getTabIndexOfParentForm(_sForm) {
// dataset columns: [containername(1),formname(2),tabpanel or beanname(3),tabname(4),tabindex(5)]
// dataset rows: mainform(1) -> parent(2) -> current form(3) (when 3 forms deep)
/** @type {JSDataSet} */
var _dsContext = forms[_sForm].controller.getFormContext(),
_nParentIndex = _dsContext.getMaxRowIndex() - 1,
_nIndex = 0;
if (_dsContext.getMaxRowIndex() > 1) {
// we're in a tabpanel or a bean (like a splitpane bean)
_nIndex = (_dsContext.getValue(_nParentIndex, 5) != null ? _dsContext.getValue(_nParentIndex, 5) + 1 : 0);
}
// returns 0 when no form matches or not in a tabpanel
return _nIndex;
}
However I did find that when you use a tabpanel as a splitpane (in Webclient in my case) anywhere in your context the moment you call controller.getFormContext() you get the following error:
com.servoy.j2db.server.headlessclient.dataui.WebSplitPane cannot be cast to com.servoy.j2db.server.headlessclient.dataui.WebAccordionPanel
Wrapped java.lang.ClassCastException: com.servoy.j2db.server.headlessclient.dataui.WebSplitPane cannot be cast to com.servoy.j2db.server.headlessclient.dataui.WebAccordionPanel
This seems to me that this is a bug and filed it as such in Servoy’s support system (SVY-3626).
Option 2 would be to iterate over the tabs in your tabpanel and see if the tab contains the form you are looking for.
So you could use the following (global) method like so:
var _nTabIndex = globals.getTabIndexOfForm(forms.myParentForm.elements.parentTabPanel, controller.getName())
/**
* @param {RuntimeTabPanel} _oTabpanel tabpanel of the parent form
* @param {String} _sForm the form name you are looking for
* @return {Number}
*/
function getTabIndexOfForm(_oTabpanel, _sForm) {
var _nMax = _oTabpanel.getMaxTabIndex(),
_nIndex = 0;
for (var i = 1; i <= _nMax; i++) {
if (_oTabpanel.getTabFormNameAt(i) == _sForm) {
_nIndex = i;
break;
}
}
// returns 0 when no form matches
return _nIndex;
}
Hope this helps.