YII2:选项卡中的过滤器适用于所有选项卡

时间:2016-04-27 10:42:35

标签: php yii2

我刚刚设法(感谢另一个答案)在两个不同的标签中显示相同模型的不同结果。但是现在当我在两个标签中的一个中添加过滤器时,过滤器也适用于另一个标签。 例如,使用该代码:

<?=
Tabs::widget([
'items' => [
[
    'label' => 'New Books',
    'content' =>  GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'columns' => [
                        [
                            'label' => 'Book Name',
                            'attribute' => 'book_name',
                            'value' => 'book_name',
                        ],

                    ],
                ]),
],
[
    'label' => 'Old Books',
    'content' => GridView::widget([
                    'dataProvider' => $dataProviderException,
                    'filterModel' => $searchModel,
                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],

                        //'id',
                        [
                            'label' => 'Book Color',
                            'attribute' => 'book_color',
                            'value' => 'book_color',
                        ],
                        [
                            'label' => 'Book Name',
                            'attribute' => 'book_name',
                            'value' => 'book_name',
                        ],
                         ],
                ]),
      ],
],
]);
?>

例如,如果我在标签中搜索图书名称,则相同的过滤器适用于两个不是我想要的标签。

这是我在控制器中的动作

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

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

这是我的模型搜索(所有这些都是简化版)

<?php

namespace app\modules\book\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\book\models\Book;
use yii\db\Expression;

class BookSearch extends Book
{
public $location;
/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id', 'pages', 'author_id'], 'integer'],
        [['name', 'color', 'release_date'], 'safe'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios()
{
    return Model::scenarios();
}

/**
 * @param array $params
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = Book::find();

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

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'pages' => $this->pages,
        'author_id' => $this->author_id, 
        'name' => $this->name, 
        'color' => $this->color,
        'release_date' => $this->release_date,
    ]);

    return $dataProvider;
}

public function searchException($params)
{
    $query = Book::find();

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

    $this->load($params);

    if (!$this->validate()) {
        return $dataProviderException;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'pages' => $this->pages,
        'author_id' => $this->author_id, 
        'name' => $this->name, 
        'color' => $this->color,
        'release_date' => $this->release_date,
    ]);

    return $dataProviderException;
}

}

编辑:对不起,我已经改变了一些事情,使其更具可读性,但我做得更糟。在BookSearch内部,我创建了两个函数“search”和“searchexception”,它们在index.php中分配给每个选项卡的“dataProvider”。 现在我需要将应用于两个选项卡之一的过滤器也不会应用到另一个选项卡

0 个答案:

没有答案