1
votes

I'm a newbie in Yii2.

I want to display data of some ids on the first rows of GridView.

Example:I want to show the data of row 3,5,8 on the first rows (1,2,3).

The data id of 3,5,8 are an array [10,13,15] enter image description here]1

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

I know above code does not work. Just for imagining that what I want to do. Please help.

UPDATE Full view code

    <?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,
]);

?>

Search function

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
show the code of your view (gridview) and also the code for your related search model ..and please explain wìme with fiield is the id column - ScaisEdge
The image you posted is not related to your code .. right? and the number in redin your image are the result for ['class' => 'kartik\grid\SerialColumn'], column right? - ScaisEdge
Sorry, that is right. I just want to show my issue. - Sam
You can't sort the dataProvider result by the seriaColumn because the serial column is not a db column but an automatic column generated by the gridview widget .. and this value is not available in search function .. you can olnly filter and sort by activeDataProvider query columns .. - ScaisEdge
Thank you very much for your help. Just maybe, you missunderstand my issue. Sorry for English. - Sam

1 Answers