How to use selected value in Html::dropDownList with optgroups in Yii2? - yii2

in Yii2 I’m trying to choose a specific option with an Html::dropDownList that uses optgroup. I’m able to create the dropdown html just fine using a nested array but how do I select one of the choices? What do I use as the $selection value?
Here is the line of code that I'm currently using. If leave the selection blank, it all works fine, but as soon as I try to select a specific value, I can't get it to work.
echo Html::dropDownList(
'criteria',
['Page 1']['107141'],
[
'Page 1' => [
'107145' => 'Q1: some text',
'107141' => 'Q9: some text',
'107142' => 'Q10: some text',
'107143' => 'Q11: some text',
'107164' => 'Q14: some text',
],
'Page 2' => [
'107195' => 'some text',
],
],
[
'prompt' => [
'text' => 'Choose criteria',
'options' => []
],
'id' => 'criteriaSelector',
'class' => 'criteriaSelector',
'required' => 'required',
]
);
Here is the Yii documentation for the dropdownlist but I can't figure out what to do next:
https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#dropDownList()-detail
Thank you!

For some reason I kept thinking that the the selection needed to be an array element but, of course, since the dropdown option name has to be unique, the selection only needs to be a string. Here is the solution:
echo Html::dropDownList(
'criteria',
'107141',
[
'Page 1' => [
'107145' => 'Q1: some text',
'107141' => 'Q9: some text',
'107142' => 'Q10: some text',
'107143' => 'Q11: some text',
'107164' => 'Q14: some text',
],
'Page 2' => [
'107195' => 'some text',
],
],
[
'prompt' => [
'text' => 'Choose criteria',
'options' => []
],
'id' => 'criteriaSelector',
'class' => 'criteriaSelector',
'required' => 'required',
]
);

Related

Wrong rendering search field kartik select2

I use the select2 widget from kartik:
echo $form->field($model, 'person_ids')->widget(Select2::classname(), [
'data' => [1 => 'test1', 2 => 'test2'],
'theme' => Select2::THEME_KRAJEE_BS5,
'hideSearch' => true,
'options' => [
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
])->label('Personal');
The rendering of the search field is wrong - it intends the selected options. Any ideas how to fix this?

Kartik Gridview Switch inputType

I've been looking into this all day, and I'm just wondering if there is built in functionality for Kartik EditableColumn in regards to inputType.
While one can easily apply the SwitchInput to filterType - I can't seem to find a way to handle it in column. It is simply a boolean that I would like handled by this toggle.
While this code produces what I am looking for (aesthetically), I can't produce the expected functionality without strapping it to some JS.
[
//'class' => 'kartik\grid\EditableColumn',
'label' => 'Public',
'hAlign' => 'center',
'vAlign' => 'middle',
'mergeHeader' => true,
'format' => 'raw',
'value' => function ($model) {
return SwitchInput::widget([
'name' => 'test',
'value' => $model->public,
'pluginOptions' => [
'size' => 'mini',
'onText' => 'ON',
'offText' => 'OFF',
],
'labelOptions' => ['style' => 'font-size: 12px;'],
]);
}
],
Produces:

Editable row with number type

How can i make this input type with number. I mean when the page is opened in mobile phone, it'll be open the number keyboard.
[
'class'=>'kartik\grid\EditableColumn',
'headerOptions' => ['style' => 'width:10%', 'class'=>'text-center'],
'editableOptions'=>[
'asPopover' => false,
'inputType'=>\kartik\editable\Editable::INPUT_TEXT,
// Change here:
'editableValueOptions'=>['type'=>'number']
],
'attribute'=>'quantity',
'label'=>'Quantity',
],
EDIT ->> add 'editableValueOptions'=>['type'=>'number']
Use editableValueOptions. As the documentation says:
editableValueOptions: array, the HTML attributes for the editable value displayed.
[
'class'=>'kartik\grid\EditableColumn',
'headerOptions' => ['style' => 'width:10%', 'class'=>'text-center'],
'editableOptions'=>[
'asPopover' => false,
'inputType'=>\kartik\editable\Editable::INPUT_TEXT,
// Change here:
'editableValueOptions'=>['type'=>'number']
],
'attribute'=>'quantity',
'label'=>'Quantity',
],
'attribute' => 'quantity',
'class' => 'kartik\grid\EditableColumn',
'editableOptions'=>[
'valueIfNull' => 'not set',
'inputType' => \kartik\editable\Editable::INPUT_HTML5,
'options' => [
'type' => 'number',
'min' => '0.5',
'step' => '0.5',
],
],
You should use options array as shown above

Yii2, set default value for select2 widget

use kartik\widgets\Select2;
echo Select2::widget([
'model' => $myModel,
'name' => 'company[]',
'options' => [
'placeholder' => 'Select a company ...',
'multiple' => true,
],
'value' => 6, //doesn't work
'initValueText' => '6', //doesn't work
'pluginOptions' => [
'allowClear' => true,
'ajax' => [
'url' => Url::to(['/company/default/get-company-list']),
'dataType' => 'json',
'data' => new JsExpression('function(term,page) {
return {term : term.term};
}'),
'results' => new JsExpression('function(data,page) {return {results:data.results}; }'),
],
'initSelection' => new JsExpression('function(element, callback) {
$(element).val(6); //doen't work
callback({"text" : "Vendor B", "id" : 6}); // it does only set text, not id
}'),
],
]);
... many many select2 form below too, that named 'company[]'
After form submit, if user come back to this page, I want to set what user selected as default.
How can I set default value for Select2 widget?
When you update the model, it will automatically have the last selected value.
$myModel = new \app\models\myModel;
$myModel->attributes = \Yii::$app->request->post('myModel');

Yii2 kartik typeahead - get number of suggestions

I'm using kartik's typeahead widget for Yii2 in a view:
echo \kartik\typeahead\Typeahead::widget([
'name' => 'serial_product',
'options' => [
'placeholder' => 'Filter as you type ...',
'autofocus' => "autofocus"
],
'scrollable' => TRUE,
'pluginOptions' => [
'highlight' => TRUE,
'minLength' => 3
],
'dataset' => [
[
'remote' => Url::to(['transfers/ajaxgetinventoryitemsnew']) . '?search=%QUERY',
'limit' => 10
]
],
'pluginEvents' => [
"typeahead:selected" => "function(obj, item) { add_item(item.id); return false;}",
],
]);
How can i get the number of loaded suggestions after the remote dataset is retrieved to execute a javascript function like:
displaynumber(NUMBEROFSUGGESTIONS);
After checking through the source of kartiks widget i came up with the following solution:
echo \kartik\typeahead\Typeahead::widget([
'name' => 'serial_product',
'options' => [
'placeholder' => 'Filter as you type ...',
'autofocus' => "autofocus",
'id' => 'serial_product'
],
'scrollable' => TRUE,
'pluginOptions' => [
'highlight' => TRUE,
'minLength' => 3
],
'dataset' => [
[
'remote' => [
'url' => Url::to(['transfers/ajaxgetinventoryitemsnew']) . '?search=%QUERY',
'ajax' => ['complete' => new JsExpression("function(response)
{
jQuery('#serial_product').removeClass('loading');
checkresult(response.responseText);
}")]
],
'limit' => 10
]
],
'pluginEvents' => [
"typeahead:selected" => "function(obj, item) { checkresult2(item); return false;}",
],
]);
where response.responseText is containing the response from server (json).
function checkresult(response) {
var arr = $.parseJSON(response);
console.log(arr.length);
}
With this function i can get then count of suggestions delivered from server.