Yii 2.0:对 GridView 中的列进行排序
Yii 2.0: Sorting on column in a GridView
$dataProvider = new ActiveDataProvider([
'query' => UserProfile::find()->joinWith(['user'])->where(['<>', 'user.status', 0]),
'sort' => ['attributes' => ['fullname',
'phone',
'user.username' => [
'asc' => ['user.username' => SORT_ASC],
'desc' => ['user.username' => SORT_DESC],
'default' => SORT_DESC
],
'user.email' => [
'asc' => ['user.email' => SORT_ASC],
'desc' => ['user.email' => SORT_DESC],
'default' => SORT_DESC
],
]]
]);
按 fullname
和 phone
排序效果很好。但我想在 username
和 email
列上添加排序。
请指正。
User 和 UserProfile table 有关系。 (user_profile.user_id = user.id)
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Username',
'value' => 'user.username',
],
[
'label' => 'Email',
'value' => 'user.email',
],
'fullname',
'address',
'phone',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
您需要添加以下代码。还在搜索模型中添加 public 个变量。并在 gridview
中使用这些变量作为属性名称。
public $username, $email;
public function rules()
{
return [
[['username', 'email'], 'safe'],
];
}
$dataProvider = new ActiveDataProvider([
'query' => UserProfile::find()->joinWith(['user'])->where(['<>', 'user.status', 0]),
'sort' => [
'defaultOrder' => ['user.id' => SORT_DESC]
],
]);
$dataProvider->sort->attributes['username'] = [
'asc' => ['user.username' => SORT_ASC],
'desc' => ['user.username' => SORT_DESC],
];
$dataProvider->sort->attributes['email'] = [
'asc' => ['user.email' => SORT_ASC],
'desc' => ['user.email' => SORT_DESC],
];
gridview
代码。
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Username', (if sort link not appear so, add comment label line)
'attribute' => 'username',
'value' => 'user.username',
],
[
'label' => 'Email', (if sort link not appear so, add comment label line)
'attribute' => 'email',
'value' => 'user.email',
],
'fullname',
'address',
'phone',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
$dataProvider = new ActiveDataProvider([
'query' => UserProfile::find()->joinWith(['user'])->where(['<>', 'user.status', 0]),
'sort' => ['attributes' => ['fullname',
'phone',
'user.username' => [
'asc' => ['user.username' => SORT_ASC],
'desc' => ['user.username' => SORT_DESC],
'default' => SORT_DESC
],
'user.email' => [
'asc' => ['user.email' => SORT_ASC],
'desc' => ['user.email' => SORT_DESC],
'default' => SORT_DESC
],
]]
]);
按 fullname
和 phone
排序效果很好。但我想在 username
和 email
列上添加排序。
请指正。
User 和 UserProfile table 有关系。 (user_profile.user_id = user.id)
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Username',
'value' => 'user.username',
],
[
'label' => 'Email',
'value' => 'user.email',
],
'fullname',
'address',
'phone',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
您需要添加以下代码。还在搜索模型中添加 public 个变量。并在 gridview
中使用这些变量作为属性名称。
public $username, $email;
public function rules()
{
return [
[['username', 'email'], 'safe'],
];
}
$dataProvider = new ActiveDataProvider([
'query' => UserProfile::find()->joinWith(['user'])->where(['<>', 'user.status', 0]),
'sort' => [
'defaultOrder' => ['user.id' => SORT_DESC]
],
]);
$dataProvider->sort->attributes['username'] = [
'asc' => ['user.username' => SORT_ASC],
'desc' => ['user.username' => SORT_DESC],
];
$dataProvider->sort->attributes['email'] = [
'asc' => ['user.email' => SORT_ASC],
'desc' => ['user.email' => SORT_DESC],
];
gridview
代码。
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Username', (if sort link not appear so, add comment label line)
'attribute' => 'username',
'value' => 'user.username',
],
[
'label' => 'Email', (if sort link not appear so, add comment label line)
'attribute' => 'email',
'value' => 'user.email',
],
'fullname',
'address',
'phone',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>