Skip to content

Commit

Permalink
Merge pull request #380 from unclead/fix_rendering_action_buttons
Browse files Browse the repository at this point in the history
Fix rendering action buttons
  • Loading branch information
unclead authored Apr 21, 2024
2 parents c9a236d + fa5ac6a commit 21d0138
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 98 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Yii2 multiple input change log
2.30.0 (in development)
=======================

2.30.0
=======================
- #369 fix rendering action buttons in case the dataset contains non-numeric indices (unclead)

2.29.0
=======================
- fix addind active form fields doesn't work properly in case of 10 rows and more (unclead)
Expand Down
32 changes: 32 additions & 0 deletions src/renderers/BaseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ protected function isAddButtonPositionRowBegin()
return in_array(self::POS_ROW_BEGIN, $this->addButtonPosition);
}

protected function isFixedNumberOfRows()
{
return $this->max === $this->min;
}

private function prepareIndexPlaceholder()
{
$this->indexPlaceholder = 'multiple_index_' . $this->id;
Expand Down Expand Up @@ -579,4 +584,31 @@ public function isBootstrapTheme()
return $this->theme === self::THEME_BS;
}

protected function renderRows()
{
$rows = [];

$rowIndex = 0;
if ($this->data) {
foreach ($this->data as $index => $item) {
if ($rowIndex <= $this->max) {
$rows[] = $this->renderRowContent($index, $item, $rowIndex);
} else {
break;
}
$rowIndex++;
}
for (; $rowIndex < $this->min; $rowIndex++) {
$rows[] = $this->renderRowContent($rowIndex, null, $rowIndex);
}
} elseif ($this->min > 0) {
for (; $rowIndex < $this->min; $rowIndex++) {
$rows[] = $this->renderRowContent($rowIndex, null, $rowIndex);
}
}

return $rows;
}

abstract protected function renderRowContent($index = null, $item = null, $rowIndex = null);
}
51 changes: 19 additions & 32 deletions src/renderers/DivRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,7 @@ public function renderFooter()
*/
protected function renderBody()
{
$rows = [];

if ($this->data) {
$j = 0;
foreach ($this->data as $index => $item) {
if ($j++ <= $this->max) {
$rows[] = $this->renderRowContent($index, $item);
} else {
break;
}
}
for ($i = $j; $i < $this->min; $i++) {
$rows[] = $this->renderRowContent($i);
}
} elseif ($this->min > 0) {
for ($i = 0; $i < $this->min; $i++) {
$rows[] = $this->renderRowContent($i);
}
}

return implode("\n", $rows);
return implode("\n", $this->renderRows());
}

