Skip to content

Commit 21d0138

Browse files
authored
Merge pull request #380 from unclead/fix_rendering_action_buttons
Fix rendering action buttons
2 parents c9a236d + fa5ac6a commit 21d0138

File tree

5 files changed

+99
-98
lines changed

5 files changed

+99
-98
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Yii2 multiple input change log
44
2.30.0 (in development)
55
=======================
66

7+
2.30.0
8+
=======================
9+
- #369 fix rendering action buttons in case the dataset contains non-numeric indices (unclead)
10+
711
2.29.0
812
=======================
913
- fix addind active form fields doesn't work properly in case of 10 rows and more (unclead)

src/renderers/BaseRenderer.php

+32
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ protected function isAddButtonPositionRowBegin()
514514
return in_array(self::POS_ROW_BEGIN, $this->addButtonPosition);
515515
}
516516

517+
protected function isFixedNumberOfRows()
518+
{
519+
return $this->max === $this->min;
520+
}
521+
517522
private function prepareIndexPlaceholder()
518523
{
519524
$this->indexPlaceholder = 'multiple_index_' . $this->id;
@@ -579,4 +584,31 @@ public function isBootstrapTheme()
579584
return $this->theme === self::THEME_BS;
580585
}
581586

587+
protected function renderRows()
588+
{
589+
$rows = [];
590+
591+
$rowIndex = 0;
592+
if ($this->data) {
593+
foreach ($this->data as $index => $item) {
594+
if ($rowIndex <= $this->max) {
595+
$rows[] = $this->renderRowContent($index, $item, $rowIndex);
596+
} else {
597+
break;
598+
}
599+
$rowIndex++;
600+
}
601+
for (; $rowIndex < $this->min; $rowIndex++) {
602+
$rows[] = $this->renderRowContent($rowIndex, null, $rowIndex);
603+
}
604+
} elseif ($this->min > 0) {
605+
for (; $rowIndex < $this->min; $rowIndex++) {
606+
$rows[] = $this->renderRowContent($rowIndex, null, $rowIndex);
607+
}
608+
}
609+
610+
return $rows;
611+
}
612+
613+
abstract protected function renderRowContent($index = null, $item = null, $rowIndex = null);
582614
}

src/renderers/DivRenderer.php

+19-32
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,7 @@ public function renderFooter()
9898
*/
9999
protected function renderBody()
100100
{
101-
$rows = [];
102-
103-
if ($this->data) {
104-
$j = 0;
105-
foreach ($this->data as $index => $item) {
106-
if ($j++ <= $this->max) {
107-
$rows[] = $this->renderRowContent($index, $item);
108-
} else {
109-
break;
110-
}
111-
}
112-
for ($i = $j; $i < $this->min; $i++) {
113-
$rows[] = $this->renderRowContent($i);
114-
}
115-
} elseif ($this->min > 0) {
116-
for ($i = 0; $i < $this->min; $i++) {
117-
$rows[] = $this->renderRowContent($i);
118-
}
119-
}
120-
121-
return implode("\n", $rows);
101+
return implode("\n", $this->renderRows());
122102
}
123103

