YII2 Insert records selected from one grid to another - php

i'm new in Yii2, previously i use self-made-mvc-php + extjs 4.2
So i working on Yii2 now, i'm starting to get the hang of it for simple CRUD, my pobrem starts when i need to make entry form for parent-child tables.
I have 3 tables
commission
commission_id
commission_name
commission_amount
commission_percent
commission_scheme
cscheme_id
cscheme_name
cscheme_amount
cscheme_percent
cscheme_bonus
cscheme_description
commission_scheme_detail
cscheme_detail_id
commission_id
cscheme_id
The entry form is for commission_scheme and commission_scheme_detail.
I have generated all the crud using gii
i modify _form.php, add a gridview of commission_scheme_detail after the active form
i also add modal containing selection grid from commission
this is _form.php
gridview
<?php Pjax::begin(['id' => 'pjaxCschemedetail',
'timeout' => false,
'enablePushState' => false,
'clientOptions' => ['method' => 'POST']
]);
echo GridView::widget([
'id' => 'gridCschemedetail',
'dataProvider' => $detailData,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'CSCHEME_DETAIL_ID',
'CSCHEME_ID',
'COMMISSION_ID',
[
'class' => 'yii\grid\ActionColumn',
'buttons' => [
'update' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', Yii::$app->urlManager->createUrl(['commission-scheme-detail/view','id' => $model->CSCHEME_DETAIL_ID,'edit'=>'t']), [
'title' => Yii::t('yii', 'Edit'),
]);}
],
],
],
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
'panel' => [
'type'=>'info',
'before'=>
'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset List', ['index'], ['class' => 'btn btn-info']),
Html::button('Add', [
'id' => 'addDetailButton',
'class' => 'glyphicon glyphicon-plus btn btn-success btn-ajax-modal',
'value' => Url::to('#web/commission/listselect'),
'data-target' => '#modal_cschemedetail',
]),
'showFooter'=>false
],
]); Pjax::end();
Modal
Modal::begin([
'id' => 'modal_cschemedetail',
'header' => '<h4>Category</h4>',
]);
echo '<div id="modal-content"></div>';
echo Html::button('Add Selected', [
'id' => 'addCommissionsButton',
'class' => 'btn btn-success btn-ajax-modal'
]);
Modal::end();
?>
</div>
</div>
<div class="box-body">
</div>
</div>
selection grid
<?php Pjax::begin(); echo GridView::widget([
'id' => 'gridCommissionSelection',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'COMMISSION_ID',
'visible' => false
],
'COMMISSION_NAME',
[
'attribute' => 'TRANSACTION_TYPE_ID',
'value' => 'transactionTypeName'
],
'COMMISSION_DESC:ntext',
'COMMISSION_AMOUNT',
]
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
]); Pjax::end(); ?>
Is it possible to insert the selected records from the modal to the detail gridview, then after user click the create button, the form and the inserted records in gridview saved to respective tables?
How can i insert the selected records to gridview (not to db table, it will be done after user click create)?

I suggest you to simplify your flow. You can easily save your models (performing an insert/update) at Modal and then refresh the page or call the $.pjax.reload('#gridCommissionSelection') to redraw the grid-view with new entries.
Another way is to append new lines with JS and then save them via AJAX request. It will be rather dirty and unclear solution, I guess :)

Related

Yii2: how to create a custom delete button?

