加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Yii2 gridview排序

发布时间:2020-12-13 21:59:03 所属栏目:PHP教程 来源:网络整理
导读:我有一个问题:我无法对gridview进行排序. 当我点击“产品名称”时: 在URL中我可以看到:index?sort = -product_name但没有任何反应.我没有使用CRUD发生器. 调节器 public function actionIndex(){ $searchModel = new CompanyProductSearch(); $dataProvi
我有一个问题:我无法对gridview进行排序.
当我点击“产品名称”时:

enter image description here


在URL中我可以看到:index?sort = -product_name但没有任何反应.我没有使用CRUD发生器.
调节器

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',$this->searchstring]);

    return $dataProvider;
}

视图

<?php Pjax::begin(); ?>
<?= GridView::widget([
    'summary'=>"",'dataProvider' => $dataProvider,'tableOptions' => [
        'class' => 'table table-responsive','id' => 'sortable-table',],'pager' => [
        'class' => 'commonwidgetsCustomPager','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' => 'yiigridCheckboxColumn',[
            'class' => 'yiigridSerialColumn','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 = commonmodelsProductInfo::findOne(['product_id'=>$model->id]);
                return $units->shop_units;
            }
        ],]); ?>
<?php Pjax::end(); ?>

谢谢!

解决方法

这可能是因为您已经在查询中设置了排序.数据提供者无法覆盖此.您应该删除查询中的orderBy子句.

通常我更喜欢在数据提供者中设置这样的排序,因为它更清楚地允许对哪些属性进行排序;

$dataProvider->setSort([
        'attributes' => [
            'product_name' => [
                'asc' => ['product_name' => SORT_ASC],'default' => SORT_ASC
            ],'date' => [
                'asc' => ['date' => SORT_ASC],'desc' => ['date' => SORT_DESC],'default' => SORT_ASC,'defaultOrder' => [
            'date' => SORT_ASC
        ]
    ]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读