How to generate Dynamic radio buttons in Yii advanced template? - radio-button

This is my controller:
$Shipping = Shipping::find()->all();
foreach ($Shipping as $key => $value) {
pr($value['name']);
}die;
O:P/
Free
Pickup
Country Wise
API
I want to generate radio button list in view with those O:P .How?

If the radio buttons are in active form
you can use active radioList
<?= $form->field($model, 'your_field')->
radioList(ArrayHelper::map( Shipping::find()->all(), 'your_id', 'your_name')); ?>

Related

Yii2: set text field's value when an item from a dropdownlist is selected

I have the following code on my form:
<?php
$unitPrices = ArrayHelper::map(Item::find()->where('isApproved and
vendor_id=:id', [':id' => $vendorId])->all(),'id','unitPrice');
?>
this gives me a map called unitPrices where the key-value pairing is id-unitPrice
<?= $form->field($model, 'item_id')->dropDownList(
ArrayHelper::map(Item::find()->where('isApproved and vendor_id=:id', [':id' => $vendorId])->all(),'id','itemCode','description'),
[
'prompt'=>'--Select Item--',
'id'=>'item_selected',
'onchange' => '$("#priceOnLine").val($unitPrices[item_id_value]);',
])
?>
what I want to do is when an item is selected from the dropdownlist, it also sets the value of a text field called priceOnLine using the unitPrices map where the id is the value of the selected item from the dropdownlist.
I tried setting the text field to a static value (100), and it works but I can't figure out how to set it using the map.
'onchange' => '$("#priceOnLine").val(100);',
Thanks in advance for the help :D
You need to get the text of the selected option you can add the following script on the top of your view and remove the 'onchange' => '$("#priceOnLine").val($unitPrices[item_id_value]);', from your dropDown()
<?php
$js=<<<JS
$("#item_selected").on("change",function(){
$("#priceOnLine").val(($(this).find("option:selected").text()));
});
JS;
$this->registerJs($js,\yii\web\View::POS_READY);
Hope it helps

Yii radioButtonList: radio button and item description are in separate lines

I've defined a radio button list in Yii as following:
<?php echo $form->radioButtonList($model, 'send_option', $email_exist); ?>
Here $email_exist variable is set in the controller.
The problem is radio button is in one line and item description is in another line. I want both are in the same line.
Any suggestions are appreciated.
Thank you in advance
Try to use template option , this example for checkBoxList:
<ul>
<?php echo CHtml::checkBoxList('checkBoxList','',$data, array(
'id'=>'checklist',
'name'=>'checklist',
'template'=>'<li>{input} {label}</li>',
));?>
</ul>
Note: it's same with radioButtonList

Tab visible on form save action in yii

How can i make the CTabview visible on form create/save action in Yii?
IN your form where you have this button. wrap it inside if()
eg:-
<?php if($model->isNewRecord): ?>
// your button code
<?php endif; ?>

Is it possible to have the same Model and CGrid parameter in a form

I have a form
$form=$this->beginWidget('CActiveForm', array('id'=>'subject-form','enableAjaxValidation'=>false,));
which has a parameter "Subject"
$form->labelEx($model,'subject');
$form->textField($model,'subject',array('size'=>200,'maxlength'=>255));
I would also like to add a "Subject Search Grid" further down the view (so I can search other Subjects as I'm editing the current). To that end I am passing $model into the view as normal, plus an alias $relatives to the grid.
$subject_search_grid= $this->widget(
'zii.widgets.grid.CGridView',
array(
'id'=>'subject-grid',
'dataProvider'=>$relatives->search(),
'filter'=>$relatives,
'columns'=>array(
'id',
'subject',
array('class'=>'CButtonColumn',),
),
),
$captureOutput=true
);
CGrid is working perfectly, however it's instance of 'subject' is overwriting the $form 'subject' when I try save or update the form. Is there any way of moving CGrid out of the view logic so it does not overwrite the value?
thanks
Place the gridview outside the form? So after <?php $this->endWidget(); ?>.

How to implement a dynamic js script in Yii List View?

Hello and thanks for reading my question. I have a typical list view:
<?php $this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'emptyText'=>'No Jobs',
)); ?>
In my _view file I have a div and a button that slideToggles the div. If I just put the Javascript at the top of the page, it does not work because the results are dynamic and the name of the div changes with the id returned, eg:
id="detailsDiv-<?php echo $data->id_employer_contract;?>"
The problem is in my Javascript, which is as follows:
<?php Yii::app()->clientScript->registerScript('details', "$('#details-$data-id_employer_contract').click(function(){
$('#detailsDiv-$data->id_employer_contract').slideToggle();
return false;});");?>
How can I make this Javascript code dynamic? Meaning, how can I loop through the id? I tried adding the code to the listview property ajaxUpdate but it's still not working. Can someone tell me how I can loop a Javascript in a list view?
Add the id to your toggle buttons as data attribute:
<button class="toggleDetails" data-id="<?php echo $data->id_employer_contract ?>">
Then you can access these data attributes like this js:
<?php Yii::app()->clientScript->registerScript('toggleDetails', "
$('.toggleDetails').click(function(e){
var id = $(this).data('id');
$('#detailsDiv-' + id).slideToggle();
e.preventDefault();
});
", CClientScript::POS_READY) ?>
NOTE: You should not put this javascript into _view.php but into the main file where you render the List View. You only need this one single snippet to deal with all your buttons.