0
votes

i have a table with record with multiple years (2017, 2018, 2019 ect..). I need by default the gridView show the year bigger (in this example is "2019"). I did it this way. it's ok?

MaintenanceController.php

        $greaterYear = Maintenance::find()->select(['year'])->where(['id_type' => $id_type])->orderBy(['year' => SORT_DESC])->one();
        $searchModel->year = $greaterYear->year;

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('view', [
            'model' => $this->findModel($id_type),
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
?>

but i need filter with others years, now i already have the link with othes years like this:

view.php

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

now i need filter gridView when i click in the link of other years i made this "trick" using Pjax but i think this is not good, and if i hide the "year" from gridView obviously does not work.

view.php

<?php
$script = <<< JS
$(function(){
    $('body').on('click', '.year', function(e) {
        e.preventDefault();
        $('input[name="PlanoManutencaoSearch[ano]"').val($(this).attr("id"));
        $("#w0").yiiGridView("applyFilter");
    });
});
JS;
$this->registerJs($script);
?>

which way to implement this?

note i use this gridView in view with params in url like this/controller/action?id_type=8, because of this the url can not be completely changed and i need it to be possible add filter from others "attributes" (url is incremental).

Update add complete view file.

<div class="maintenance-view">

    <div class="panel-group">
        <div class="panel panel-primary">
            <div class="panel-heading">Filter</div>
            <div class="panel-body">
                <?php echo Html::a('2019', '', ['class' => 'year label label-default', 'id' => '2019']) ; ?>
                <?php echo Html::a('2018', '', ['class' => 'year label label-default', 'id' => '2018']) ; ?>
                <?php echo Html::a('2017', '', ['class' => 'year label label-default', 'id' => '2017']) ; ?>
            </div>
        </div>
    </div>

    <?php Pjax::begin(); ?>    <?php echo GridView::widget([
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,

        'columns' => [
                'year',
                ['attribute' => 'id_client',
                    'value' => function($model){
                        if (isset($model->idClient->name)) {
                            return $model->idClient->name;
                        }
                    }
                ],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>

<?php
$script = <<< JS
$(function(){
    $('body').on('click', '.year', function(e) {
        e.preventDefault();
        $('input[name="PlanoManutencaoSearch[ano]"').val($(this).attr("id"));
        $("#w0").yiiGridView("applyFilter");
    });

});
JS;
$this->registerJs($script);
?>

in this case i need when page load, show records in gridView with "year" = "2019" ($greaterYear->year (variable from controller)). And when i click in label with other year (ex. "2017") the gridView only show record with year = "2017".

1
i am unabe to understand what you are trying to ask. are you trying to filter the records in the grid view according to any given year? or just the given link? add your complete view file - Muhammad Omer Aslam
@MuhammadOmerAslam just the given links. I update my question, added complete view, and an explanation below. - Moutinho
@MuhammadOmerAslam sorry but you understand? - Moutinho

1 Answers

0
votes

If you want to manually pass the searchModel filter:

Uncomment Your filterModel*

<?php echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel, //<--- Renable....
    ...
?>

Filter Link

<?php
$route = '?PlanoManutencaoSearch[ano]=2019';
echo Html::a('2019', $route, ['class' => 'year label label-default', 'id' => '2019']) ; 
?>