How to display multpiple values in one field.. I Use Select2 Widget. If i use $courses_model[0] it display only one value
Controller
public function actionUpdateteachers($id)
{
$courses_model = ReferenceTeachersCourses::find()->where(['reference_teachers_id' => $id])->all();
.....
}
View
...
<?= $form->field($courses_model[0], 'reference_course_type_id')->widget(Select2::classname(), [
'data' =>ArrayHelper::map($courses,'id','name'),
'options' => ['multiple' => true],
'pluginOptions' => [
'allowClear' => true,
],
]);
...
?>
This widgets works fine - but your $courses_model[0]->reference_course_type_id must have an array of ids as a value if you want to see multiple values selected.
Related
I have the following:
Html::a('Link', ['some/route'], [
'class' => 'btn btn-lg btn-primary', // WORKS
'style' => 'padding: 100px;', // WORKS
'data-id' => 123, // DOES NOT WORK
'data' => [
'id' => 123, // DOES NOT WORK
],
]);
As per docs, both of the specified data-* attributes in Html::a helper should render their respective attributes in the HTML output, but they do not, and I do not understand why.
Yii 2 documentation on renderTagAttributes also states the following:
Renders the HTML tag attributes.
Attributes whose values are of boolean type will be treated as boolean
attributes.
Attributes whose values are null will not be rendered.
The values of attributes will be HTML-encoded using encode().
The "data" attribute is specially handled when it is receiving an
array value. In this case, the array will be "expanded" and a list
data attributes will be rendered. For example, if 'data' => ['id' =>
1, 'name' => 'yii'], then this will be rendered: data-id="1"
data-name="yii". Additionally 'data' => ['params' => ['id' => 1,
'name' => 'yii'], 'status' => 'ok'] will be rendered as:
data-params='{"id":1,"name":"yii"}' data-status="ok".
EDIT: I am trying to do this inside GridView column.
Okay, since I have used Html::a inside a GridView column, you will have to change the output format of that column. html will not work for data attributes, so you will need to switch to raw:
[
'label' => 'Actions',
'format' => 'raw',
'value' => function($model) {
return Html::a('Link', ['some/route'], [
'class' => 'btn btn-lg btn-primary', // WORKS
'style' => 'padding: 100px;', // WORKS
'data-id' => 123, // WORKS
'data' => [
'id-second' => 123, // WORKS
],
]);
},
]
I am using a Select2 widget for Yii2. It shows a list with the ids of the users.
I need to show two fields defined in my model called Users: first_name_user and last_name_user. Like this:
Daniel Gates
John Connor
John Doe
Maria Key
But I don't know how use map() to show more than one field.
<?= $form
->field($model, 'id_user')
->widget(\common\widgets\Select2::classname(), [
'items' => \yii\helpers\ArrayHelper::map(\app\models\Users::find()->orderBy('name_user')->all(), 'id_user', 'name_user')
])
?>
Model
Add use app\models\Users; and use yii\helpers\ArrayHelper; at top.
public function userList()
{
$userList = [];
$users = Users::find()->orderBy('first_name_user')->all();
$userList = ArrayHelper::map($users, 'id_user', function ($user) {
return $user->first_name_user.' '.$user->last_name_user;
});
return $userList;
}
_form
<?= $form->field($model, 'id_user')->widget(Select2::className(), [
'data' => $model->userList(),
'options' => ['placeholder' => 'Select User'],
]) ?>
You need to use data option instead of items for Select2.
You need to modify your query to show the concatenated first_name_user and last_name_user as an alias and then return it along with the id column to be used in Select2 by ArrayHelper::map().
It's better to add a function to the model you are using to populate the form and return the results from there to the ArrayHelper::map().
Your query should look like
function userList(){
return \app\models\Users::find()
->select([new \yii\db\Expression('[[id_user]],CONCAT([[first_name_user]]," ",[[last_name_user]]) as full_user_name')])
->orderBy('name_user')
->all();
}
Your form field should look like below
<?=
$form->field($model, 'id_user')->widget(Select2::className(), [
'data' => \yii\helpers\ArrayHelper::map($model->userList(), 'id_user', 'full_user_name'),
'options' => [
'placeholder' => 'Select User',
'id' => 'id_user'
],
'theme' => Select2::THEME_DEFAULT,
'pluginOptions' => [
'allowClear' => true
],
]);
?>
This is my form. I have save value when I create but then when I update the value wont appear.
$form->field($model, 'unit_id')->widget(Select2::classname(), [
'data' => $getunit,
'language' => 'en',
'value'=>$model->unit_id, //cant work
'initValueText'=>$model->unit_id //cant work
'options' => ['placeholder' => 'Select','multiple'=>true,'value'=>$model->unit_id' //cant work ],
'pluginOptions' => [
'allowClear' => true,
'tags'=>true,
'maximumInputLength'=>10,
],
])->label(false)
Anyone know why? I search through online but I still cant solve it
In select2 widget of yii2, how can we make an ajax call from the widget to a function in our controller:
Scenario is I need to create a custom ID for a table the id depends upon the two dropdown value and on select event of the select2 drop down I need to fetch the record and construct the ID and put the value of the newly created id in the form filed.
I just have problem in making an ajax call from the select2 dropdown widget
Try following:
You can use select2:select event to make ajax call.
echo $form->field($model, 'state_1')->widget(Select2::classname(), [
'data' => $data,
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"select2:select" => "function() { // function to make ajax call here }",
]
]);
'pluginEvents' => [
'change' => 'function() {
var selectedIds = $(this).val();
$.pjax.reload({container: "#testing", data:{tags:selectedIds}});
}'
Updates have been made below
I am trying to use the Kartik-V Typeahead Basic widget with the Yii2 Framework.
The code below is working to display the required data, the user can search via the university name and it appears in the autocomplete list.
The issue is, the model needs to the university id, not the name. Thus the rules are this field can only store an integer and returns a validation error once you select one of the typeahead results.
<?= $form->field($model, 'university_id')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
I am hoping someone can help me understand if there is a setting that needs to be changed so when saving, the user friendly 'uni_name' data is changed back to the uni 'id'.
UPDATE:
I have gotten the code partly working thanks to "Insane Skull".
The new code is:
<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...', 'id' => 'testID'],
'pluginEvents' => [
'typeahead:select' => new yii\web\JsExpression("function(event, ui) { $('#testing123').val(ui.item.id); }"),
]
]); ?>
<?= Html::activeHiddenInput($model, 'university_id', array ('id' => 'testing123'))?>
Now I am unfortunately getting the error:
Method yii\web\JsExpression::__toString() must return a string value
I would rather use Select2 instead of Typeahead, you are basically trying to implement the functionality that already exists on Select2 but using Typeahead.
<?= $form->field($model, 'university_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
You can use activeHiddenInput() for this purpose.
Create one public variable in model say name.
Then:
<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
'select' => new yii\web\JsExpression("function( event, ui ) {
$('#id_of_hiddenField').val(ui.item.id);
}")
]); ?>
<?= Html::activeHiddenInput($model, 'university_id')?>
And in Controller Get the value of activeHiddenField.