/**
Expand All @@ -128,14 +108,16 @@ protected function renderBody()
* @param ActiveRecordInterface|array $item
* @return mixed
*/
private function renderRowContent($index = null, $item = null)
protected function renderRowContent($index = null, $item = null, $rowIndex = null)
{
$elements = [];

$columnIndex = 0;
foreach ($this->columns as $column) {
/* @var $column BaseColumn */
$column->setModel($item);
$elements[] = $this->renderCellContent($column, $index, $columnIndex++);
$elements[] = $this->renderCellContent($column, $index, $columnIndex, $rowIndex);
$columnIndex++;
}

$content = Html::tag('div', implode("\n", $elements), $this->prepareRowOptions($index, $item));
Expand Down Expand Up @@ -174,10 +156,11 @@ protected function prepareRowOptions($index, $item)
* @param BaseColumn $column
* @param int|null $index
* @param int|null $columnIndex
* @param int|null $rowIndex
* @return string
* @throws \Exception
*/
public function renderCellContent($column, $index, $columnIndex = null)
public function renderCellContent($column, $index = null, $columnIndex = null, $rowIndex = null)
{
$id = $column->getElementId($index);
$name = $column->getElementName($index);
Expand Down Expand Up @@ -263,9 +246,10 @@ public function renderCellContent($column, $index, $columnIndex = null)

// first line
if ($columnIndex == 0) {
if ($this->max !== $this->min) {
$content .= $this->renderActionColumn($index);
if (!$this->isFixedNumberOfRows()) {
$content .= $this->renderActionColumn($index, $column->getModel(), $rowIndex);
}

if ($this->cloneButton) {
$content .= $this->renderCloneColumn();
}
Expand All @@ -287,14 +271,15 @@ public function renderCellContent($column, $index, $columnIndex = null)
* @param null|ActiveRecordInterface|array $item
* @return string
*/
private function renderActionColumn($index = null, $item = null)
private function renderActionColumn($index = null, $item = null, $rowIndex = null)
{
$content = $this->getActionButton($index) . $this->getExtraButtons($index, $item);
$content = $this->getActionButton($index, $rowIndex) . $this->getExtraButtons($index, $item);

$options = ['class' => 'list-cell__button'];
$layoutConfig = array_merge([
'buttonActionClass' => $this->isBootstrapTheme() ? 'col-sm-offset-0 col-sm-2' : '',
], $this->layoutConfig);

Html::addCssClass($options, $layoutConfig['buttonActionClass']);

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

private function getActionButton($index)
private function getActionButton($index, $rowIndex)
{
if ($index === null || $this->min === 0) {
return $this->renderRemoveButton();
}

$index++;
if ($index < $this->min) {
// rowIndex is zero-based, so we have to increment it to properly cpmpare it with min number of rows
$rowIndex++;

if ($rowIndex < $this->min) {
return '';
}

if ($index === $this->min) {
if ($rowIndex === $this->min) {
return $this->isAddButtonPositionRow() ? $this->renderAddButton() : '';
}

Expand Down
43 changes: 13 additions & 30 deletions src/renderers/ListRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,7 @@ public function renderFooter()
*/
protected function renderBody()
{
$rows = [];

if ($this->data) {
$j = 0;
foreach ($this->data as $index => $item) {
if ($j++ <= $this->max) {
$rows[] = $this->renderRowContent($index, $item);
} else {
break;
}
}
for ($i = $j; $i < $this->min; $i++) {
$rows[] = $this->renderRowContent($i);
}
} elseif ($this->min > 0) {
for ($i = 0; $i < $this->min; $i++) {
$rows[] = $this->renderRowContent($i);
}
}

return Html::tag('tbody', implode("\n", $rows));
return Html::tag('tbody', implode("\n", $this->renderRows()));
}

/**
Expand All @@ -133,7 +113,7 @@ protected function renderBody()
* @return mixed
* @throws InvalidConfigException
*/
private function renderRowContent($index = null, $item = null)
protected function renderRowContent($index = null, $item = null, $rowIndex = null)
{
$elements = [];

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

$content = [];
$content[] = Html::tag('td', implode("\n", $elements));
if ($this->max !== $this->min) {
$content[] = $this->renderActionColumn($index);
if (!$this->isFixedNumberOfRows()) {
$content[] = $this->renderActionColumn($index, $item, $rowIndex);
}

if ($this->cloneButton) {
Expand Down Expand Up @@ -290,12 +270,13 @@ public function renderCellContent($column, $index, $columnIndex = null)
*
* @param null|int $index
* @param null|ActiveRecordInterface|array $item
* @param null|int $rowIndex
* @return string
* @throws \Exception
*/
private function renderActionColumn($index = null, $item = null)
private function renderActionColumn($index = null, $item = null, $rowIndex = null)
{
$content = $this->getActionButton($index) . $this->getExtraButtons($index, $item);
$content = $this->getActionButton($index, $rowIndex) . $this->getExtraButtons($index, $item);

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

private function getActionButton($index)
private function getActionButton($index, $rowIndex)
{
if ($index === null || $this->min === 0) {
return $this->renderRemoveButton();
}

$index++;
if ($index < $this->min) {
// rowIndex is zero-based, so we have to increment it to properly cpmpare it with min number of rows
$rowIndex++;

if ($rowIndex < $this->min) {
return '';
}

if ($index === $this->min) {
if ($rowIndex === $this->min) {
return $this->isAddButtonPositionRow() ? $this->renderAddButton() : '';
}

Expand Down
Loading

0 comments on commit 21d0138

Please sign in to comment.