2016-04-24 18 views
1

私はYii2の初心者です。Yii2 GridViewはID列を配列IDでソートします

いくつかのIDのデータをGridViewの最初の行に表示したいとします。

例:行3,5,8のデータを最初の行(1,2,3)に表示します。

3,5,8のデータIDは、私は上記のコードは動作しない知っている[10,13,15] enter image description here] 1

$dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 
$dataProvider->setSort(['defaultOrder' =>['id' => [10,13,15]]]); 

アレイです。ちょうど私がしたいことを想像してください。 助けてください。

UPDATE 完全なビューのコード

<?php 
$gridColumns = [ 
    ['class' => 'kartik\grid\SerialColumn'], 
    [ 
     'attribute' => 'ten', 
     'vAlign'=>'middle', 
     'noWrap'=>true 
    ], 
    [ 
     'attribute'=>'soLuongSan', 
     'vAlign'=>'middle', 
     'format'=>'raw', 
     'noWrap'=>false 
    ], 
    [ 
     'class' => 'kartik\grid\DataColumn', 
     'attribute'=>'dc_tinhThanhPho', 
     'label' => ' Tỉnh Thành Phố', 
     'format' => 'text', 
     'value' => function($model, $index, $column) { 
      $m = Province::find()->where(['provinceid' => $model->dc_tinhThanhPho])->one(); 
      return $m ? $m->name : 'Unknow'; 
     }, 
     'filterType'=>GridView::FILTER_SELECT2, 
     'filter'=> ArrayHelper::map(Province::find()->asArray()->all(), 'provinceid', 'name'), 
     'filterWidgetOptions'=>[ 
      'pluginOptions'=>['allowClear'=>true], 
     ], 
     'filterInputOptions'=>['placeholder'=>Yii::t('app', 'Chọn Tỉnh - TP')] 
    ], 
    [ 
     'class' => 'kartik\grid\DataColumn', 
     'attribute'=>'dc_quanHuyen', 
     'label' => 'Quận Huyện', 
     'format' => 'text', 
     'value' => function($model, $index, $column) { 
      $m = District::find()->where(['districtid' => $model->dc_quanHuyen])->one(); 
      return $m ? $m->name : 'Unknow'; 
     }, 
     'filterType'=>GridView::FILTER_SELECT2, 
     'filter'=> ArrayHelper::map(District::find()->asArray()->all(), 'districtid', 'name'), 
     'filterWidgetOptions'=>[ 
      'pluginOptions'=>['allowClear'=>true], 
     ], 
     'filterInputOptions'=>['placeholder'=>Yii::t('app', 'Chọn Quận - Huyện')] 
    ], 
    [ 
     'class' => 'kartik\grid\DataColumn', 
     'attribute'=>'dc_phuongXa', 
     'label' => 'Phường Xã', 
     'format' => 'text', 
     'value' => function($model, $index, $column) { 
      $m = Ward::find()->where(['wardid' => $model->dc_phuongXa])->one(); 
      return $m ? $m->name : 'Unknow'; 
     }, 
     'filterType'=>GridView::FILTER_SELECT2, 
     'filter'=> ArrayHelper::map(Ward::find()->asArray()->all(), 'wardid', 'name'), 
     'filterWidgetOptions'=>[ 
      'pluginOptions'=>['allowClear'=>true], 
     ], 
     'filterInputOptions'=>['placeholder'=>Yii::t('app', 'Chọn Phường - Xã')] 
    ], 
    [ 
     'attribute'=>'sdt', 
     'vAlign'=>'middle', 
     'noWrap'=>true 
    ], 

    (!Yii::$app->user->isGuest && Yii::$app->user->identity->isAdmin) ? (
    ['class' => 'kartik\grid\ActionColumn'] 
    ) : [ 
     'attribute'=>'gioHoatDong', 
     'vAlign'=>'middle', 
     'noWrap'=>true 
    ], 
]; 

?> 

