Skip to content

Commit

Permalink
Add change quantity functionality in cart index grid
Browse files Browse the repository at this point in the history
  • Loading branch information
tafid committed Oct 28, 2015
1 parent 1e49467 commit 473b3fc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 47 deletions.
17 changes: 12 additions & 5 deletions src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ public function actionAdd()
throw new NotFoundHttpException();
}

public function actionUpdateQuantity($id, $quantity)
public function actionUpdateQuantity()
{
$position = Yii::$app->cart->getPositionById($id);
if ($position) {
Yii::$app->cart->update($position, $quantity);
Yii::$app->end();
$request = Yii::$app->request;
$id = $request->post('id');
$quantity = $request->post('quantity');
if ($id && $quantity) {
$position = Yii::$app->cart->getPositionById($id);
if ($position) {
Yii::$app->cart->update($position, $quantity);
$this->redirect('index');
}
}

throw new NotFoundHttpException();
}

public function actionClear()
Expand Down
39 changes: 4 additions & 35 deletions src/grid/QuantityColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,15 @@

namespace hipanel\modules\cart\grid;

use hipanel\helpers\ArrayHelper;
use hiqdev\xeditable\grid\XEditableColumn;
use hiqdev\xeditable\widgets\XEditable;
use Yii;
use yii\helpers\Url;
use yii\grid\DataColumn;

class QuantityColumn extends XEditableColumn
class QuantityColumn extends DataColumn
{
public $xEditableType;

public $sourceData;

public function init()
public function getDataCellValue($model, $key, $index)
{
$this->pluginOptions['mode'] = 'inline';

if ($this->xEditableType === null) {
$this->pluginOptions['type'] = 'number';
} else {
$this->pluginOptions['type'] = $this->xEditableType;
}
}

/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
$quantityOptions = $model->getQuantityOptions();
$xEditableVariant = ['select', 'checklist', 'typeahead', 'select2'];
if (in_array($this->xEditableType, $xEditableVariant) || !empty($quantityOptions)) {
$this->pluginOptions = ArrayHelper::merge(['source' => $quantityOptions], $this->pluginOptions);
}
$this->pluginOptions['url'] = ['update-quantity', 'id' => $model->id, 'quantity' => $model->quantity];

return Yii::createObject(ArrayHelper::merge([
'class' => XEditable::className(),
'model' => $model,
'attribute' => $this->attribute,
'pluginOptions' => $this->pluginOptions
], $this->widgetOptions))->run();
return $key;
}
}
18 changes: 11 additions & 7 deletions src/views/cart/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use hipanel\modules\cart\grid\QuantityColumn;
use hipanel\modules\cart\widgets\QuantityCell;
use yii\grid\ActionColumn;
use yii\grid\GridView;
use yii\helpers\Html;
Expand Down Expand Up @@ -38,15 +39,18 @@
}
],
[
'class' => QuantityColumn::className(),
// 'class' => 'hiqdev\xeditable\grid\XEditableColumn',
'xEditableType' => 'select',
'attribute' => 'quantity',
'pluginOptions' => [
'url' => 'set-description',
],
'value' => function($model, $key, $index, $column) {
return QuantityCell::widget(['model' => $model]); //, 'type' => 'number'
},
'format' => 'raw'
],
[
'attribute' => 'price',
'value' => function($model) {
return $model->getCost();
}
],
'price',
'actions' => [
'class' => ActionColumn::className(),
'template' => '{remove}',
Expand Down
42 changes: 42 additions & 0 deletions src/widgets/QuantityCell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace hipanel\modules\cart\widgets;

use Yii;

class QuantityCell extends \yii\base\Widget
{
const MODE_SELECT = 'select';

const MODE_NUMBER = 'number';

public $model;

public $type;

public function init()
{
parent::init();
$this->registerClientScript();
}

public function run()
{
return $this->render('QuantityCell_view', [
'view' => $this->getView(),
'model' => $this->model,
'type' => $this->type ?: self::MODE_SELECT,
]);
}

public function registerClientScript()
{
$this->getView()->registerJs(<<<JS
jQuery(document).on('change', '.quantity-field', function(evnet) {
var form = jQuery(this).parents('form').get(0);
$(form).submit();
});
JS
);
}
}
18 changes: 18 additions & 0 deletions src/widgets/views/QuantityCell_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use hipanel\modules\cart\widgets\QuantityCell;
use yii\helpers\Html;

?>

<?= Html::beginForm('update-quantity', 'post', ['id' => $model->id]) ?>

<?= Html::hiddenInput('id', $model->id) ?>

<?php if ($type == QuantityCell::MODE_SELECT) : ?>
<?= Html::dropDownList('quantity', $model->quantity, $model->getQuantityOptions(), ['class' => 'form-control quantity-field']) ?>
<?php else : ?>
<?= Html::input('number', 'quantity', $model->quantity, ['class' => 'form-control quantity-field', 'min' => 1, 'max' => 99, 'step' => 1]) ?>
<?php endif; ?>

<?= Html::endForm() ?>

0 comments on commit 473b3fc

Please sign in to comment.