Yii2 删除初始搜索

Yii2 Remove initial Search

我在互联网上搜索了几个小时,试图删除 yii2 gridview 上的初始过滤器。所有选项都指向在搜索中设置默认值。我希望在加载索引页面时,只有在用户搜索时才在 gridview 中显示任何数据。非常感谢任何帮助。

https://github.com/yiisoft/yii2/issues/5668



<?php
use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel frontend\models\AnimalSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Animal and Animal Products Search';
$this->params['breadcrumbs'][] = $this->title;
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
    //////prevent the dropdown from being populated when the user click back
$(window).bind("pageshow", function() {
    $("#commodity").val('');
    $("#species").val('');
    $("#finality").val('');
    $("#origin").val('');
});
</script>  


<div class="animal-index">

    <h1 align="center"><?= Html::encode($this->title) ?></h1>
    <br>

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


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        // 'filterModel' => $searchModel,
       //  'showHeader'=> false,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

           // 'id',

            'commodity',
            'species',
            'finality',
            'origin',
            // 'intendeduse',

            // 'risk',
            // 'conditions',
            // 'scu',
            // 'maf',
            // 'cities',
            // 'fisheries',
            // 'forestry',
            // 'comments',

            ['class' => 'yii\grid\ActionColumn',
                'visibleButtons' => [
             'update' =>false, // or whatever condition
              'delete' => false,
          ],
        ],
        ],
    ]); ?>
</div>

您需要添加一个条件,只要不存在搜索参数就不会产生任何结果

将您的搜索模型更新为类似这样的东西

public function search($params)
{
    $query = Model::find();
    
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    
    // this ensures no rows are returned if nothing was submitted
    if (is_null($params) || empty($params)) {
        $query->where('0 = 1');
        return $dataProvider;
    }
    
    // ....
}