<i id='xeCBM'><tr id='xeCBM'><dt id='xeCBM'><q id='xeCBM'><span id='xeCBM'><b id='xeCBM'><form id='xeCBM'><ins id='xeCBM'></ins><ul id='xeCBM'></ul><sub id='xeCBM'></sub></form><legend id='xeCBM'></legend><bdo id='xeCBM'><pre id='xeCBM'><center id='xeCBM'></center></pre></bdo></b><th id='xeCBM'></th></span></q></dt></tr></i><div id='xeCBM'><tfoot id='xeCBM'></tfoot><dl id='xeCBM'><fieldset id='xeCBM'></fieldset></dl></div>

    <tfoot id='xeCBM'></tfoot>
    <legend id='xeCBM'><style id='xeCBM'><dir id='xeCBM'><q id='xeCBM'></q></dir></style></legend>
      <bdo id='xeCBM'></bdo><ul id='xeCBM'></ul>
  1. <small id='xeCBM'></small><noframes id='xeCBM'>

      在 GridView Yii2 中对数据进行排序和过滤,其中列

      时间:2023-05-21
      • <i id='zQHfm'><tr id='zQHfm'><dt id='zQHfm'><q id='zQHfm'><span id='zQHfm'><b id='zQHfm'><form id='zQHfm'><ins id='zQHfm'></ins><ul id='zQHfm'></ul><sub id='zQHfm'></sub></form><legend id='zQHfm'></legend><bdo id='zQHfm'><pre id='zQHfm'><center id='zQHfm'></center></pre></bdo></b><th id='zQHfm'></th></span></q></dt></tr></i><div id='zQHfm'><tfoot id='zQHfm'></tfoot><dl id='zQHfm'><fieldset id='zQHfm'></fieldset></dl></div>
          <tbody id='zQHfm'></tbody>
        • <bdo id='zQHfm'></bdo><ul id='zQHfm'></ul>

        • <small id='zQHfm'></small><noframes id='zQHfm'>

          <tfoot id='zQHfm'></tfoot>

                <legend id='zQHfm'><style id='zQHfm'><dir id='zQHfm'><q id='zQHfm'></q></dir></style></legend>
                本文介绍了在 GridView Yii2 中对数据进行排序和过滤,其中列不在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                如果我在 db 中有 2 个字段 - 概率和影响,并且我需要 GridView 中的一列,其中这两个字段相乘.我设法将它添加到那里:

                If I have 2 fields in db - probability and influence and I need a column in GridView where these two fields are multiplied. I managed to add it there like:

                    [
                            'attribute' => 'priority',
                            'format' => 'raw',
                            'value' => function ($model) {
                                return $model->influence * $model->probability;
                            },
                        ],
                

                但无法处理排序,因为该列不在数据库中,向 $query 添加过滤器只会导致错误.

                But can't handle the sorting, because that column is not in db and adding filters to $query causes only errors.

                    $query = Risks::find();
                    $query->select(`probability*influence AS priority`);
                    $dataProvider = new ActiveDataProvider([
                        'query' => $query,
                    ]);
                

                已更新(适用于 Asc 和 Desc 但不适用于过滤器)

                Updated (works Asc and Desc but not with filters)

                public function search($params)
                {
                    $query = Risks::find();
                
                    $query->joinWith(['author', 'proj']);
                
                    $query->select('*, (probability * influence) as priority');
                
                    $dataProvider = new ActiveDataProvider([
                        'query' => $query,
                    ]);
                
                    $dataProvider->setSort([
                        'attributes' => [
                           // 'id',
                            'probability',
                            'risks',
                            'influence',
                            'del' => [
                                'asc' => ['risks.del' => SORT_ASC],
                                'desc' => ['risks.del' => SORT_DESC],
                            ],
                            'priority' => [
                                'asc' => ['priority' => SORT_ASC],
                                'desc' => ['priority' => SORT_DESC],
                                'label' => 'Priority',
                            ],
                            'proj' => [
                                'asc' => ['projects.name' => SORT_ASC],
                                'desc' => ['projects.name' => SORT_DESC],
                            ],
                            'author' => [
                                'asc' => ['users.name' => SORT_ASC],
                                'desc' => ['users.name' => SORT_DESC],
                            ]
                        ]
                    ]);
                
                    $this->load($params);
                
                    if (!$this->validate()) {
                        // uncomment the following line if you do not want to any records when validation fails
                        // $query->where('0=1');
                        return $dataProvider;
                    }
                
                
                    $query->andFilterWhere([
                        'id' => $this->id,
                        'proj_id' => $this->proj_id,
                        'author_id' => $this->author_id,
                        'influence' => $this->influence,
                        'probability' => $this->probability,
                        //'del' => $this->del,
                    ])
                        ->andFilterWhere(['like', 'projects.name', $this->proj])
                        ->andFilterWhere(['like', 'users.name', $this->author]);
                
                    $query->andFilterWhere(['like', 'risks', $this->risks]);
                
                    $query->having('priority = '. $this->priority);
                    //$query->having(['priority' => $this->priority]);
                
                    return $dataProvider;
                }
                

                推荐答案

                第 1 步:将 getter 函数添加到您的基本 Risks 模型:

                STEP 1: Add a getter function to your base Risks model:

                public function getPriority() {
                    return ($this->probability * $this->influence);
                }
                

                第 2 步:将属性 priority 添加到您的模型 RisksSearch 并配置您的规则.

                STEP 2: Add an attribute priority to your model RisksSearch and configure your rules.

                /* your calculated attribute */
                public $priority;
                
                /* setup rules */
                public function rules() {
                   return [
                       /* your other rules */
                       [['priority'], 'safe']
                   ];
                }
                

                第 3 步:编辑 search() 方法以包含计算字段 priority

                public function search($params) {
                
                    $query = Person::find();
                
                    $query->select('*, (probability * influence) as priority');
                
                    $dataProvider = new ActiveDataProvider([
                        'query' => $query,
                    ]);
                
                    /**
                     * Setup your sorting attributes
                     * Note: This is setup before $this->load($params)
                     */
                    $dataProvider->setSort([
                        'attributes' => [
                            'id',
                            'priority' => [
                                'asc' => ['priority' => SORT_ASC],
                                'desc' => ['priority' => SORT_DESC],
                                'label' => 'Priority',
                                'default' => SORT_ASC
                            ],
                        ]
                    ]);
                    ...
                

                第 4 步:在 $this->load($params) 之后添加 $query->andFilterWhere() 以便能够过滤计算出的字段

                STEP 4: Add $query->andFilterWhere() after $this->load($params) to be able to filter the calculated field

                // use the operator you wish, i.e. '=', '>', '<' etc
                $query->andFilterWhere(['=', '(probability * influence)', $this->priority]);
                

                这篇关于在 GridView Yii2 中对数据进行排序和过滤,其中列不在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <i id='i5GZ9'><tr id='i5GZ9'><dt id='i5GZ9'><q id='i5GZ9'><span id='i5GZ9'><b id='i5GZ9'><form id='i5GZ9'><ins id='i5GZ9'></ins><ul id='i5GZ9'></ul><sub id='i5GZ9'></sub></form><legend id='i5GZ9'></legend><bdo id='i5GZ9'><pre id='i5GZ9'><center id='i5GZ9'></center></pre></bdo></b><th id='i5GZ9'></th></span></q></dt></tr></i><div id='i5GZ9'><tfoot id='i5GZ9'></tfoot><dl id='i5GZ9'><fieldset id='i5GZ9'></fieldset></dl></div>
                1. <small id='i5GZ9'></small><noframes id='i5GZ9'>

                    <tbody id='i5GZ9'></tbody>

                        <bdo id='i5GZ9'></bdo><ul id='i5GZ9'></ul>

                        <legend id='i5GZ9'><style id='i5GZ9'><dir id='i5GZ9'><q id='i5GZ9'></q></dir></style></legend>
                        <tfoot id='i5GZ9'></tfoot>