2017-06-09 7 views
0

yii2グリッドビューのテーブルでは、アルファベット順の並べ替えに使用する[1 => car、2 => motobike、3 =>フェリー]古典的な関係の)。配列をグリッドビュー(Yii2)の並べ替え基準として使用する

これをデータプロバイダで設定するにはどうすればよいですか?どんな助けでも大歓迎です!

<?= GridView::widget([ 
      'dataProvider' => $dataProvider, 
     // 'filterModel' => $searchModel, 
      'tableOptions' => ['class' => 'table'], 
      'layout'=>"{summary}\n<div class='rounded'>{items}</div>\n{pager}", 
      'summary' => "Zeige {begin} - {end} von {totalCount} Ideen", 
      'columns' => [ 

       'id', 
       [ 
        'attribute' => 'art', 
        'value' => function ($model) { 
         $arten = \Yii::$app->params['ideenArten']; //[1=>car, 2=>bike, 3=>scooter etc] 
         return $arten[$model->art]; 
        }, 
        'label' => 'Kategorie', 
        'filter' => Html::activeDropDownList($searchModel, 'art', \Yii::$app->params['ideenArten'],['class'=>'form-control','prompt' => 'Select Type']), 
       ], 

      ], 
     ]); ?> 

そして、私は関係で正常にいくつかの他のフィールドのソート設定どこ私の検索モデルは以下の通りです:

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

    // add conditions that should always apply here 
    $query->joinWith(['authorprofile']); 


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


    $dataProvider->sort->attributes['authorprofile'] = [ 
     // The tables are the ones our relation are configured to 
     // in my case they are prefixed with "tbl_" 
     'asc' => ['user_profile.lastname' => SORT_ASC], 
     'desc' => ['user_profile.lastname' => SORT_DESC], 
    ]; 

    $dataProvider->sort->attributes['art'] = [ 
     'asc' => ['user_profile.lastname' => SORT_ASC], 
     'desc' => ['user_profile.lastname' => SORT_DESC], 
    ]; 

    $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, 
     'art' => $this->art, 
     'status' => $this->status, 
     'projektstart' => $this->projektstart, 
     'projektende' => $this->projektende, 
     'created_by' => $this->created_by, 
     'updated_by' => $this->updated_by, 
     'created_at' => $this->created_at, 
     'updated_at' => $this->updated_at, 

    ]); 

    $query->andFilterWhere(['like', 'title', $this->title]) 
     ->andFilterWhere(['like', 'body', $this->body]) 
     ->andFilterWhere(['or', 
      ['like', 'user_profile.firstname', $this->authorprofile], 
      ['like', 'user_profile.lastname', $this->authorprofile], 
     ]); 


    return $dataProvider; 
} 

とコントローラのコードは次のとおりです。

public function actionSpeicher() 
{ 
    $lastIdea = Idee::find()->orderBy("updated_at DESC")->one(); 
    $searchModel = new IdeeSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

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

updateあなたの質問gridviewのコードを追加すると、関連するコントローラ/アクションコード.. pelase – scaisEdge

+0

こんにちはscaisEdge、私の最初の投稿に私のgridviewコード(関連部分)が追加されました – dave3011

答えて

0

1 、2,3 ..これらはDB内のデータの表現なので、データベースはフレームワーク内で1が "car"と呼ばれていることを知らない。タイプを文字列として保存するか、手動で数値を並べ替えるか(1 => car、2 => ferry、3 => motobikeなど)

しかし、あなたはそれに満足していない場合、あなたはこのようなものを回避することができます(それにもかかわらず、それが汚いと、低速だと、私はこの方法を実装することはお勧めしません)

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

    // add conditions that should always apply here 
    $query->joinWith(['authorprofile']); 

    // ************* GENERATE SQL TABLE FROM arten PHP ARRAY ******************** 
    // Get your List from params 
    $arten = \Yii::$app->params['ideenArten']; // [1=>car, 2=>bike, 3=>scooter etc] 
    $artenQuery = null; 
    // Convert PHP array to SQL table, pass value (1,2,3) and values (car, boke, scooter) 
    foreach ($arten as $key => $value) { 
     if(!($artenQuery instanceof \yii\db\Query)) { 
      $artenQuery = new \yii\db\Query(); 
      $artenQuery->select(new \yii\db\Expression("{$key} AS value, \"{$value}\" AS label")); 
     } else { 
      $artenQuery->union("SELECT {$key} AS value, \"{$value}\" AS label"); 
     } 
    } 
    // If you execute $artenQuery itself, you can notice that it will return a table (generated from php array of cource) 
    // Then we can leftJoin the table to our main table. Call our temporary arten table as "dummy" 
    // Make sure "art" attribute stores number from "arten" list 
    $query->leftJoin(['dummy' => $artenQuery], 'dummy.value = art'); 
    // ************************************************************* 

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


    // REGISTER SORT FOR RELATIONAL FIELD (joined 'dummy' table, which holds string representative of "art" now) 
    $dataProvider->sort->attributes['art'] = [ 
     'asc' => ['dummy.label' => SORT_ASC], 
     'desc' => ['dummy.label' => SORT_DESC], 
    ]; 

    //..... continue your code 

私はいくつかの詳細を欠場する可能性があなたのコードで。 私の場合、私はDBに0,1,2として保存された状態でテストしました。(削除済み、アクティブ、無効..というようにWebに表示されています)ので、正常に文字列を処理してソートしました。

+0

Yerke、大きなありがとう、私はこれを試してみますそれが仕事をするかどうか見てください - 私は配列データをデータベースに保存しない場合は! – dave3011

関連する問題