如果我在db中有2个字段 - 概率和影响力,我需要在GridView中使用这两个字段相乘的列。我设法将它添加到那里:
[
'attribute' => 'priority',
'format' => 'raw',
'value' => function ($model) {
return $model->influence * $model->probability;
},
],
但无法处理排序,因为该列不在db中,并且向$ query添加过滤器只会导致错误。
$query = Risks::find();
$query->select(`probability*influence AS priority`);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
已更新(使用Asc和Desc但不使用过滤器)
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;
}
答案 0 :(得分:10)
第1步:为您的基础Risks
模型添加一个getter函数:
public function getPriority() {
return ($this->probability * $this->influence);
}
第2步:向模型priority
添加属性RisksSearch
并配置规则。
/* 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步:在$query->andFilterWhere()
之后添加$this->load($params)
,以便能够过滤计算字段
// use the operator you wish, i.e. '=', '>', '<' etc
$query->andFilterWhere(['=', '(probability * influence)', $this->priority]);
答案 1 :(得分:2)
删除
$query->select('*, (probability * influence) as priority');
改变
'priority' => [
'asc' => ['(probability * influence)' => SORT_ASC],
'desc' => ['(probability * influence)' => SORT_DESC],
'label' => 'Priority',
],
之后$ this-&gt;加载($ params);
$query->andFilterWhere(['=', '(probability * influence)', $this->priority]);
搜索按需运行! :)
感谢您的帮助!
答案 2 :(得分:0)
这是我的解决方案,适用于对非表字段进行排序
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<ImageView
android:id="@+id/img"
android:layout_width="@dimen/_200sdp"
android:layout_height="@dimen/_200sdp"
android:layout_marginTop="16dp"
android:src="@drawable/ic_launcher"
app:layout_constraintBottom_toTopOf="@+id/btn1"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/left"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintBottom_toTopOf="@+id/btn2"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/left"
app:layout_constraintTop_toBottomOf="@+id/img" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintBottom_toTopOf="@+id/txt_already_member"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/left"
app:layout_constraintTop_toBottomOf="@+id/btn1" />
<TextView
android:id="@+id/txt_already_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Already Member? Sign In"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+id/left"
app:layout_constraintTop_toBottomOf="@+id/btn2" />
<TextView
android:id="@+id/txt_agreement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="By continuing you agree to our terms and privacy policy"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintStart_toStartOf="@+id/left"
app:layout_constraintTop_toTopOf="@+id/divider"
app:layout_constraintVertical_bias="1.0" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="@dimen/_16sdp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="@dimen/_16sdp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="0.7"
app:layout_constraintTop_toBottomOf="@+id/txt_already_member" />