Pau Rodriguez
Pau Rodriguez

Reputation: 21

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!

Upvotes: 2

Views: 1928

Answers (2)

Yordankis
Yordankis

Reputation: 93

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.

Upvotes: 1

Tibor Nagy
Tibor Nagy

Reputation: 1236

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].

Upvotes: 0

Related Questions