how to save kartik editable yii2 - php

I have a problem with editable kartik, how can I make editable kartik stored in the database? sometimes BAD REQUEST notifications appear
My Controller
public function actions()
{
return ArrayHelper::merge ( parent::actions () , [
'editable' => [
'class' => EditableColumn::className() ,
'modelClass' => AssetMasterRequest::className() ,
'forceCreate'=> false,
]
]);
}
My View
<?php
$gridColumns = [
'assetMaster.asset_name',
'request_date',
[
'class' => 'kartik\grid\EditableColumn',
'attribute' => 'requested_by',
'pageSummary' => 'Total',
'vAlign' => 'middle',
'width' => '210px',
'editableOptions' => function ($model, $key, $index) use ($ambildata) {
return [
'header' => 'Request By',
'formOptions' => ['action'=>'pru'] ,
'size' => 'md',
'afterInput' => function ($form, $widget) use ($model, $index) {
return $form->field($model, "request_notes");
}
];
}
],
[
'class' => 'kartik\grid\EditableColumn',
'attribute' => 'request_notes',
'vAlign' => 'middle',
'width' => '210px',
'editableOptions' =>[
'formOptions'=>[
'action'=>'editable'
],
]
],
'requested_by',
];?>
Please help. Have spent two days on this. Thanks!

You must return a JSON encoded response in the array format specified above with output and message values. This is important for the AJAX request to be processed and completed successfully.
Here is a grid view configuration with editable column
echo GridView::widget([
'id' => 'kv-grid-demo',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns, // check the configuration for grid columns by clicking button above
'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
'headerRowOptions' => ['class' => 'kartik-sheet-style'],
'filterRowOptions' => ['class' => 'kartik-sheet-style'],
'pjax' => true, // pjax is set to always true for this demo
// set your toolbar
'toolbar' => [
[
'content' =>
Html::button('<i class="fas fa-plus"></i>', [
'class' => 'btn btn-success',
'title' => Yii::t('kvgrid', 'Add Book'),
'onclick' => 'alert("This will launch the book creation form.\n\nDisabled for this demo!");'
]) . ' '.
Html::a('<i class="fas fa-redo"></i>', ['grid-demo'], [
'class' => 'btn btn-outline-secondary',
'title'=>Yii::t('kvgrid', 'Reset Grid'),
'data-pjax' => 0,
]),
'options' => ['class' => 'btn-group mr-2']
],
'{export}',
'{toggleData}',
],
'toggleDataContainer' => ['class' => 'btn-group mr-2'],
// set export properties
'export' => [
'fontAwesome' => true
],
// parameters from the demo form
'bordered' => $bordered,
'striped' => $striped,
'condensed' => $condensed,
'responsive' => $responsive,
'hover' => $hover,
'showPageSummary' => $pageSummary,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => $heading,
],
'persistResize' => false,
'toggleDataOptions' => ['minCount' => 10],
'exportConfig' => $exportConfig,
'itemLabelSingle' => 'book',
'itemLabelPlural' => 'books'
]);

Related

Style Dialog - FullExportMenu Kartik widget yii2

I'm working with FullExportMenu from Kartik. I have problem with styling confirm dialog.
My code:
$fullExport = FullExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $columns,
'exportConfig' => [
ExportMenu::FORMAT_TEXT => false,
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_EXCEL => false,
ExportMenu::FORMAT_EXCEL_X => false,
ExportMenu::FORMAT_PDF => false,
],
'filename' => 'export-faktur',
'noExportColumns' => [0, 7, 15, 16],
'hiddenColumns' => [],
'showColumnSelector' => false,
'pjax' => true,
'clearBuffers' => true,
'dropdownOptions' => [
'class' => 'btn-primary btn-sm',
'title' => Yii::t('kvexport', 'Export data in selected format'),
'icon' => '<i class="fas fa-file-download"></i>',
'itemsBefore' => [
'<div class="dropdown-header">Eksportuj całą listę</div>',
],
],
'options' => [
'id' => 'expFullInvoiceMenu',
],
'target' => ExportMenu::TARGET_SELF,
'exportContainer' => ['class' => 'btn-group-md'],
]);
I tried to use krajeeDialogSettings but it doesn't work. How do I get to the dialog styling options? I need to change the color of the dialog and add classes to the buttons. I don't want to overwrite classes in css.

Yii2 ExpandRowColumn not working with ScrollPager

