Yii2-搜索链接的参数

时间:2019-06-22 04:07:51

标签: search parameters yii2 pjax

我需要通过属性“ year”实现搜索,但是我需要在链接中进行搜索(例如,当我单击链接“ 2017”时,$ dataProvider仅返回“ year” =“ 2017”的记录)。 .php我有:

<div class="y-index">

    <?php echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Oficios Apqe'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

<?php Pjax::begin(); ?>    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => [
                'id',
                'name',
                'minuta',
                'year',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
<?php Pjax::end(); ?>

</div>

_search.php

<div class="y-search">

    <?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
    ]); ?>


    <?= $form->field($model, 'id') ?>

    <?= $form->field($model, 'name') ?>

    <?= $form->field($model, 'minuta') ?>

<?= Html::a('2019', ['index', 'year' => '2019'], ['class' => 'year label label-efault', 'id' => '2019']) ; ?>
<?= Html::a('2018', ['index', 'year' => '2018'], ['class' => 'year label label-default', 'id' => '2018']) ; ?>
<?= Html::a('2017', ['index', 'year' => '2017'], ['class' => 'year label label-default', 'id' => '2017']) ; ?>


    <div class="form-group">
        <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
        <?= Html::a(Yii::t('app', 'Reset'), [Yii::$app->controller->action->id], ['class' => 'btn btn-default']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

基本上,我需要yii网站的示例:

enter image description here

当我单击“教程”选项中的左侧菜单时,链接设置为“ https://www.yiiframework.com/wiki?category=3&version=2.0”;

enter image description here 然后,当我单击菜单“ AJAX”后,该链接将被追加并设置为“ https://www.yiiframework.com/wiki?tag=ajax&category=3&version=2.0”;

enter image description here

基本上,我需要带有链接的搜索形式参数“ year”,以及来自形式“ _search.php”的串联参数。

2 个答案:

答案 0 :(得分:0)

您可以将数组作为第二个参数传递。

Html::a()在后​​台使用yii\helpers\Url::to()创建链接docs。您可以使用想要将它们作为数组传递的任何参数。

Html::a(
    '2017', 
    ['', 'year' => '2017'], 
    ['class' => 'year label label-default', 'id' => '2017']
) ; 

答案 1 :(得分:0)

您只需要从action表单中删除_search.php,它将自动提交给当前操作,并且它将使用从表单中传递的新搜索参数附加当前查询字符串。 / p>

只需更改

<?php $form = ActiveForm::begin([
     'action' => ['index'],
     'method' => 'get',
]); ?>

<?php $form = ActiveForm::begin([
     'method' => 'get',
]); ?>