Yii2使用url hash(#)或Fragment从控制器重定向到特定选项卡

时间:2018-04-20 11:02:18

标签: php twitter-bootstrap yii2

这是我表格中的相关代码:

<?php $form = ActiveForm::begin ( [ 'id' => 'dynamic-form' , 'layout' => 'horizontal' , 'enableClientValidation' => true , 'enableAjaxValidation' => true ] ); ?>
<div class="panel panel-default">
    <div class="panel-body nav-tabs-animate nav-tabs-horizontal">
        <ul class="nav nav-tabs nav-tabs-line" data-plugin="nav-tabs" role="tablist">
            <li class="active" role="presentation"><a data-toggle="tab" href="#information" aria-controls="information" role="tab">
                    <?= Yii::t ( 'app' , 'Information' ); ?></a></li>    
            <li role="presentation"><a data-toggle="tab" href="#rest_days" aria-controls="rest_days" role="tab">
                    <?= Yii::t ( 'app' , 'Rest Days' ); ?></a></li>
            <li role="presentation"><a data-toggle="tab" href="#instructor_schedule" aria-controls="instructor_schedule" role="tab">
                    <?= Yii::t ( 'app' , 'Instructor Schedule' ); ?></a></li>
        </ul>
    </div>
</div>

和控制器中的代码:

public function actionUpdate( $id ) {
    $model = $this->findModel ( $id );

    if ( $model->load ( Yii::$app->request->post () ) && $model->save () ) {
        Yii::$app->getSession ()->setFlash ( 'successClass' );
        // return $this->redirect(['view', 'id' => $model->id]);
        return $this->redirect ( [ 'user/update-instructor' , 'id' => $model->instructor_id , '#' => 'instructor_schedule' ] );
    }
    return $this->render ( 'update' , [
                'model' => $model ,
    ] );
}

我想重定向到标签 - instructor_schedule,它正确地重定向到user/update-instructor,但没有转到特定标签,而是第一个标签。

我怎么能做到这一点?

更新 查看文件 - update_instructor.php

use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $model app\models\User */
$tab='';
if (isset($_GET["tab"])){
$tab= $_GET['tab'];
}
if(!empty($tab)){
$this->registerJs ( "$('#dynamic-form .nav-tabs a[href=\"#".$tab."\"]').tab('show');" , yii\web\View::POS_READY );
}

$this->title = $title . ' ' . $model->first_name . ' ' . $model->last_name;
$this->params['breadcrumbs'][] = ['label' => 'Instructors', 'url' => ['instructor']];
//$this->params['breadcrumbs'][] = ['label' => $model->first_name.' '.$model->last_name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = $this->title;
$manager = User::find()->where(['user_role' => 'manager'])->andwhere(['status' => 10])->all();
$manager_array = ArrayHelper::map($manager, 'id',
    function ($model) {
        return $model->first_name . ' ' . $model->last_name . '(' . $model->manager->location['location_title'] . ')';
    });
?>



<?php $form = ActiveForm::begin(['id' => 'dynamic-form', 'layout' => 'horizontal', 'enableClientValidation' => true, 'enableAjaxValidation' => true]);?>
  <div class="panel panel-default">
      <div class="panel-body nav-tabs-animate nav-tabs-horizontal">
      <ul class="nav nav-tabs nav-tabs-line" data-plugin="nav-tabs" role="tablist">
        <li class="active" role="presentation"><a data-toggle="tab" href="#information" aria-controls="information" role="tab">
          <?=Yii::t('app', 'Information');?>
        </a></li>

        <li role="presentation"><a data-toggle="tab" href="#rest_days" aria-controls="rest_days" role="tab">
          <?=Yii::t('app', 'Rest Days');?>
        </a></li>
        <li role="presentation"><a data-toggle="tab" href="#instructor_schedule" aria-controls="instructor_schedule" role="tab">
          <?=Yii::t('app', 'Instructor Schedule');?>
        </a></li>
        <li role="presentation"><a data-toggle="tab" href="#custom_break_time" aria-controls="custom_break_time" role="tab">
          <?=Yii::t('app', 'Break Time');?>
        </a></li>


......

<div class="tab-content"> <br clear="all">
        <!-- start information tab -->
        <div class="tab-pane active animation-slide-left" id="information" role="tabpanel">
.....

<?php
if (!$model->isNewRecord) {
    ?>
        <div class="tab-pane animation-slide-left" id="rest_days" role="tabpanel">
          <?php DynamicFormWidget::begin([
....

<?php
if (!$model->isNewRecord) {
    ?>
          <div class="tab-pane animation-slide-left" id="instructor_schedule" role="tabpanel">
              <?php $this->title = Yii::t('app', 'Class Durations');
              $this->params['breadcrumbs'][] = $this->title;
            ?>

......

<?php
if (!$model->isNewRecord) { ?>
<div class="tab-pane animation-slide-left" id="custom_break_time" role="tabpanel">
 <?php   $this->title = Yii::t('app', 'Custom Break Times');
$this->params['breadcrumbs'][] = $this->title;
?>

.....

控制器 - ClassDurationController.php

public function actionUpdate($id,$tab='information') {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->getSession()->setFlash('successClass');

            // return $this->redirect(['view', 'id' => $model->id]);
                        return $this->redirect(['user/update-instructor', 'id' => $model->instructor_id, 'tab' => 'instructor_schedule']);
        }

        return $this->render('update', [
            'model' => $model,                        
        ]);
    }

3 个答案:

答案 0 :(得分:1)

主要的是你的哈希#fragment在浏览器中得到解决而从未发送到服务器,但它可以通过javascript访问。

如果您可以将fragment更改为parameter,请执行以下操作。

我假设你使用的是bootstrap标签。

  1. 将您的actionUpdate($id)更改为actionUpdate($id,,$tab='information')

  2. 将重定向的代码更改为return $this->redirect(['user/update-instructor', 'id' => $model->instructor_id, 'tab' => 'instructor_schedule']);

  3. 然后在您的视图文件$this->registerJs ( "$('#dynamic-form .nav-tabs a[href=\"#".$tab."\"]').tab('show');" , yii\web\View::POS_READY );

  4. 中添加以下内容

    现在,如果您的网址为http://www.yoursite.com/update?id=1,则会始终显示您的第一个标签,如果http://www.yoursite.com/update?id=1&tab=instructor_schedule它会显示instructor_schedule标签或任何其他提供的有效名称。

    希望这能解决你的问题。

    EDIT

    您应该从activeli中删除tabs类以禁用默认标签的显示,然后切换到所选问题,这就是导致它的原因

    见下面的2个罪魁祸首

    <li class="active" role="presentation"><a data-toggle="tab" href="#information" aria-controls="information" role="tab">
              <?=Yii::t('app', 'Information');?>
            </a></li>
    

    和标签

    <div class="tab-pane active animation-slide-left" id="information" role="tabpanel">
    

答案 1 :(得分:0)

如果您的控制器知道要打开哪个标签,请将其作为参数传递给您的视图:

return $this->render('update', [
        'model' => $model,
        'activeTab' => ...
    ]);

然后在你的视野中做一些

的事情
<li class="<?= $activeTab == 'xyz' ? 'active' : '' ?>" ...

答案 2 :(得分:0)

除了@ Muhammed.Omer.Aslam答案外,由于JQUERY的版本不同,您可能还会收到控制台错误,

Error: Syntax error, unrecognized expression: ....

我正在使用

$js=<<<JS 
          $('#dynamic-form .nav-tabs a[href="#$tab\\"]').tab('show');
 JS;

Reference  https://github.com/jquery/jquery/issues/2885