From bfb16b4b4b8e28059ec99b60835ecda78bed13d2 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Mon, 26 Aug 2019 17:50:17 +0530 Subject: [PATCH] Fix #407: Enhance `data-key` generation --- CHANGE.md | 1 + src/ExpandRowColumn.php | 3 +-- src/GridView.php | 21 ++++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGE.md b/CHANGE.md index 486053a2..55c7fa41 100755 --- a/CHANGE.md +++ b/CHANGE.md @@ -6,6 +6,7 @@ Change Log: `yii2-grid` **Date:** _under development_ - (bug #924): Correct `rowOptions` init. +- (bug #407): Enhance `data-key` generation. ## Version 3.3.3 diff --git a/src/ExpandRowColumn.php b/src/ExpandRowColumn.php index 506f129e..e04f5ca3 100644 --- a/src/ExpandRowColumn.php +++ b/src/ExpandRowColumn.php @@ -327,8 +327,7 @@ public function getDataCellValue($model, $key, $index) Html::addCssClass($detailOptions, 'skip-export'); } $detailOptions['data-index'] = $index; - $detailOptions['data-key'] = !is_string($key) && !is_numeric($key) ? - (is_array($key) ? Json::encode($key) : (string)$key) : $key; + $detailOptions['data-key'] = GridView::parseKey($key); Html::addCssClass($detailOptions, ['kv-expanded-row', $this->_colId]); $content = Html::tag('div', $detail, $detailOptions); return <<< HTML diff --git a/src/GridView.php b/src/GridView.php index bce3be49..b2bad667 100644 --- a/src/GridView.php +++ b/src/GridView.php @@ -1254,7 +1254,7 @@ protected function getPageSummaryRow() } $cells[] = $column->renderPageSummaryCell(); if (!empty($column->pageSummaryOptions['colspan'])) { - $span = (int) $column->pageSummaryOptions['colspan']; + $span = (int)$column->pageSummaryOptions['colspan']; $dir = ArrayHelper::getValue($column->pageSummaryOptions, 'data-colspan-dir', 'ltr'); if ($span > 0) { $fm = ($dir === 'ltr') ? ($i + 1) : ($i - $span + 1); @@ -1265,7 +1265,7 @@ protected function getPageSummaryRow() } } } - if (!empty($skipped )) { + if (!empty($skipped)) { for ($i = 0; $i < $cols; $i++) { if (isset($skipped[$i])) { $cells[$i] = ''; @@ -1274,6 +1274,7 @@ protected function getPageSummaryRow() } return implode('', $cells); } + /** * @inheritdoc * @throws InvalidConfigException @@ -1307,11 +1308,21 @@ public function renderTableRow($model, $key, $index) } else { $options = $this->rowOptions; } - $options['data-key'] = is_array($key) ? json_encode($key) : (string) $key; + $options['data-key'] = static::parseKey($key); Html::addCssClass($options, $this->options['id']); return Html::tag('tr', implode('', $cells), $options); } + /** + * Parses the key and returns parsed key value as string based on the data type + * @param mixed $key + * @return string + */ + public static function parseKey($key) + { + return is_array($key) ? Json::encode($key) : (is_object($key) ? serialize($key) : (string)$key); + } + /** * Renders the toggle data button. * @@ -2119,8 +2130,8 @@ protected function renderToolbarContainer() * forcing float-right only if no float is defined in toolbarContainerOptions */ if ( - !strpos($this->toolbarContainerOptions['class'],$this->getCssClass(self::BS_PULL_RIGHT)) - && !strpos($this->toolbarContainerOptions['class'],$this->getCssClass(self::BS_PULL_LEFT)) + !strpos($this->toolbarContainerOptions['class'], $this->getCssClass(self::BS_PULL_RIGHT)) + && !strpos($this->toolbarContainerOptions['class'], $this->getCssClass(self::BS_PULL_LEFT)) ) { $this->addCssClass($this->toolbarContainerOptions, self::BS_PULL_RIGHT); }