YII anchor doesnt work?

Mark Lewis
Mark Lewis used Ask the Experts™
on
Looking thru YII documentation I confirmed that to create a link using an anchor this is the correct syntax:
Yii::app()->createUrl("box/update", array("id" => $id, '#' => 'second-tab'))

Open in new window


The corresponding view for the action "box/update" is made like this:
$this->widget(
    'bootstrap.widgets.TbTabs',
    array(
        'type' => 'tabs',
        'placement' => 'top',
        'tabs' => array(
            array(
                'id' => 'first-tab',
                'label' => Yii::t('app', 'First Tab'),
                'content' => $this->renderPartial('_memberFirstTab', array('model'=>$model, 'form'=>$form), true),
                'active' => true
            ),
            array(
                'id' => 'second-tab',
                'label' => Yii::t('app', 'Second Tab'),
                'content' => $this->renderPartial('_memberSecondTab', array('model'=>$model, 'form'=>$form), true),
            ),
        ),
    )
);

Open in new window


The result is that if I am within the box/update action I can move back and forth between the two tabs but if I call it from outside (YII or not) I cannot link directly to something different from the first tab.

Where is the error I cannot see
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
No one on this whole site knows the answer to this???

Commented:
I don't know Yii very well, but I can tell you that there is no error. The TbTabs widget is a client-side Javascript-triggered widget. It doesn't automatically read the incoming tab ID and select it.

If you want to pre-select a specific tab based on a URL, you'd need to pass the tab ID as a parameter in the URL itself (NOT as a page anchor like page#tab_id). Then have your view change the 'active' setting based on that parameter, so if second-tab was indicated as the desired pre-selected tab in the URL, you'd move "active" => true to the second-tab definition.

Commented:
My comment apparently got posted after yours, but bear in mind that not everyone sees every question. This didn't show up on my radar at all until someone mentioned it to me as an unanswered question.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
This is not a rocket science question, I am simply asking about using anchors in yii. How could it be that not one single person on this site could offer something useful?

Commented:
This isn't about a lack of knowledge - it's about the capabilities of the widget. The widget itself does not support what you're asking for. Sorry it's not the answer you want, but it is what it is.

Author

Commented:
Not sure what you mean by referencing a widget. Yii is not a widget.
Commented:
No, I'm not saying Yii is the widget. Yii is the framework. On top of Yii is Yiistrap, which defines the TtTabs widget:

$this->widget(
    'bootstrap.widgets.TbTabs',  <<<<<<<<<<<<<<<<<<<<<<<<
    array(
        'type' => 'tabs',

Open in new window


The documentation for that widget is here:
http://www.getyiistrap.com/site/widgets

So your PHP  / Yii code generates the usage of the TbTabs widget, which is responsible for rendering those tabs on the final page.

When you click on each tab, TbTabs uses anchors at that point to "navigate" between each tab without refreshing the whole page. However, it does not try to read the anchor when the page first loads, which is why it's not changing the page automatically when you pass in an anchor at the beginning. The initially-selected tab is defined by the "active" flag in your PHP code, which is rendered server-side.

 array(
                'id' => 'first-tab',
                'label' => Yii::t('app', 'First Tab'),
                'content' => $this->renderPartial('_memberFirstTab', array('model'=>$model, 'form'=>$form), true),
                'active' => true   <<<<<<<<<<<<<<<<<<<<<<<
            ),

Open in new window


So per my very first comment, if you want to change which tab is initially shown, then you have to use PHP to look at the incoming request (probably pass in a value in the query string) and then adjust which tab has the "active" flag.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial