Catching element by ID when it's defined with twig - javascript

I've created some elements with php in order to made up a form. This is the snippet of PHP code:
$builder->add('path', FileType::class, array('label' => 'Submit', 'attr' => array('class' => 'style-1 btn_upload_pdf_php js-btn_upload_pdf', 'id' => 'pdf')));
$builder->add('title', TextType::class, array('label' => 'Flyer\'s name', 'attr' => array('class' => 'style-1 name_pdf js-name_pdf', 'placeholder' => 'Nome del volantino')));
$builder->add('expirationData', DateType::class, array('label' => 'scadenza', 'attr' => array('class' => 'style-1 deadline_pdf js-deadline_pdf', 'placeholder' => 'Nome del volantino', 'id' => 'deadline_pdf')));
I've tried to set path's ID by twig with the following statement:
{{ form_widget(form1.path, { 'id': 'pdf'}) }}
But when I try to get element ($('#pdf')) via JavaScript, it doesn't work. It seems that the element isn't created.
Thx.

to pass id to twig form, you have to do it like that :
{{ form_widget(form1.path, {'attr': {'id': 'pdf'}}) }}

Related

yii2 kartik switch input type radio button get value in javascript

I use the yii2 kartik switch input with the type radio and i want to get the value of the selected option in javascript (specially in a JsExpression), here is my code :
$model->orientation = 'Portrait';
echo $form->field($model, 'orientation')->widget(SwitchInput::classname(), [
'name' => 'information_orientation',
'type' => SwitchInput::RADIO,
'value' => 'Portrait',
'items' => [
['label' => 'Portrait&nbsp&nbsp&nbsp', 'value' => 'Portrait'],
['label' => 'Paysage&nbsp&nbsp&nbsp', 'value' => 'Paysage'],
],
'pluginOptions' => [
'onText' => 'Oui',
'offText' => 'Non',
'onColor' => 'success',
'offColor' => 'danger',
'size' => 'mini'
],
'labelOptions' => ['style' => 'font-size: 13px'],
]);
I have tried :
$([name='information-orientation']).val()
But it returned an undifined value
SwitchInput class does not care about the name property you give it
according to their docs, you need to wrap name in a options array
echo $form->field($model, 'orientation')
->widget(SwitchInput::classname(), [
'options' => ['name' => 'information_orientation'],
'type' => SwitchInput::RADIO,
....
from docs:
options: array the HTML attributes for the widget input tag.
edit:
you can then use the following
// to get value:
$("[name='information_orientation']").val()
// to check if the switch is or on off
$("[name='information_orientation']").prop('checked')

Html datalist values from array from controller

I'm trying to make an input in HTML that when you write something it show you some possibilities. This possibilities are sent by my controller with an array.
This is my view:
<div class="form-group">
{!!Form::label('marca','Marca: ')!!}
{!!Form::text('marca',null, ['id' => 'marca', 'class' => 'form-control', 'onkeyup'=>'javascript:this.value=this.value.toUpperCase();', 'list'=>'lista'])!!}
<datalist id="lista"></datalist>
<br>
{!!Form::label('modelo','Modelo: ')!!}
{!!Form::text('modelo',null, ['id' => 'modelo', 'class' => 'form-control', 'placeholder' => 'Ingresa el modelo'])!!}
<br>
{!!Form::label('part_number','Part Number: ')!!}
{!!Form::text('part_number',null, ['id' => 'pn', 'class' => 'form-control', 'placeholder' => 'Ingresa el part number','onkeyup'=>'javascript:this.value=this.value.toUpperCase();'])!!}
<br>
{!!Form::label('coste','Coste: ')!!}
{!!Form::number('coste',null, ['id' => 'coste', 'step'=>'any', 'class' => 'form-control', 'placeholder' => 'Ingresa el coste del equipo'])!!}
<br>
{!!Form::label('caracteristicas','Características: ')!!}
{!!Form::text('caracteristicas',null, ['id' => 'caract', 'class' => 'form-control', 'placeholder' => 'Ingresa las características necesarias del part number'])!!}
</div>
<script type="text/javascript">
var marcas = $array;
var list = document.getElementById('lista');
marcas.forEach(function(item){
var option = document.createElement('option');
option.value = item;
list.appendChild(option);
});
This is my Controller:
public function create()
{
$marcas = Modelos::Marcas();
$array = array();
foreach ($marcas as $marca) {
$array[] = $marca->marca;
}
return view('modelos.create',compact('array'));
}
The console gives me the following error:
{Uncaught ReferenceError: $array is not defined at create:241}

How to draw a CGridView after document ready?

How to draw a CGridView after document ready? I have Yii framework 1.14, jquery and jquery.ui successfully loaded!
Console.error! Because the grid is draw after document ready
TypeError: jQuery(...).dialog is not a function
jQuery("#dialog").dialog();
My code:
[
'class' => 'zii.widgets.grid.CButtonColumn',
'template' => '{edit}',
'headerHtmlOptions' => ['class' => 'col-lg-1', 'style' => 'text-align:center'],
'buttons' => [
'edit' => [
'label' => '<span class="glyphicon glyphicon-comment"></span>',
'url' => "Yii::app()->controller->createUrl('ViewComments',['key'=>\$data->key])",
'imageUrl' => null,
'options' => [
'style' => 'color: black',
'rel' => 'tooltip',
'data-toggle' => 'modal',
'data-target' => "#myModal",
'title' => 'Комментировать',
'ajax' => [
'type' => 'post',
'url' => 'js:$(this).attr("href")',
'dataType' => 'html',
'success' => 'js:function(data){
jQuery("#dialog").dialog();
}'
],
],
],
]
],
Have you tried reloading js in your view
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerCoreScript('jquery.ui');

Retrieve data in Select Option in Edit Page

I'm using Laravel 4. I'm having some problem. I have a users table with fields like. State, Region, Area and City, they are all in string (varchar).
Now, I have my "Edit Users" page. In edit users how can I display the users info ing State, Region etc. in a Select Dropdown? Here's my code.
{{ Form::select('state', array(
'' => 'Choose One', 1 => 'Luzon', 2 => 'Visayas', 3 => 'Mindanao'),
null,
array('class' => 'selectpicker',
'id' => 'state',
'data-live-search' => 'true',
'onchange' => 'document.getElementById("state2").value=this.options[this.selectedIndex].text'
));
}}
How can I make a value for that specific user already appeared or selected in the dropdown?
Thanks!
See if this works for you:
$states = array( 'Choose One', 'Luzon', 'Visayas', 'Mindanao' );
{{ Form::select('state',
$states,
array_search($user->State, $states ), // default value
array(
'class' => 'selectpicker',
'id' => 'state',
'data-live-search' => 'true',
'onchange' => 'document.getElementById("state2").value=this.options[this.selectedIndex].text'
));
}}

How can I use callback use x-editable like yii extension

I try use callback. If data success update then i need to display modal window. But its not working! Help please! I dont know, how it works. Write me please.
In View
<?php
$this->widget('editable.EditableField', array(
'type' => 'select',
'params' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
'model' => $model,
'attribute' => 'category_id',
'url' => $this->createUrl('course/updateSameInfo'),
'source' => Editable::source(Coursecat::model()->findAll(), 'id', 'name'),
'placement' => 'right',
));
?>
In Controller
public function actionUpdateSameInfo()
{
$es = new EditableSaver('Course'); //'User' is name of model to be updated
$es->update();
}
Use success property. Here is the documentation http://x-editable.demopage.ru/index.php?r=site/widgets#Options
Try this
<?php
$this->widget('editable.EditableField', array(
'type' => 'select',
'params' => array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
'model' => $model,
'attribute' => 'category_id',
'url' => $this->createUrl('course/updateSameInfo'),
'source' => Editable::source(Coursecat::model()->findAll(), 'id', 'name'),
'placement' => 'right',
'success' => 'js: function(response, newValue) {
console.log(response); //Open the browser console to check the data
}'
));
?>

Categories

Resources