Skip to content

Commit 2f7d33c

Browse files
committed
Added:
- Param "toolbarTemplate" for BulkCheckboxColumn - Replacing status value in BulkCheckboxAction -- Minor fixes
1 parent 387aad2 commit 2f7d33c

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.idea
2+
3+
nbproject
4+
5+
.buildpath
6+
.project
7+
.settings
8+
9+
Thumbs.db
10+
11+
/vendor
12+
13+
composer.phar
14+
composer.lock
15+
16+
.DS_Store

BulkCheckboxAction.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace integready\bulkactionscheckboxcolumn;
44

55
use Yii;
6-
use yii\helpers\Url;
76
use yii\base\Action;
8-
use yii\helpers\Json;
7+
use yii\db\ActiveRecord;
98
use yii\helpers\ArrayHelper;
9+
use yii\helpers\Json;
10+
use yii\helpers\Url;
1011

1112
/**
1213
* Class BulkCheckboxAction
@@ -17,7 +18,7 @@ class BulkCheckboxAction extends Action
1718
const UPDATE_TYPE_ONEBYONE = 0;
1819

1920
/**
20-
* @var \yii\db\ActiveRecord
21+
* @var ActiveRecord
2122
*/
2223
public $modelClass;
2324

@@ -31,6 +32,11 @@ class BulkCheckboxAction extends Action
3132
*/
3233
public $statusField;
3334

35+
/**
36+
* @var array|null
37+
*/
38+
public $values;
39+
3440
/**
3541
* @var int
3642
*/
@@ -44,13 +50,25 @@ public function run()
4450
$postUrl = Yii::$app->request->get();
4551
$data = empty(Yii::$app->request->get($this->gridId . '_' . $this->statusField)) ? false : Json::decode(Yii::$app->request->get($this->gridId . '_' . $this->statusField), true);
4652
if ($data && count($data) >= 1) {
47-
if ($this->updateType === self::UPDATE_TYPE_ALL) {
48-
$this->modelClass::updateAll([$this->statusField => $data['status']], [$this->modelClass::primaryKey()[0] => $data['ids']]);
49-
} elseif ($this->updateType === self::UPDATE_TYPE_ONEBYONE) {
50-
$models = $this->modelClass::find()->where([$this->modelClass::primaryKey()[0] => $data['ids']])->all();
51-
foreach ($models as $model) {
52-
$model->{$this->statusField} = $data['status'];
53-
$model->save();
53+
if (is_array($this->values) && isset($this->values[$data['status']])) {
54+
if ($this->updateType === self::UPDATE_TYPE_ALL) {
55+
$this->modelClass::updateAll([$this->statusField => $this->values[$data['status']]], [$this->modelClass::primaryKey()[0] => $data['ids']]);
56+
} elseif ($this->updateType === self::UPDATE_TYPE_ONEBYONE) {
57+
$models = $this->modelClass::find()->where([$this->modelClass::primaryKey()[0] => $data['ids']])->all();
58+
foreach ($models as $model) {
59+
$model->{$this->statusField} = $this->values[$data['status']];
60+
$model->save();
61+
}
62+
}
63+
} else {
64+
if ($this->updateType === self::UPDATE_TYPE_ALL) {
65+
$this->modelClass::updateAll([$this->statusField => $data['status']], [$this->modelClass::primaryKey()[0] => $data['ids']]);
66+
} elseif ($this->updateType === self::UPDATE_TYPE_ONEBYONE) {
67+
$models = $this->modelClass::find()->where([$this->modelClass::primaryKey()[0] => $data['ids']])->all();
68+
foreach ($models as $model) {
69+
$model->{$this->statusField} = $data['status'];
70+
$model->save();
71+
}
5472
}
5573
}
5674
}

BulkCheckboxColumn.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class BulkCheckboxColumn extends CheckboxColumn
1717
*/
1818
public $elements;
1919

20+
/**
21+
* @var array
22+
*/
23+
public $toolbarTemplate = [
24+
'{export}',
25+
'{toggleData}',
26+
];
27+
2028
/**
2129
* @var string
2230
*/
@@ -39,7 +47,6 @@ public function init()
3947
}
4048

4149
if (!isset($element['buttonType']) || $element['buttonType'] === self::BUTTON_TYPE_DROPDOWN) {
42-
4350
$element['field'] = $grid->getId() . '_' . $element['field'];
4451
if ($comma) {
4552
$this->fieldFilterSelector .= ', input[name=\'' . $element['field'] . '\']';
@@ -72,11 +79,8 @@ public function init()
7279
}
7380
}
7481

75-
$grid->toolbar = [
76-
'content' => $buttons,
77-
'{export}',
78-
'{toggleData}',
79-
];
82+
$grid->toolbar = $this->toolbarTemplate;
83+
array_unshift($grid->toolbar, $buttons);
8084

8185
if (!empty($this->fieldFilterSelector)) {
8286
if (!empty($grid->filterSelector)) {

ButtonDropdown.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace integready\bulkactionscheckboxcolumn;
44

5+
use yii\base\Widget;
56
use yii\bootstrap\ButtonDropdown as BBDropdown;
67
use yii\helpers\ArrayHelper;
7-
use yii\base\Widget;
88
use yii\helpers\Html;
99
use yii\helpers\Json;
1010
use yii\web\View;
@@ -64,6 +64,7 @@ public function init()
6464

6565
/**
6666
* @return bool|string
67+
* @throws \Exception
6768
*/
6869
public function run()
6970
{

assets/js/buttonDropdown.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
32
let selectorName = '';
43
let key;
54
let comma = false;
@@ -16,7 +15,7 @@
1615
e.preventDefault();
1716
let buttonSelector = '#parent-' + $(this).attr('class');
1817
if (confirm('Are you sure you want to "' + $(buttonSelector).text() + '" to "' + $(this).text() + '" for selected items?')) {
19-
let data = '{"status":' + $(this).data('status') + ',"ids":[' + $('#' + $(this).attr('class').replace('-ids', '')).yiiGridView('getSelectedRows').join(',') + ']}';
18+
let data = '{"status":"' + $(this).data('status') + '","ids":[' + $('#' + $(this).attr('class').replace('-ids', '')).yiiGridView('getSelectedRows').join(',') + ']}';
2019
let selectorSearch = 'input[name=' + $(this).data('field') + ']';
2120
$(selectorSearch).val(data);
2221
$(selectorSearch).change();

0 commit comments

Comments
 (0)