脚本宝典收集整理的这篇文章主要介绍了php – Yii2 gridview排序,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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' => '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(); ?>
谢谢!
通常我更喜欢在数据提供者中设置这样的排序,因为它更清楚地允许对哪些属性进行排序;
$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 ] ]);
以上是脚本宝典为你收集整理的php – Yii2 gridview排序全部内容,希望文章能够帮你解决php – Yii2 gridview排序所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。