I have this below code and when I click to expand the row then its working for first page and when its scroll down and when load the next page onward then expand not working.
<?php
echo GridView::widget([
"dataProvider" => $dataProvider,
"filterSelector" => "select[name='per-page']",
'floatHeader' => true,
'floatHeaderOptions' => [
'scrollingTop' => '50',
'floatContainerClass' => "float_table_header"
],
"layout" => getGridLayout(),
"tableOptions" => ["class" => "table table-striped task-management-table"],
"columns" => [
[
'class' => 'kartik\grid\ExpandRowColumn',
'width' => '50px',
'detailRowCssClass' => 'detailRowCssClass',
'detailUrl' => Url::to(['test/test1']),
'value' => function ($model, $key, $index, $column) {
return GridView::ROW_COLLAPSED;
},
'headerOptions' => ['class' => 'kartik-sheet-style'],
'expandOneOnly' => false
],
],
'pager' => [
'class' => ScrollPager::className(),
'container' => '.grid-view tbody',
'item' => 'tr',
'paginationSelector' => '.grid-view .pagination',
'triggerTemplate' => '<tr class="ias-trigger"><td colspan="100%" style="text-align: center"><a style="cursor: pointer">{text}</a></td></tr>',
'enabledExtensions' => [
ScrollPager::EXTENSION_SPINNER,
//ScrollPager::EXTENSION_NONE_LEFT,
ScrollPager::EXTENSION_PAGING,
],
],
]);
?>
<?php //ActiveForm::end(); ?>

Yii2 - create gridview customizing "model/style" for all gridview and call the style

