YII2 - index.php中的列过滤器无法正常工作

时间:2016-11-22 13:52:21

标签: php yii yii2

我修改了index.php以显示名称而不是ID。

这是我的订单/ index.php

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\helpers\ArrayHelper;
use frontend\models\Statuses;


$this->title = Yii::t('app', 'Ordini');
$this->params['breadcrumbs'][] = $this->title;
$user = Yii::$app->user->identity;
?>
<div class="orders-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?php 
            if ($user->role == 10 || $user->role > 40)
                echo Html::a(Yii::t('app', 'Nuovo'), ['create'], ['class' => 'btn btn-success']);
        ?>
    </p>
<?php Pjax::begin(); ?>   

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

            ['attribute'=>'customer_id', 'value'=>'CustomersName',],
            ['attribute'=>'product_id', 'value'=>'ProductsName',],
             'date',
             [
                 'attribute'=>'status_id', 
                 'value'=>'StatusesName',
                 'filter' => Html::activeDropDownList($searchModel, 'status_id', ArrayHelper::map(Statuses::find()->asArray()->all(), 'id', 'name'),['class'=>'form-control','prompt' => 'Select Category']),
             ],

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
<?php Pjax::end(); ?></div>

过滤器不起作用。我尝试过下拉或只有数字但没有成功。 我不知道怎么检查失败的地方!

在OrdersSearch中?在模型或控制器中?

谢谢

1 个答案:

答案 0 :(得分:0)

您不必为下拉字段提供整个代码。这应该足够了:

'filter' => ArrayHelper::map(Statuses::find()->asArray()->all(), 'id', 'name'),