使 Kartik GridView 仅在搜索后显示

Making Kartik GridView to only show after search

所以我在我的 gridview 中做了一些调整,其中之一是将 gridview 显示为空白,并且只在我搜索后显示结果。我知道这是一个不寻常的调整,但这是我被要求做的。

我不能只评论数据提供者,因为它必须被设置。这是我可以通过视图做的事情吗?提前致谢

通过添加检查提交的过滤器参数是否具有值:

$searchModel = new Articles();      
$query = Articles::find();
$loadData = Yii::$app->request->get();
$scope = $searchModel->formName();
$isLoadedAttributes = false;
if(isset($loadData[$scope])){
    foreach($loadData[$scope] as $attributeValue){
        $isLoadedAttributes = $this->isLoadedAttributes || (bool)$attributeValue;
    }
}
if($isLoadedAttributes){
  $query->where('1 = 2');
}else{
    $searchModel->load($loadData);
}

$dataProvider = new ActiveDataProvider([
    'query' => query,
]);

做到了。如果以后有人想知道

在产品搜索中:

$query = Produtos::find();

// add conditions that should always apply here
if (!isset($_GET['ProdutosSearch'])){

$query = Produtos::find();
$query->where('1 = 2');
$dataProvider = new ActiveDataProvider([
    'query' => $query,
]);
return $dataProvider;
}

$dataProvider = new ActiveDataProvider([
    'query' => $query,
]);

$this->load($params);

控制器没有任何变化

public function actionIndex()
{
    $searchModel = new ProdutosSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}