124104
/**
@@ -128,14 +108,16 @@ protected function renderBody()
128108
* @param ActiveRecordInterface|array $item
129109
* @return mixed
130110
*/
131-
private function renderRowContent($index = null, $item = null)
111+
protected function renderRowContent($index = null, $item = null, $rowIndex = null)
132112
{
133113
$elements = [];
114+
134115
$columnIndex = 0;
135116
foreach ($this->columns as $column) {
136117
/* @var $column BaseColumn */
137118
$column->setModel($item);
138-
$elements[] = $this->renderCellContent($column, $index, $columnIndex++);
119+
$elements[] = $this->renderCellContent($column, $index, $columnIndex, $rowIndex);
120+
$columnIndex++;
139121
}
140122

141123
$content = Html::tag('div', implode("\n", $elements), $this->prepareRowOptions($index, $item));
@@ -174,10 +156,11 @@ protected function prepareRowOptions($index, $item)
174156
* @param BaseColumn $column
175157
* @param int|null $index
176158
* @param int|null $columnIndex
159+
* @param int|null $rowIndex
177160
* @return string
178161
* @throws \Exception
179162
*/
180-
public function renderCellContent($column, $index, $columnIndex = null)
163+
public function renderCellContent($column, $index = null, $columnIndex = null, $rowIndex = null)
181164
{
182165
$id = $column->getElementId($index);
183166
$name = $column->getElementName($index);
@@ -263,9 +246,10 @@ public function renderCellContent($column, $index, $columnIndex = null)
263246

264247
// first line
265248
if ($columnIndex == 0) {
266-
if ($this->max !== $this->min) {
267-
$content .= $this->renderActionColumn($index);
249+
if (!$this->isFixedNumberOfRows()) {
250+
$content .= $this->renderActionColumn($index, $column->getModel(), $rowIndex);
268251
}
252+
269253
if ($this->cloneButton) {
270254
$content .= $this->renderCloneColumn();
271255
}
@@ -287,14 +271,15 @@ public function renderCellContent($column, $index, $columnIndex = null)
287271
* @param null|ActiveRecordInterface|array $item
288272
* @return string
289273
*/
290-
private function renderActionColumn($index = null, $item = null)
274+
private function renderActionColumn($index = null, $item = null, $rowIndex = null)
291275
{
292-
$content = $this->getActionButton($index) . $this->getExtraButtons($index, $item);
276+
$content = $this->getActionButton($index, $rowIndex) . $this->getExtraButtons($index, $item);
293277

294278
$options = ['class' => 'list-cell__button'];
295279
$layoutConfig = array_merge([
296280
'buttonActionClass' => $this->isBootstrapTheme() ? 'col-sm-offset-0 col-sm-2' : '',
297281
], $this->layoutConfig);
282+
298283
Html::addCssClass($options, $layoutConfig['buttonActionClass']);
299284

300285
return Html::tag('div', $content, $options);
@@ -317,18 +302,20 @@ private function renderCloneColumn()
317302
return Html::tag('div', $this->renderCloneButton(), $options);
318303
}
319304

320-
private function getActionButton($index)
305+
private function getActionButton($index, $rowIndex)
321306
{
322307
if ($index === null || $this->min === 0) {
323308
return $this->renderRemoveButton();
324309
}
325310

326-
$index++;
327-
if ($index < $this->min) {
311+
// rowIndex is zero-based, so we have to increment it to properly cpmpare it with min number of rows
312+
$rowIndex++;
313+
314+
if ($rowIndex < $this->min) {
328315
return '';
329316
}
330317

331-
if ($index === $this->min) {
318+
if ($rowIndex === $this->min) {
332319
return $this->isAddButtonPositionRow() ? $this->renderAddButton() : '';
333320
}
334321

src/renderers/ListRenderer.php

+13-30
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,7 @@ public function renderFooter()
102102
*/
103103
protected function renderBody()
104104
{
105-
$rows = [];
106-
107-
if ($this->data) {
108-
$j = 0;
109-
foreach ($this->data as $index => $item) {
110-
if ($j++ <= $this->max) {
111-
$rows[] = $this->renderRowContent($index, $item);
112-
} else {
113-
break;
114-
}
115-
}
116-
for ($i = $j; $i < $this->min; $i++) {
117-
$rows[] = $this->renderRowContent($i);
118-
}
119-
} elseif ($this->min > 0) {
120-
for ($i = 0; $i < $this->min; $i++) {
121-
$rows[] = $this->renderRowContent($i);
122-
}
123-
}
124-
125-
return Html::tag('tbody', implode("\n", $rows));
105+
return Html::tag('tbody', implode("\n", $this->renderRows()));
126106
}
127107

128108
/**
@@ -133,7 +113,7 @@ protected function renderBody()
133113
* @return mixed
134114
* @throws InvalidConfigException
135115
*/
136-
private function renderRowContent($index = null, $item = null)
116+
protected function renderRowContent($index = null, $item = null, $rowIndex = null)
137117
{
138118
$elements = [];
139119

@@ -146,8 +126,8 @@ private function renderRowContent($index = null, $item = null)
146126

147127
$content = [];
148128
$content[] = Html::tag('td', implode("\n", $elements));
149-
if ($this->max !== $this->min) {
150-
$content[] = $this->renderActionColumn($index);
129+
if (!$this->isFixedNumberOfRows()) {
130+
$content[] = $this->renderActionColumn($index, $item, $rowIndex);
151131
}
152132

153133
if ($this->cloneButton) {
@@ -290,12 +270,13 @@ public function renderCellContent($column, $index, $columnIndex = null)
290270
*
291271
* @param null|int $index
292272
* @param null|ActiveRecordInterface|array $item
273+
* @param null|int $rowIndex
293274
* @return string
294275
* @throws \Exception
295276
*/
296-
private function renderActionColumn($index = null, $item = null)
277+
private function renderActionColumn($index = null, $item = null, $rowIndex = null)
297278
{
298-
$content = $this->getActionButton($index) . $this->getExtraButtons($index, $item);
279+
$content = $this->getActionButton($index, $rowIndex) . $this->getExtraButtons($index, $item);
299280

300281
return Html::tag('td', $content, [
301282
'class' => 'list-cell__button',
@@ -315,18 +296,20 @@ private function renderCloneColumn()
315296
]);
316297
}
317298

318-
private function getActionButton($index)
299+
private function getActionButton($index, $rowIndex)
319300
{
320301
if ($index === null || $this->min === 0) {
321302
return $this->renderRemoveButton();
322303
}
323304

324-
$index++;
325-
if ($index < $this->min) {
305+
// rowIndex is zero-based, so we have to increment it to properly cpmpare it with min number of rows
306+
$rowIndex++;
307+
308+
if ($rowIndex < $this->min) {
326309
return '';
327310
}
328311

329-
if ($index === $this->min) {
312+
if ($rowIndex === $this->min) {
330313
return $this->isAddButtonPositionRow() ? $this->renderAddButton() : '';
331314
}
332315

0 commit comments

Comments
 (0)