yii2-bootstrap
yii2-bootstrap copied to clipboard
Allow rendering of tab content via Closure
What steps will reproduce the problem?
Use a tab widget, inside use any widget that uses javascript and targets the widgets' id. Then hide the specific tab that has the javascript widget.
What's expected?
The tab should be hidden, no errors.
What do you get instead?
Errors. Since the tabs does get render, javascript gets registered but the html does not get outputted. This means the JS will not find elements it is expecting.
This is not so much a bug as a limit of the implementation.
My proposal is to allow rendering of tab widgets via a Closure:
echo Tabs::widget([
'items' => [
[
'visible' => false,
'content' => function() {
}
]
]);
A work around currently is to use an object that implements __toString() and lazily renders the tab only when __toString() gets called.
How to reproduce the issue exactly?
Suppose this is my tab view:
<?php
$this->registerScript("script-for-test", "$('#test').animate(....);");
// Do some slow advanced processing as well, like generate a 10000 element table.
sleep(5);
?><div id="test"></div>
It is rendered in the tab widget via:
echo Tabs::widget([
'items' => [
[
'visible' => false,
'content' => $this->render('test')
]
]);
Now the call to render will register the javascript but the target element #test will not exist so javascript will throw an exception.
From a speed perspective, not rendering a tab when it's not visible makes sense as well!