Yii2: Сделать отключаемую функцию data-key в GridView

Created on 12 Jun 2018  ·  2Comments  ·  Source: yiisoft/yii2

data-key указывается в каждой строчке таблицы GridView.

Допустим у меня появилась задача скрыть реальный PK и подставить скрытый ID являющийся хешем. GridView все равно будет выводить оригинальный PK где-то вглубине, что недопустимо.

Хочется сделать параметр отключаемым. Да и вообще зачем оно здесь?
Как я понял для работы чек боксов (yii.gridView.js => getSelectedRows), но ведь это не "ответственность строки", а ответственность самого чек бокса.

Привожу место в коде где создается data-key

    /**
     * Renders a table row with the given data model and key.
     * @param mixed $model the data model to be rendered
     * @param mixed $key the key associated with the data model
     * @param int $index the zero-based index of the data model among the model array returned by [[dataProvider]].
     * @return string the rendering result
     */
    public function renderTableRow($model, $key, $index)
    {
        $cells = [];
        /* @var $column Column */
        foreach ($this->columns as $column) {
            $cells[] = $column->renderDataCell($model, $key, $index);
        }
        if ($this->rowOptions instanceof Closure) {
            $options = call_user_func($this->rowOptions, $model, $key, $index, $this);
        } else {
            $options = $this->rowOptions;
        }
        $options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;

        return Html::tag('tr', implode('', $cells), $options);
    }

| Q | A
| ---------------- | ---
| Yii version | 2.0.?
| PHP version |
| Operating system |

Most helpful comment

Привет, на сколько я помню ключ можно изменить через провайдер данных, через Свойство key

// use "slug" column as key values
$provider = new ActiveDataProvider([
    'query' => Post::find(),
    'key' => 'slug',
]);

// use the result of md5(id) as key values
$provider = new ActiveDataProvider([
    'query' => Post::find(),
    'key' => function ($model) {
        return md5($model->id);
    }
]);

All 2 comments

Привет, на сколько я помню ключ можно изменить через провайдер данных, через Свойство key

// use "slug" column as key values
$provider = new ActiveDataProvider([
    'query' => Post::find(),
    'key' => 'slug',
]);

// use the result of md5(id) as key values
$provider = new ActiveDataProvider([
    'query' => Post::find(),
    'key' => function ($model) {
        return md5($model->id);
    }
]);

det er jeg helt enig i

Was this page helpful?
0 / 5 - 0 ratings