10

I have a problem: I can't sorting my gridview.

When i click on "Product name": enter image description here In URL i can see : index?sort=-product_name but nothing happens. I did not use CRUD generator.

Controller

public function actionIndex()
{
    $searchModel = new CompanyProductSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    return $this->render('index', [
        'dataProvider' => $dataProvider,
        'searchModel' => $searchModel,
    ]);
}

SearchModel

public $searchstring;

public function rules()
{
    return [
        [['date', 'product_name', 'searchstring'], 'safe'],
    ];
}


public function scenarios()
{
    return Model::scenarios();
}


public function search($params)
{
    $array = array();
    $user = Yii::$app->user->identity;
    $product_influencer = ProductInfluencer::find()->all();
    foreach($product_influencer as $product){
        $array[] .= $product->product_id;
    }
    $query = Product::find()->where(['company_id'=>$user->company_id])
        ->andWhere(['id'=>$array])
        ->andWhere(['is not', 'shop_price', null])
        ->andWhere(['is not', 'main_category_id', null])
        ->orderBy('date DESC');

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

    $dataProvider->sort->attributes['product_name'] = [
        'asc' => ['product_name' => SORT_ASC],
        'desc' => ['product_name' => SORT_DESC],
    ];



    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'product_name' => $this->product_name,
    ]);


    $query->andFilterWhere(['like', 'product_name', $this->searchstring]);

    return $dataProvider;
}

View

<?php Pjax::begin(); ?>
<?= GridView::widget([
    'summary'=>"",
    'dataProvider' => $dataProvider,
    'tableOptions' => [
        'class' => 'table table-responsive',
        'id' => 'sortable-table',
    ],
    'pager' => [
        'class' => 'common\widgets\CustomPager',
        'prevPageLabel' => '<div style="border: none" class="glyphicon glyphicon-menu-left"></div>',
        'nextPageLabel' => '<div style="border: none" class="glyphicon glyphicon-menu-right"></div>',
        'maxButtonCount' => 0,
    ],
    'columns' => [
        ['class' => 'yii\grid\CheckboxColumn',],
        [
            'class' => 'yii\grid\SerialColumn',
            'header' => 'Nr.',
        ],
        [
            'format' => 'raw',
            'label' => 'Product name',
            'attribute' => 'product_name',
            'value' => function($model){
                return Html::a($model->product_name, ['detail-product'], ['data' => [
                    'params'=>['id'=>$model->id],
                    'method' => 'get',
                ]]);
            }
        ],
        [
            'label' => 'Total earnings',
            'value' => function($model){
                return '$ 950 (test)';
            }
        ],
        [
            'label' => 'Units available',
            'value' => function($model){
                $units = \common\models\ProductInfo::findOne(['product_id'=>$model->id]);
                return $units->shop_units;
            }
        ],
    ],
]); ?>
<?php Pjax::end(); ?>

Thanks!

2
  • Remove dot inside $array[] .= $product->product_id;. Commented Apr 17, 2017 at 19:49
  • Removed, still does not work Commented Apr 17, 2017 at 19:55

1 Answer 1

24

It's probably because you've already set the sort in the query. The dataProvider is unable to override this. You should remove the orderBy clause in the query.

Usually I prefer to set the sorting in the dataProvider like this, as it makes it clearer what attributes are allowed to be sorted;

    $dataProvider->setSort([
        'attributes' => [
            'product_name' => [
                'asc' => ['product_name' => SORT_ASC],
                'desc' => ['product_name' => SORT_DESC],
                'default' => SORT_ASC
            ],
            'date' => [
                'asc' => ['date' => SORT_ASC],
                'desc' => ['date' => SORT_DESC],
                'default' => SORT_ASC,
            ],
        ],
        'defaultOrder' => [
            'date' => SORT_ASC
        ]
    ]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.