kartik\Select2 作为 yii2\grid 中的过滤器输入
kartik\Select2 as filter input in yii2\grid
我的 Yii2 项目遇到了另一个虚拟问题。在我看来,我有一个标准的 gridView,定义如下:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{items}{summary}',
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => [
'style' => 'vertical-align: middle;'
]
],
[
'attribute' => 'name',
],
[
'attribute' => 'type',
'value' => function($model){
/* @var $model app\models\Object */
return $model->typeNames()[$model->type];
},
'filter' => Select2::widget([
'name' => 'ObjectSearch[type]',
'data' => Object::typeNames(),
'theme' => Select2::THEME_BOOTSTRAP,
'hideSearch' => true,
'options' => [
'placeholder' => 'Wybierz typ obiektu...',
'value' => isset($_GET['ObjectSearch[type]']) ? $_GET['ObjectSearch[type]'] : null
]
]),
],
[
'attribute' => 'countryId',
'value' => 'country.name',
'filter' => Select2::widget([
'name' => 'ObjectSearch[countryId]',
'data' => Country::forWidgets(),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => 'Wybierz państwo...'
]
]),
],
//other columns
],
]); ?>
我定义了一个 searchModel class:
class ObjectSearch extends Object
{
public function rules()
{
return [
[['type'], 'integer'],
[['name', 'city', 'countryId'], 'string']
];
}
//some other functions
public function search($params)
{
$query = Object::find()->where(['userId' => Yii::$app->user->id]);
$query->joinWith('country');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['countryId'] = [
'asc' => ['countries.name' => 'ASC'],
'desc' => ['countries.name' => 'DESC']
];
if (!$this->load($params) && $this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'objects.name', $this->name])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['=', 'objects.countryId', $this->countryId])
->andFilterWhere(['=', 'type', $this->type]);
return $dataProvider;
}
}
排序和搜索工作正常 - 我得到了正确的结果。那我的问题是什么?对于标准列,当我在 textInput 中键入一些文本时,此输入的值在搜索后保留在其中。但是当我在 Select2 小部件搜索工作中选择一些值时,但在搜索之后所选值消失并且我只有一个占位符。
感谢您的帮助,
卡米尔
你应该简单地使用 initValueText
参数 :
initValueText
: string, the text to displayed in Select2 widget for the initial value.
例如:
Select2::widget([
'name' => 'ObjectSearch[type]',
'data' => Object::typeNames(),
'initValueText' => $searchModel->type,
// ... other params
])
您也可以将其用作 InputWidget :
Select2::widget([
'model' => $searchModel,
'attribute' => 'type',
'data' => Object::typeNames(),
// ... other params
])
我的 Yii2 项目遇到了另一个虚拟问题。在我看来,我有一个标准的 gridView,定义如下:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => '{items}{summary}',
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => [
'style' => 'vertical-align: middle;'
]
],
[
'attribute' => 'name',
],
[
'attribute' => 'type',
'value' => function($model){
/* @var $model app\models\Object */
return $model->typeNames()[$model->type];
},
'filter' => Select2::widget([
'name' => 'ObjectSearch[type]',
'data' => Object::typeNames(),
'theme' => Select2::THEME_BOOTSTRAP,
'hideSearch' => true,
'options' => [
'placeholder' => 'Wybierz typ obiektu...',
'value' => isset($_GET['ObjectSearch[type]']) ? $_GET['ObjectSearch[type]'] : null
]
]),
],
[
'attribute' => 'countryId',
'value' => 'country.name',
'filter' => Select2::widget([
'name' => 'ObjectSearch[countryId]',
'data' => Country::forWidgets(),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => 'Wybierz państwo...'
]
]),
],
//other columns
],
]); ?>
我定义了一个 searchModel class:
class ObjectSearch extends Object
{
public function rules()
{
return [
[['type'], 'integer'],
[['name', 'city', 'countryId'], 'string']
];
}
//some other functions
public function search($params)
{
$query = Object::find()->where(['userId' => Yii::$app->user->id]);
$query->joinWith('country');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['countryId'] = [
'asc' => ['countries.name' => 'ASC'],
'desc' => ['countries.name' => 'DESC']
];
if (!$this->load($params) && $this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'objects.name', $this->name])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['=', 'objects.countryId', $this->countryId])
->andFilterWhere(['=', 'type', $this->type]);
return $dataProvider;
}
}
排序和搜索工作正常 - 我得到了正确的结果。那我的问题是什么?对于标准列,当我在 textInput 中键入一些文本时,此输入的值在搜索后保留在其中。但是当我在 Select2 小部件搜索工作中选择一些值时,但在搜索之后所选值消失并且我只有一个占位符。
感谢您的帮助,
卡米尔
你应该简单地使用 initValueText
参数 :
initValueText
: string, the text to displayed in Select2 widget for the initial value.
例如:
Select2::widget([
'name' => 'ObjectSearch[type]',
'data' => Object::typeNames(),
'initValueText' => $searchModel->type,
// ... other params
])
您也可以将其用作 InputWidget :
Select2::widget([
'model' => $searchModel,
'attribute' => 'type',
'data' => Object::typeNames(),
// ... other params
])