I'm using Yii2 Framework. I have data on GridView and each row has a delete button. Here's my code:
'content'=>function($data){
return Html::a('<i class="glyphicon glyphicon-ban-circle"></i> Bekor qilish', ['delete'], ['class' => 'btn btn-danger', 'data-method'=>'post']);
I want post actionDelete to be fired when user clicks the button. My code isn't working, and it's giving me Error #400: Missing required parameters: id.
I found the following code that I believe from Yii v1:
echo CHtml::link("Delete", '#', array('submit'=>array('post/delete', "id"=>$data->id), 'confirm' => 'Are you sure you want to delete?'));
I want this very function for Yii2.
I'm new to Yii Framework. Please, kindly help me.
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'Sample_Column1',
'Sample_Column2',
['header' => 'Actions',
'class' => 'yii\grid\ActionColumn',
'template' => '{new_action}',
'buttons' => [
'new_action' => function ($url) {
return Html::a('<span class="glyphicon glyphicon-ban-circle"></span>', $url, [
'title' => Yii::t('app', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete?'),
'data-method' => 'post', 'data-pjax' => '0',
]);
}
], 'urlCreator' => function ($action, $model) {
if ($action === 'new_action') {
$url = Url::to(['your_controller/your_delete_action', 'id' => $model->Some_id_from_your_db]);
return $url;
}
}
],
],
]);
Make sure you set a delete action in your controller and replace the 'your_controller/your_delete_action'
Replace the Some_id_from_your_db to the element id you want to delete from your db. Hope it helps. Cheers.
Edit: The 'Sample_Column1', 'Sample_Column2', is just for your reference because you didn't mention your table details. Therefore edit it accordingly.

Get selected rows value from kartik gridView in modal and pass it to parent form

I try to get value from modal bootstrap (contain table kartik gridview), and i want to pass the selected value to parent form (action create form). How to do that? Please advice.
modal form contains fields like this
<?php
$gridColumns = [
'id',
'date',
[
'attribute' => 'agen_id',
'value' => 'agen.agen_name'
],
[
'attribute' => 'price',
'format' => ['decimal', 0],
],
'remark_1:ntext',
'remark_2:ntext',
] ?>
and this is the button in parent form
<div class="col-xs-8">
<div class="form-group field-poagen-price">
<br>
<?= Html::button('Price List', ['value' => Url::to('../pricelist/list'), 'class' => 'btn btn-primary', 'id' => 'BtnModalPriceList']) ?>
<?php
Modal::begin([
'header' => 'List Harga',
'id' => 'modal',
'size' => 'modal-md'
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
</div>
</div>
and i register my jquery like this
$('#BtnModalPriceList').click(function(){
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));});
I want to get the value from modal to pass to the parent in order to fill the required field.
I have searched for a few days and still have no luck.
Please advice for the master. Thanks before.
I have solved it. This is what i do.
Create custom button in the gridView
[
'class' => 'yii\grid\ActionColumn',
'template' => '{select}',
'buttons' => [
'select' => function($url, $model, $key){
return Html::button('Select', [
'title' => Yii::t('yii', 'Select'),
'aria-label' => Yii::t('yii', 'Select'),
'class' => 'btn btn-primary select-row',
'data-id' => $model->id,
]);
},
],
],
and then register to jquery to process the data
$(document).on('click', '.select-row', function(){
// get id from custom button
var id = $(this).attr('data-id');
$.get('../pricelist/get-price', {id : id}, function(data){
$('#poagen-price').val(data);
});
$('#modalPriceList').modal('hide');
$('#poagen-fee').focus();
});
got help from #milos-ozegovic

Yii gridview How to set editable option based on the data

