Yii2 GridView按数组ID对id列进行排序

时间:2016-04-24 04:00:53

标签: gridview yii2

我是Yii2的新手。

我想在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]]]);

我知道上面的代码不起作用。只是为了想象我想做什么。 请帮忙。

更新 完整视图代码

    <?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 个答案:

答案 0 :(得分:0)

相关问题