I have customized my gridview in Yii2 to show columns, headers, and pager in a certain way
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'filterSelector' => 'select[name="per-page"]',
'tableOptions'=> ['class'=>'table datatable-header-footer datatable-header-footer'],
'showFooter' => true,
'layout'=>"{items}\n\n{summary}\n\n<div class='text-right'>{pager}</div>\n",
//'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}",
'summary' => " <br/> Affichage de {begin} à {end} des {totalCount} lignes <br/> <br/>",
'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ' - '],
'pager' => [
'nextPageLabel' => '→',
'prevPageLabel' => '←',
'firstPageLabel' => true,
'maxButtonCount' => 5,
'lastPageLabel' => true
],
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
//'id', ...
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:10%;'],
'header'=>'Actions',
'template' => '{all}',
'buttons' => [
'all' => function ($url, $model, $key) {
return ButtonDropdown::widget([
'encodeLabel' => false,
'label' => 'Choisir',
'dropdown' => [
'encodeLabels' => false,
'items' => [
[
'label' => \Yii::t('yii', '<i class="icon-search4"></i> Details'),
'url' => ['view', 'id' => $key],
],
[
'label' => \Yii::t('yii', '<i class="icon-pencil5"></i> Modifier'),
'url' => ['update', 'id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list"></i> Annonces'),
'url' => ['annonces', 'agence_id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list2"></i> Agents'),
'url' => ['professionnels', 'agence_id' =>$key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-bin"></i> Supprimer'),
'linkOptions' => [
'data' => [
'method' => 'post',
'confirm' => \Yii::t('yii', 'Are you sure you want to delete this item?'),
],
],
'url' => ['delete', 'id' => $key],
'visible' => true, // same as above
],
],
'options' => [
'class' => 'dropdown-menu-right', // right dropdown
],
],
'options' => [
'class' => 'btn-default',
'style' => 'padding-left: 5px; padding-right: 5px;', // btn-success, btn-info, et cetera
],
'split' => true,
]);
Im wondering if there is a way to create a gridview "style" so I can call and apply this style to every gridview in the project !
Yes, extend GridView class with your own, set all properties, override anything you need and use your class instead of default GridView.

How to put an input text into a yii2 kartik gridview and pass params from the view to controller?

I have tried with this code but not appear to be ok, no parm recieved with GET.
The idea is put an input box for the quantity (attribute cantidad) and then pass this quantity when user press the view button (it's the view function that I customized)
View code:
<?php
Html::beginForm(Url::toRoute(["customers/addproduct", 'id'=>$searchModel->id]), "POST");
$dataProvider->pagination = [
'pagesize' => 10,
];
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'summary' => "Mostrando {begin} - {end} de {totalCount} productos",
'containerOptions' => ['style' => 'overflow: auto'],
'headerRowOptions' => ['class' => 'kartik-sheet-style'],
'filterRowOptions' => ['class' => 'kartik-sheet-style'],
'pjax' => false,
'columns' => [
['class'=>'kartik\grid\SerialColumn'],
[
'label' => 'Imagen',
'attribute' => 'id_image',
'vAlign' => 'middle',
'format' => 'image',
'value' => function($searchModel) {
return 'http://prestashop.xxxxxx.xxx:1100/'.$searchModel->id_image.'-home/'.$searchModel->link_rewrite.'.jpg';
},
],
[
'label' => 'EAN',
'attribute'=>'ean13',
'vAlign'=>'left',
'pageSummary'=>'Total',
],
[
'attribute'=>'producto',
'vAlign'=>'left',
'pageSummary'=>'Total',
],
'categoria',
'atributos',
[
'header' => 'Cantidad',
'attribute'=>'cantidad',
'vAlign'=>'middle',
'hAlign'=>'right',
'value' => function($searchModel){
return Html::textInput('quantity'.$searchModel->id, $searchModel->cantidad, ['value'=>'quantity', 'maxlength'=>10,'style'=>'width:80px']);
},
'format' => 'raw',
],
[
'label' => 'Precio',
'attribute' => 'precio',
'vAlign' => 'middle',
'hAlign' => 'right',
'format' => ['decimal', 2],
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '{view}',
'dropdown' => false,
'vAlign' => 'middle',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('Añadir Producto', $url, [
'title' => Yii::t('app', 'Añadir'),
'type' => 'submit',
]);
}
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url = 'index.php?r=customers/shopping&act=v&id='.$model->id.'&c='.$model->id_customer;
return $url;
}
}
],
],
// set export properties
'export' => [
'fontAwesome' => true,
],
'bordered' => false,
'striped' => false,
'condensed' => true,
'responsive' => true,
'hover' => true,
'showPageSummary' => true,
'showFooter' => false,
'floatHeader' => true,
'floatHeaderOptions' => ['scrollingTop' => '50'],
'pjax' => false,
'panel' => [
'type' => GridView::TYPE_SUCCESS,
'heading' => '<i class="glyphicon glyphicon-align-justify"></i> Productos',
'footer' => '',
'after' => false,
],
'persistResize' => false,
]);
Html::endForm()
Controller Code
if (Yii::$app->request->get())
{
if (isset($_GET['c']))
{
$id_unique = Html::encode($_GET["id"]);
$quantity = Html::encode($_POST['quantity'.$id_unique]);
$id_customer = Html::encode($_GET["c"]);
($quantity > 0) ? $d = 1 : $d = 2;
$this->addProduct($idshop, $id_customer);
}
elseif (isset($_GET['shoppingcart']))
{
$id_supplier = Html::encode($_GET["shoppingcart"]);
//return Yii::$app->response->redirect(Url::to(["shoppings/vieworder"]));
}
elseif (isset($_GET['bootbox']))
{
$id_supplier = Html::encode($_GET["checkout"]);
//$this->launchModalDialog();
}
else
{
$id_supplier = $user->id_supplier;
}
}
else
{
$searchModel = new TablaNsCustomerProductSearch;
$dataProvider = $searchModel->searchCustomer(Yii::$app->request->queryParams);
return $this->render('shopping/shopping', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
$searchModel = new TablaNsCustomerProductSearch;
$dataProvider = $searchModel->searchCustomer(Yii::$app->request->queryParams);
return $this->render('shopping/shopping', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
Thank you!
i do some modification to your code, maybe you need to do more because i don't know how you are implementing this, but here it is:
the view:
Html::beginForm(Url::toRoute(["customers/addproduct",'id'=>$searchModel->id]), "POST");
$dataProvider->pagination = [
'pagesize' => 10,
];
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'summary' => "Mostrando {begin} - {end} de {totalCount} productos",
'containerOptions' => ['style' => 'overflow: auto'],
'headerRowOptions' => ['class' => 'kartik-sheet-style'],
'filterRowOptions' => ['class' => 'kartik-sheet-style'],
'pjax' => false,
'columns' => [
['class'=>'kartik\grid\SerialColumn'],
[
'label' => 'Imagen',
'attribute' => 'id_image',
'vAlign' => 'middle',
'format' => 'image',
'value' => function($searchModel) {
return 'http://prestashop.xxxxxx.xxx:1100/'.$searchModel->id_image.'-home/'.$searchModel->link_rewrite.'.jpg';
},
],
[
'label' => 'EAN',
'attribute'=>'ean13',
'vAlign'=>'left',
'pageSummary'=>'Total',
],
[
'attribute'=>'producto',
'vAlign'=>'left',
'pageSummary'=>'Total',
],
'categoria',
'atributos',
[
'header' => 'Cantidad',
'attribute'=>'cantidad',
'vAlign'=>'middle',
'hAlign'=>'right',
'value' => function($searchModel){
return Html::textInput('quantity'.$searchModel->id, $searchModel->cantidad, ['id'=>"quantity.$searchModel->id",'value'=>'quantity', 'maxlength'=>10,'style'=>'width:80px']);
},
'format' => 'raw',
],
[
'label' => 'Precio',
'attribute' => 'precio',
'vAlign' => 'middle',
'hAlign' => 'right',
'format' => ['decimal', 2],
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '{view}',
'dropdown' => false,
'vAlign' => 'middle',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('Añadir Producto', '#', [
'onclick'=>"view($model->id,'$url')",
'title' => Yii::t('app', 'Añadir'),
'type' => 'submit',
]);
}
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url = 'index.php?r=customers/shopping&act=v&id='.$model->id.'&c='.$model->id_customer;
return $url;
}
}
],
],
// set export properties
'export' => [
'fontAwesome' => true,
],
'bordered' => false,
'striped' => false,
'condensed' => true,
'responsive' => true,
'hover' => true,
'showPageSummary' => true,
'showFooter' => false,
'floatHeader' => true,
'floatHeaderOptions' => ['scrollingTop' => '50'],
'pjax' => false,
'panel' => [
'type' => GridView::TYPE_SUCCESS,
'heading' => '<i class="glyphicon glyphicon-align-justify"></i> Productos',
'footer' => '',
'after' => false,
],
'persistResize' => false,
]);
Html::endForm()
?>
<script type="text/javascript">
function view(id,url){
c = $('#quantity'+id).val();
url = url+'&quantity='+c;
window.location=url;
return false;
}
</script>
and this is the modified controler:
if (Yii::$app->request->get())
{
if (isset($_GET['c']))
{
$id_unique = Html::encode($_GET["id"]);
$quantity = Html::encode($_POST['quantity']);
$id_customer = Html::encode($_GET["c"]);
($quantity > 0) ? $d = 1 : $d = 2;
$this->addProduct($idshop, $id_customer);
}
elseif (isset($_GET['shoppingcart']))
{
$id_supplier = Html::encode($_GET["shoppingcart"]);
//return Yii::$app->response->redirect(Url::to(["shoppings/vieworder"]));
}
elseif (isset($_GET['bootbox']))
{
$id_supplier = Html::encode($_GET["checkout"]);
//$this->launchModalDialog();
}
else
{
$id_supplier = $user->id_supplier;
}
}
else
{
$searchModel = new TablaNsCustomerProductSearch;
$dataProvider = $searchModel->searchCustomer(Yii::$app->request->queryParams);
return $this->render('shopping/shopping', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
$searchModel = new TablaNsCustomerProductSearch;
$dataProvider = $searchModel->searchCustomer(Yii::$app->request->queryParams);
return $this->render('shopping/shopping', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
i can't test this but i think it should work.
The HTML a tag has a type option, but it has nothing to do with the submitting of the form.
Use Html::button(..., ['type' => 'submit']), or Html::submitInput()
Of course the value of "cantidad" will be in $POST['quantity'.$id].

Show actionCoumn only for user Login Gridview Yii2

I am trying to make feedback page for users who want to send feedback for my web, and I am using gridview to show feedback list. This is the gridview code i made.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}{pager}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
[ 'class' => 'yii\grid\ActionColumn',
'contentOptions'=>['style'=>'width: 5px;'],
'template' => '{update} {delete}'
],
],
]); ?>
in my gridview it shows action column at every feedback sent. but what i want is that the action column should only show the feedback's sent by only user login. so where should I customize it?
If you want to show the ActionColumn only when the user is logged in, you can just do it like this:
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['style' => 'width:34px; font-size:18px;']
'visible' => !Yii::$app->user->isGuest,
],
If you want to show the ActionColumn only for the feedbacks the user created, then you will have to do something like this:
[
'format' => 'html',
'value' => function($model) {
if($model->user_id == Yii::$app->user->identity->id) {
return Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id])
.' '.Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], [
'data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']
]
);
}
return '';
},
],
Try this way setting true or false the visible optioon for actionColumn
// for guest
if(Yii::$app->user->isGuest)
{
$actionColumn = [ 'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['style' => 'width:34px; font-size:18px;']
'visible' => true,
],
}
// for users
else
{
$actionColumn = [ 'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['style' => 'width:34px; font-size:18px;']
'visible' => false,
],
}
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}{pager}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
$actionColumn
],
]); ?>
Simply try:
<?php
// non logged in users
if(Yii::$app->user->isGuest)
{
$actionColumn = [];
}
// logged in users
else
{
$actionColumn = [ 'class' => 'yii\grid\ActionColumn',
'contentOptions'=>['style'=>'width: 5px;'],
'template' => '{update} {delete}'
];
}
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}{pager}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
$actionColumn
],
]); ?>

Categories