How can redirect be used to go to a particular form's tab?


#1

Under certain circumstances I would like to redirect the user to the second tab on a page, that is, the ‘account’ tab.
I have tried:

	$this->redirect('mycontroller/myaction#account");

	$this->redirect(array(
		'controller' => 'mycontroller',
		'action' => 'myaction',
		'#' => 'account'
	));

	$this->redirect(array(
		'controller' => 'mycontroller',
		'action' => 'myaction#account'
	));

	$this->redirect(array(
		'action' => 'myaction#account'
	));

	$this->redirect(array(
		'action' => 'myaction',
		'#' => 'account'
	));

None of which work; you end up on the User tab. (The call is from within MyController, so controller should be optional.)

The important bits from the ctp file:

	<ul class="nav nav-tabs" style="clear:both;" id="tabs">
		<li class="active"><a href="#user-data">User</a></li>
		<li><a href="#account">Account</a></li>
	</ul>
	<div class="tab-content">
		<div class="tab-pane active" id="user-data">
			...
		</div>
		<div class="tab-pane form-horizontal container-fluid" id="account">
			...
		</div>
	</div>

Searching online I have found discussions on how to do this from javascript (dealing with the ‘active’ class), but nothing for Controller::redirect().
Can it be done? How?


#2

Hi.

As far as i know the problem in here is that you cannot set css-attributes by passing arguments when redirecting, but you can generally achieve jumping to a tab from within a controller like this:

In the controller-function
$this->set("activetab", "mytab");

and in the ctp-file

<div class="tab-content">
	<div class="tab-pane <?= strcmp($activetab, "mytab") == 0 ? "active" : ""  ?>" id="user-data">
		...
	</div>
	<div class="tab-pane form-horizontal container-fluid" id="account">
		...
	</div>
</div>

Note the <?= strcmp($activetab, "mytab") == 0 ? "active" : "" ?> part in here, which echo’s “active” when the view variable set in the controller is equal to “mytab”.

When you want to load the content just in time/on demand (when the relevant tab is clicked), you have to use js/ajax to achieve this.

Hope this helps.


#3

Thank you! With that I was able to get it to work.
A note on implementation for any that may land here looking for the same: I had to add a named parameter to the redirect call and alter the target action to inspect for that parameter and use set() as appropriate.