<?= GridView::widget([ 
    'dataProvider' => $dataProviderSanChu, 
    'filterModel' => $searchModelSanChu, 
    'bordered' => true, 
    'striped' => false, 
    'condensed' => false, 
    'hover'=>true, 
    'pjax'=>true, 
    'resizableColumns'=>true, 
    'resizeStorageKey'=>Yii::$app->user->id. '-' . date("m"), 
    'floatHeader'=>true, 
    'headerRowOptions'=>['class'=>'kartik-sheet-style'], 
    'filterRowOptions'=>['class'=>'kartik-sheet-style'], 
    'panel' => [ 
     'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i>'.Yii::t('app', ' Danh sách sân').'</h3>', 
     'type'=>'success', 
     'before'=> '<i style="color: blue">'.Yii::t('app', '* Tìm kiếm bằng cách nhập thông tin vào các ô trống bên dưới.').'</i>', 
     'after'=> Html::a('<i class="glyphicon glyphicon-repeat"></i>'.Yii::t('app', ' Reset danh sách'), ['index'], ['class' => 'btn btn-default']), 
     'footer'=>false 
    ], 
    'toolbar' => [ 
     [ 
      'content'=> 
       Html::a('<i class="glyphicon glyphicon-plus"></i>'.Yii::t('app', ' Thêm sân mới'), ['create'], ['class' => 'btn btn-success', 'title' => Yii::t('app', 'Thêm sân mới')]), 
     ], 
     (!Yii::$app->user->isGuest && Yii::$app->user->identity->isAdmin) ? ('{export}') : '', 
     '{toggleData}' 
    ], 
    // set export properties 
    'export'=>[ 
     'fontAwesome'=>true, 
     'target' => '_self', 
     'showConfirmAlert' => false, 
    ], 

    'rowOptions' => function ($searchModelSanChu, $key, $index, $grid) { 
     return [ 
      'style' => "cursor: pointer", 
      'onclick' => 'location.href="' 
       . Yii::$app->urlManager->createUrl('datsan/index') 
       . '?id_sanChu='.$key.'";', 
     ]; 
    }, 
    'columns' => $gridColumns, 
]); 

?> 

あなたは計算フィールドを通して、あなたのソート順を表現することができ

public function search($params) 
{ 
    $query = Sancon::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, 
     'id_sanChu' => $this->id_sanChu, 
     'id_loaiSan' => $this->id_loaiSan, 
     'chieuNgang' => $this->chieuNgang, 
     'chieuDoc' => $this->chieuDoc, 
     'thanhLap' => $this->thanhLap, 
     'created_date' => $this->created_date, 
     'update_date' => $this->update_date, 
     'id_nguoiTao' => $this->id_nguoiTao, 
     'id_nguoiCapNhat' => $this->id_nguoiCapNhat, 
     'xacNhan' => $this->xacNhan, 
     'ngungHoatDong' => $this->ngungHoatDong, 
    ]); 

    $query->andFilterWhere(['like', 'chatLuong', $this->chatLuong]) 
     ->andFilterWhere(['like', 'khangDai', $this->khangDai]) 
     ->andFilterWhere(['like', 'maiChe', $this->maiChe]) 
     ->andFilterWhere(['like', 'gia_ngayThuongNgay', $this->gia_ngayThuongNgay]) 
     ->andFilterWhere(['like', 'gia_ngayThuongDem', $this->gia_ngayThuongDem]) 
     ->andFilterWhere(['like', 'gia_cuoiTuanNgay', $this->gia_cuoiTuanNgay]) 
     ->andFilterWhere(['like', 'gia_cuoiTuanDem', $this->gia_cuoiTuanDem]); 

    return $dataProvider; 
} 
+1

ビューのコードを示し、(GridViewの)また、あなたの関連する検索モデルのコード..そして、wìmew ith fiieldはID列です – scaisEdge

+1

投稿した画像はあなたのコードとは関係ありません..そうですか?あなたのイメージを再描画する際の数字は、['class' => 'kartik \ grid \ SerialColumn']の列の結果ですか? – scaisEdge

+0

申し訳ありませんが、それは正しいです。私はちょうど私の問題を示したい。 – Sam

答えて

関連する問題