如何仅在yii2 gridview中显示修复记录

时间:2017-08-29 16:04:49

标签: yii2

我正在使用yii2 Framework。我想在gridview中只有少数记录,其中status = enter image description here

中的状态= 1

这是gridview的屏幕截图我只希望突出显示状态为1的记录 本“查看代码”页面为 - :

<p>All Issued Devices are Highlighted   </p>
<?php

use yii\helpers\Html;
use yii\grid\GridView;

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

//$this->title = 'Tracked Devices';
//$this->params['breadcrumbs'][] = $this->title;
?>
<b style="font-size: 30px;">ALL ISSUED DEVICES ARE HIGHLIGHTED   </b>
<div class="device-tracker-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'rowOptions'=>function($model)
         {
          if($model->status == '1')
          {
              return ['class'=>'info'];
          }
          else
          {
              return ['class'=>'default'];
          }


        },

        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute'=>'devices_device_id',
                'value'=>'devicesDevice.device_name'



            ],
            //'id',
            //'devices_device_id',
            'user_first_name',
            'user_last_name',
            'email_address:email',
            // 'user_department',
             'assign_date',
             'return_date',
             //'status',
             //'Issued_by',
            [

              'attribute'=>'Issued_by' ,
              'value'=>'issuedBy.first_name'
            ],

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
</div>

请告诉我如何解决问题?

控制器操作是 -

 public function actionReturn()
    {
       // echo "welcome";die;
        if(isset(Yii::$app->user->identity->user_type))
        {
        $status = Yii::$app->user->identity->user_type;
        if($status == 1)
        {
        $this->layout = 'headerLayoutSuperadmin';
        $searchModel = new DeviceTrackerSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index1', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
        }
        else
        { 
        $this->layout = 'headerLayout';
        $searchModel = new DeviceTrackerSearch();
        $dataProvider =    $searchModel->search(Yii::$app->request->queryParams);

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

             return $this->redirect(array('site/login'));
        }

    }

sql搜索模型 - :

<?php

namespace frontend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\DeviceTracker;

/**
 * DeviceTrackerSearch represents the model behind the search form about `frontend\models\DeviceTracker`.
 */
class DeviceTrackerSearch extends DeviceTracker
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id',  'status', 'Issued_by'], 'integer'],
            [['user_first_name', 'devices_device_id', 'user_last_name', 'email_address', 'user_department', 'assign_date', 'return_date'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = DeviceTracker::find();
         //$query = DeviceTracker::findAll('status=>1');
        // print_r($query);die;
         //echo $query;die;
        // add conditions that should always apply here

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

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        $query->joinWith('devicesDevice');
        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            //'devices_device_id' => $this->devices_device_id,
            'assign_date' => $this->assign_date,
            'return_date' => $this->return_date,
            'status' => $this->status,
            'Issued_by' => $this->Issued_by,
        ]);

        $query->andFilterWhere(['like', 'user_first_name', $this->user_first_name])
            ->andFilterWhere(['like', 'user_last_name', $this->user_last_name])
            ->andFilterWhere(['like', 'email_address', $this->email_address])
            ->andFilterWhere(['like', 'user_department', $this->user_department])
            ->andFilterWhere(['like', 'devices.device_name', $this->devices_device_id]);

        return $dataProvider;
    }
}

1 个答案:

答案 0 :(得分:0)

在搜索模型中

,更新查询以适合您要传递给视图的输出。 当前条件的类似情况,即您只想显示状态为1

的行

所以,

$query->andFilterWhere([
            'id' => $this->id,
            //'devices_device_id' => $this->devices_device_id,
            'assign_date' => $this->assign_date,
            'return_date' => $this->return_date,
            'status' => 1, // **this is where you are filtering the data with status == 1**
            'Issued_by' => $this->Issued_by,
        ]);

并简要解释流程

  • 调用dataProvider的控制器,为您提供传递给视图的数据,然后最终传递给gridView
  • 数据存储在mysql表中,因此无论您要发送哪些数据进行查看,都需要在dataProvider查询中的searchModel中查询数据
  • 将dataProvide返回给控制器&gt;视图&gt; gridWidget