Skip to content

Commit 09f3762

Browse files
author
Eugene Tupikov
committed
TabularInput: now new row does't copy values from the most recent row
1 parent 11763d1 commit 09f3762

File tree

7 files changed

+80
-30
lines changed

7 files changed

+80
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Yii2 multiple input change log
44
1.2.4 in development
55
--------------------
66

7+
- Bug #39: TabularInput: now new row does't copy values from the most recent row
8+
79
1.2.3
810
-----
911

examples/actions/TabularInputAction.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function run()
2525
{
2626
Yii::setAlias('@unclead-examples', realpath(__DIR__ . '/../'));
2727

28-
$models = [new Item()];
28+
$models = $this->getItems();
29+
2930
$request = Yii::$app->getRequest();
3031
if ($request->isPost && $request->post('ajax') !== null) {
3132
$data = Yii::$app->request->post('Item', []);
@@ -42,7 +43,30 @@ public function run()
4243
// your magic
4344
}
4445

45-
4646
return $this->controller->render('@unclead-examples/views/tabular-input.php', ['models' => $models]);
4747
}
48+
49+
private function getItems()
50+
{
51+
$data = [
52+
[
53+
'id' => 1,
54+
'title' => 'Title 1',
55+
'description' => 'Description 1'
56+
],
57+
[
58+
'id' => 2,
59+
'title' => 'Title 2',
60+
'description' => 'Description 2'
61+
],
62+
];
63+
64+
$items = [];
65+
foreach ($data as $row) {
66+
$item = new Item();
67+
$item->setAttributes($row);
68+
$items[] = $item;
69+
}
70+
return $items;
71+
}
4872
}

examples/models/Item.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010

1111
use Yii;
1212
use yii\base\Model;
13-
// you have to install https://github.com/vova07/yii2-fileapi-widget
14-
use vova07\fileapi\behaviors\UploadBehavior;
1513

1614
/**
1715
* Class Item
1816
* @package unclead\widgets\examples\models
1917
*/
2018
class Item extends Model
2119
{
20+
public $id;
2221

2322
public $title;
2423

@@ -32,24 +31,26 @@ class Item extends Model
3231
public function behaviors()
3332
{
3433
return [
34+
// you have to install https://github.com/vova07/yii2-fileapi-widget
35+
/*
3536
'uploadBehavior' => [
36-
'class' => UploadBehavior::className(),
37+
'class' => \vova07\fileapi\behaviors\UploadBehavior::className(),
3738
'attributes' => [
3839
'file' => [
3940
'path' => Yii::getAlias('@webroot') . '/images/',
4041
'tempPath' => Yii::getAlias('@webroot') . '/images/tmp/',
4142
'url' => '/images/'
4243
],
4344
]
44-
]
45+
]*/
4546
];
4647
}
4748

4849
public function rules()
4950
{
5051
return [
5152
[['title', 'description'], 'required'],
52-
['file', 'safe']
53+
[['id', 'file'], 'safe']
5354
];
5455
}
5556

examples/views/multiple-input.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121
'validateOnBlur' => false,
2222
]);?>
2323

24-
<h3>Single column</h3>
24+
<h3>Single column</h3>
2525
<?php
2626
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
27-
'limit' => 5,
28-
'allowEmptyList' => true,
29-
'enableGuessTitle' => true
27+
'limit' => 5,
28+
'allowEmptyList' => true,
29+
'enableGuessTitle' => true
3030
])
3131
->label(false);
3232
?>
3333

3434
<h3>Multiple columns</h3>
3535
<?= $form->field($model, 'schedule')->widget(MultipleInput::className(), [
36-
'id' => 'schedule-wrapper',
3736
'limit' => 4,
3837
'allowEmptyList' => true,
3938
'columns' => [
@@ -132,13 +131,14 @@
132131

133132
<?php
134133
$js = <<< JS
135-
$('#schedule-wrapper').on('afterInit', function(){
134+
$('#examplemodel-schedule').on('afterInit', function(){
136135
console.log('calls on after initialization event');
137136
}).on('beforeAddRow', function(e) {
138137
console.log('calls on before add row event');
139138
}).on('afterAddRow', function(e) {
140139
console.log('calls on after add row event');
141-
}).on('beforeDeleteRow', function(){
140+
}).on('beforeDeleteRow', function(e, item){
141+
console.log(item);
142142
console.log('calls on before remove row event');
143143
return confirm('Are you sure you want to delete row?')
144144
}).on('afterDeleteRow', function(){

examples/views/tabular-input.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
'validateOnBlur' => false,
3232
],
3333
'columns' => [
34+
[
35+
'name' => 'id',
36+
'type' => \unclead\widgets\TabularColumn::TYPE_HIDDEN_INPUT
37+
],
3438
[
3539
'name' => 'title',
3640
'title' => 'Title',
@@ -40,16 +44,16 @@
4044
'name' => 'description',
4145
'title' => 'Description',
4246
],
43-
[
44-
'name' => 'file',
45-
'title' => 'File',
46-
'type' => \vova07\fileapi\Widget::className(),
47-
'options' => [
48-
'settings' => [
49-
'url' => ['site/fileapi-upload']
50-
]
51-
]
52-
],
47+
// [
48+
// 'name' => 'file',
49+
// 'title' => 'File',
50+
// 'type' => \vova07\fileapi\Widget::className(),
51+
// 'options' => [
52+
// 'settings' => [
53+
// 'url' => ['site/fileapi-upload']
54+
// ]
55+
// ]
56+
// ],
5357
[
5458
'name' => 'date',
5559
'type' => \kartik\date\DatePicker::className(),

src/TabularColumn.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,24 @@ protected function ensureModel($model)
5555
{
5656
return $model instanceof Model;
5757
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public function setModel($model)
63+
{
64+
$currentModel = $this->getModel();
65+
66+
// If model is null and current model is not empty it means that widget renders a template
67+
// In this case we have to unset all model attributes
68+
if ($model === null && $currentModel !== null) {
69+
foreach ($currentModel->attributes() as $attribute) {
70+
$currentModel->$attribute = null;
71+
}
72+
} else {
73+
parent::setModel($model);
74+
}
75+
}
76+
77+
5878
}

src/components/BaseColumn.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,13 @@ public function isHiddenInput()
171171
protected function prepareValue()
172172
{
173173
$data = $this->getModel();
174-
if ($this->value !== null) {
175-
$value = $this->value;
176-
if ($value instanceof \Closure) {
177-
$value = call_user_func($value, $data);
178-
}
174+
if ($this->value instanceof \Closure) {
175+
$value = call_user_func($this->value, $data);
179176
} else {
180-
if ($data instanceof ActiveRecord) {
177+
if ($data instanceof ActiveRecord ) {
181178
$value = $data->getAttribute($this->name);
179+
} elseif ($data instanceof Model) {
180+
$value = $data->{$this->name};
182181
} elseif (is_array($data)) {
183182
$value = ArrayHelper::getValue($data, $this->name, null);
184183
} elseif(is_string($data)) {

0 commit comments

Comments
 (0)