2016-04-24 175 views
1

我是Yii2的新手。Yii2 GridView根据数组ID排序id列

我想在GridView的第一行显示一些id的数据。

示例:我想在第一行(1,2,3)上显示第3,5,8行的数据。

的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

回答