This is my grid view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'toolbar'=> [
['content'=>
Html::a('<i class="glyphicon glyphicon-plus"></i> Add New', ['create'],['class'=>'btn btn-shadow btn-success'])
],
'{toggleData}',
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'Name',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'attribute'=>'Age',
'vAlign'=>GridView::ALIGN_MIDDLE,
'value'=>'Age',
'width'=>'5%',
'hAlign'=>'center',
],
[
'attribute'=>'Address',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
'vAlign'=>GridView::ALIGN_MIDDLE,
'hAlign'=>GridView::ALIGN_CENTER,
'width'=>'15%',
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions'=>[
'options' => ['placeholder' => 'Search Status...'],
'pluginOptions' => [
'allowClear' => true
],
],
'value'=>function($model){
return ($model->Status=='0'?'Pending':($model->Status=='1'?'Accepted':'Rejected'));},
'filter'=>['Pending','Diterima'],
'label' => 'Status',
'refreshGrid'=>true,
'editableOptions'=>[
'format' => Editable::FORMAT_BUTTON,
'asPopover' => true,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data'=>['0'=>'Pending', '1'=>'Accepted', '2'=>'Rejected'],
'options' => ['class'=>'form-control'],
]
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '
<li>{view}</li>
<li>{update}</li>
<li>{delete}</li>',
'dropdown'=>true,
'dropdownOptions'=>['class'=>'pull-right'],
'headerOptions'=>['class'=>'kartik-sheet-style'],
],
],
'panel'=>[
'type'=>GridView::TYPE_DEFAULT,
]
]); ?>
Help me with the Status attribute. The result from this gridview is like this :
what i want is like this :
So, it's like when the Status is Pending, the editableOption is on.But when the Status is Accepted or Rejected, the editableOption is disable. Is that possible ?
You need Change value option of Status Attribute.
Change in value option of Status Attribute.
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
// ------- your code ------------
'format'=>'raw',
'value'=>function($model){
if($model->Status=='0')
{
// if status =0 than create button and append with string
$btn='<button type="button" id='.$model->id.' class="btn btn-default ><i class="fa fa-pencil-square-o"></i></button>';
return 'Pending'.$btn;
}
else
{
return($model->Status=='1'?'Accepted':'Rejected');
}
},
// ------- your code ------------
],
There are multiple ways to do what you have asked, if you have used editableOptions, then
For Editable::INPUT_DROPDOWN_LIST type -> use 'editableButtonOptions' => [ 'disabled' => $data->your_status_variable_condition],
For any other type add readonly property
'readonly' => function($data){
return $data->your_status_variable_condition;
},
I hope it helps someone who is looking for the same issue

Adding a class to Yii2 CRUD GridView

I've been trying to add a class to the images' column in the CRUD GridView in Yii2. So far, I've managed to display the image, but at its full width and height. I need to add a 'col-md-3' bs class to the image.
This is what I pulled off:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'emp_firstname',
'emp_lastname',
'emp_photo' => [
'format' => 'image',
'attribute' => 'emp_photo',
'value' => 'emp_photo',
'contentOptions' => ['class' => 'col-md-3'],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
if you want to add id or class or both to the table
try this.
'tableOptions' => [
'id' => 'theDatatable',
'class'=>'table table-striped table-bordered'
],
form the column group you can use options http://www.yiiframework.com/doc-2.0/yii-grid-column.html#$options-detail
'emp_photo' => [
'format' => 'image',
'attribute' => 'emp_photo',
'value' => 'emp_photo',
'options' => ['class' => 'col-md-3'],
],
but you should complete the "bootstrap grid" in the other columns

Outputting data from table after saving with ckeditor

Am saving data using the ckeditor in yii2 in my form model but when doing the view action it displays the html tags
CKEDITOR CODE:
<?= $form->field($model, 'case_description')->widget(CKEditor::className(),[
'editorOptions' => [
'preset' => 'full',
'inline' => false,
],
]);
?>
So after I save the data in the table its saved having the html codes
example of saved data:
<b>My new project being grilled</b>
So when viewing the data using yii2 detail and gridview it always shows the <b> instead of being bold.
How can I solve the problem
EXAMPLE: of grid view outputting it in the case_description column
<?= GridView::widget([
'summary'=>"",
'showOnEmpty'=>false,
'dataProvider' => $dataProviderb,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'case_description',
],
]) ?>
You just use the 'format' => 'row' in your GridView like:
<?= GridView::widget([
'summary'=>"",
'showOnEmpty'=>false,
'dataProvider' => $dataProviderb,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'case_description',
'format' => 'raw',
],
],
]) ?>

Categories