2017-12-28 9 views
0

나는 Yii-2를 처음 사용합니다. 색인 페이지에 일부 항목이 표시되는 격자보기가 있습니다.Yii2- 그리드보기에서 검색 상자를 추가하는 방법

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

     [ 
      'label' => 'Meter MSN', 
      'value' => function ($d) { 
       return $d->meter->meter_msn; 
      }, 
      // 'filter' => Html::activeDropDownList($searchModel, 'meter_id', \app\models\Meters::toArrayList(), ['prompt' => "All Meters", 'class' => 'form-control']), 

     ], 
     'imsi', 
     'telecom', 
     'status', 
     [ 
      'label' => 'Created By', 
      'value' => function ($data) { 
       if (is_object($data)) 
        return $data->created->name; 
       return ' - '; 
      }, 
      //'filter' => Html::activeDropDownList($searchModel, 'created_by', \app\models\User::toArrayList(), ['prompt' => "Created By", 'class' => 'form-control']), 
     ], 
     'comments', 
     'historic', 


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

이제 Meter MSN에 대한 검색 창을 추가하고 싶습니다. 위의 코드에서는 필터가 숨겨져 작동하지만 드롭 다운을 추가하지 않고 검색 상자를 원합니다. 다음은

내 검색 클래스입니다

public function search($params) 
{ 
    $query = MetersInventoryStore::find(); 

    // 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; 
    } 

    // grid filtering conditions 
    $query->andFilterWhere([ 
     'id' => $this->id, 
     'meter_id' => $this->meter_id, 
     'created_by' => $this->created_by, 
     'updated_by' => $this->updated_by, 
     'created_at' => $this->created_at, 
     'updated_at' => $this->updated_at, 
     'store_id' => $this->store_id, 
     'meter_serial'=>$this->meter_serial, 
     //   'historic' => $this->historic, 
     'status'=>'SIM Installed', 

    ]) 
     // ->orFilterWhere(['status'=>'Communication Failed']) 
    ; 

    //  $query->andFilterWhere(['like', 'meter_serial', $this->meter_serial]) 
    //  ->andFilterWhere(['like','meter_id',$this->meter_id]); 
    $query->orderBy(['id' => SORT_DESC]); 
    return $dataProvider; 
} 

어떻게 내가있는 검색 창을 배치 할 수 있습니다 ? 단순 검색 클래스는 기본적으로 검색 기능을 설정합니다. 그러나 내 MSN 값이 함수에서오고 있으므로 검색 상자를 어떻게 배치 할 수 있는지 잘 모릅니다. 도움을 주시면 감사하겠습니다.

계산 된 열에서 추가 필터 필드
+0

당신이 GII으로 CRUD를 만들 경우 당신은 탐색 양식을 작성하고 검색 클래스를 개발하는 _search.php 양식을 사용할 수있는 몇 가지 샘플을 찾을 수 있습니다. 그러나 이것이 당신의 경우인지 나는 모른다. 테이블의 값에 대한 측정기 msn? 그렇다면 db 열의 간단한 검색을 할 수 있습니다. –

답변

3

당신은 당신의 검색 모델

public function search($params) 
{ 
    public $your_column; 

    // declare as safe 
    public function rules() 
    { 
    return [ 
     ... 
     [[ 'your_column', ], 'safe'], 

    ]; 
    } 

    $query = MetersInventoryStore::find(); 

을에 에 pubblic의 VAR을 추가 한 다음 grid_view

... 
'columns' => [ 
    ['class' => 'yii\grid\SerialColumn'], 
    //'meter_id', 

    [ 
     'attribute' => 'your_column', 
     'label' => 'Meter MSN', 
     'value' => function ($d) { 
      return $d->meter->meter_msn; 
     }, 
    ], 

에 your_column 그리고 당신을 당신의 searchModel을 마지막으로 참조해야합니다 전달한 필터 값을 기반으로 계산 된 열을 올바르게 관리하기위한 필터 조건을 확장해야합니다.

당신은이 튜토리얼 http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

+0

올바른 답을 표시하는 정확한 값인 것입니다. – kiamoz

+0

예 : 매력이 있습니다. :) –

관련 문제