kartik-v / yii2-builder

Build forms (single or tabular) easily for Yii Framework 2.0.
http://demos.krajee.com/builder
Other
100 stars 50 forks source link

TabularForm widget with Select2 Ajax call: How set inital value? #136

Closed strtob closed 6 years ago

strtob commented 6 years ago

Hi there,

I'm quite new to yii2. Great work from kartik!

How can I set the initalValue of a select2 inside the TabularForm widget?

see below ('initValueText' => ##### HOW TO SET #####?)

Thanks a lot! T

================

echo TabularForm::widget([ 'dataProvider' => $dataProvider, 'formName' => 'GeoAreaHasCity', 'checkboxColumn' => false, 'actionColumn' => false, 'attributeDefaults' => [ 'type' => TabularForm::INPUT_TEXT, ], 'attributes' => [ "id" => ['type' => TabularForm::INPUT_HIDDEN, 'columnOptions' => ['hidden'=>true]], 'tbl_geo_city_id' => [ 'label' => 'City', 'type' => TabularForm::INPUT_WIDGET, 'widgetClass' => \kartik\widgets\Select2::className(),
'options' => [ 'initValueText' => ##### HOW TO SET #####? }, //\app\models\GeoCity::findone('tbl_geo_city_id'), //'data' => \yii\helpers\ArrayHelper::map(\app\models\GeoCity::find()->orderBy('name')->asArray()->all(), 'id', 'name'), 'options' => [ 'placeholder' => Yii::t('app', 'Choose City'), ], 'pluginOptions' => [ 'allowClear' => true, 'minimumInputLength' => 3, 'language' => [ 'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"), ], 'ajax' => [ 'url' => $url, 'dataType' => 'json', 'data' => new JsExpression('function(params) { return {q:params.term}; }') ], 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), 'templateResult' => new JsExpression('function(city) { return city.text; }'), 'templateSelection' => new JsExpression('function (city) { return city.text; }'), ], ], 'columnOptions' => ['width' => '200px'],
], 'description' => ['type' => TabularForm::INPUT_TEXT], 'del' => [ 'type' => 'raw', 'label' => '', 'value' => function($model, $key) { return Html::hiddenInput('Children[' . $key . '][id]', (!empty($model['id'])) ? $model['id'] : "") . Html::a('', '#', ['title' => Yii::t('app', 'Delete'), 'onClick' => 'delRowGeoAreaHasCity(' . $key . '); return false;', 'id' => 'geo-area-has-city-del-btn']); }, ],`

strtob commented 6 years ago

This is my solution base on https://stackoverflow.com/questions/31694113/yii2-select2-how-to-set-initvaluetext-in-gridview-or-tabularform?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

with thanks to markux

I'd no clue about to start with the inner function (syntax)

`'tbl_geo_city_id' => [ 'label' => 'City', 'type' => TabularForm::INPUT_WIDGET, 'widgetClass' => \kartik\widgets\Select2::className(),

        'options' => function($model, $key, $index, $widget) {
                $initValueText = empty(\app\models\GeoCity::findOne($model['tbl_geo_city_id'])->name) ? '' : \app\models\GeoCity::findOne($model['tbl_geo_city_id'])->name;
                return [
                    'name' => 'test',
                    'options' => [
                        'class' => 'test-to-select',
                    ],
                    'initValueText' => $initValueText,

                    'options' => [
                        'placeholder' => Yii::t('app', 'Choose City'),
                    ],

                    'pluginOptions' => [
                            'allowClear' => true,
                            'minimumInputLength' => 3,
                            'language' => [
                                'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
                            ],
                            'ajax' => [
                                'url' => \yii\helpers\Url::to(['/geo-city/list']),
                                'dataType' => 'json',
                                'data' => new JsExpression('function(params) { return {q:params.term}; }')
                            ],
                            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                            'templateResult' => new JsExpression('function(city) { return city.text; }'),
                            'templateSelection' => new JsExpression('function (city) { return city.text; }'),
                    ],
                ];
        },

        `