Hi , I’m using this extension http://demos.krajee.com/widget-details/select2#settings , I want that when I select one , I update another field simultaneously with other data ( in this case would prod_valv ) .
this is my code
controller
public function actionAuto($search = null, $id = null) {
header('Content-type: application/json');
$out = ['more' => false];
if (!is_null($search)) {
$query = new Query;
$query->select(['prod_desc AS text', 'prod_codi AS id'])
->from('ge_tprod')
->where('prod_desc LIKE "%' . $search .'%" ' )
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$prod= GeTprod::find()->where(['prod_codi' => $id])->one();
$out['results'] = ['id' => $id, 'text' => $prod->prod_desc, 'prod_valv' => $prod->prod_valv,];
}
else {
$out['results'] = ['id' => 0, 'text' => 'error'];
}
echo Json::encode($out);
}
Form
<?php
// The controller action that will render the list
$url = \yii\helpers\Url::to(['/Facturacion/ge-tprod/auto/']);
// Script to initialize the selection based on the value of the select2 element
$initScript = <<< SCRIPT
function (element, callback) {
var id=\$(element).val();
if (id!=="") {
// alert('id not null' + id);
\$.ajax("{$url}&id=" + id, {
dataType: "json"
}).done(function(data) { callback(data.results);});
}
}
SCRIPT;
// The widget
echo $form->field($model, 'prod_codi')->widget(Select2::classname(), [
'language' => es,
'options' => ['placeholder' => 'Buscar Producto ...'],
'pluginOptions' => [
'escapeMarkup' => new JsExpression("function(m) { return m; }"),
'allowClear' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => $url,
'dataType' => 'json',
'data' => new JsExpression('function(term,page) { return {search:term}; }'),
'results' => new JsExpression('function(data,page) { return {results:data.results}; }'),
],
'initSelection' => new JsExpression($initScript)
],
]);
?>