我如何在 checkBoxHtmlOptions yii 中使用 $data->id
How can i use $data->id in the checkBoxHtmlOptions yii
我正在使用 yii cgridview
并且我想在复选框中添加 alt。我成功添加了这个,但我想添加 $data->rem_type
我的意思是这个变量。这是我试过的代码
array(
'name' => 'check',
'id' => 'selectedIds',
'value' => '$data->rem_id',
'class' => 'CCheckBoxColumn',
'selectableRows' => '100',
'checkBoxHtmlOptions'=>array(
'alt'=>'$data->rem_type'),
),
但是它产生了这样的 html
<input alt="{$data->rem_type}" value="12" id="selectedIds_0" type="checkbox" name="selectedIds[]">
如果我删除引号(即 'alt'=>$data->rem_type)
),它会显示错误 Undefined variable: data
有人能帮帮我吗??
好的,所以我还没有测试过这个,但我认为你可以这样做:
将 CCheckBoxColumn
扩展为 CheckBoxColumn
并覆盖 getDataCellContent
函数:
class CheckBoxColumn extends CCheckBoxColumn {
public function getDataCellContent($row) {
$data = $this->grid->dataProvider->data[$row];
if ($this->value !== null)
$value = $this->evaluateExpression($this->value, array('data' => $data, 'row' => $row));
elseif ($this->name !== null)
$value = CHtml::value($data, $this->name);
else
$value = $this->grid->dataProvider->keys[$row];
$checked = false;
if ($this->checked !== null)
$checked = $this->evaluateExpression($this->checked, array('data' => $data, 'row' => $row));
$options = $this->checkBoxHtmlOptions;
if ($this->disabled !== null)
$options['disabled'] = $this->evaluateExpression($this->disabled, array('data' => $data, 'row' => $row));
if (array_key_exists("alt", $options)) { //checks if you have set an alt
$options['alt'] = $this->evaluateExpression($options['alt'], array('data' => $data, 'row' => $row)); //if you have it will evaluate the expression
}
$name = $options['name'];
unset($options['name']);
$options['value'] = $value;
$options['id'] = $this->id . '_' . $row;
return CHtml::checkBox($name, $checked, $options);
}
}
我复制了原版的大部分代码function
并添加了这部分:
if (array_key_exists("alt", $options)) { //checks if you have set an alt
$options['alt'] = $this->evaluateExpression($options['alt'], array('data' =>data, 'row' => $row)); //if you have it will evaluate the expression
}
可能有更好的方法,即不复制代码,但我会告诉你这个。
将其保存在您的组件文件夹中,将其包含在您的 config.php
中。
并在您的小部件中使用此 class:
array(
'name' => 'check',
'id' => 'selectedIds',
'value' => '$data->rem_id',
'class' => 'CheckBoxColumn',// <-- instead of CCheckBoxColumn
'selectableRows' => '100',
'checkBoxHtmlOptions'=>array(
'alt'=>'$data->rem_type'),
),
如果有效请告诉我!
我正在使用 yii cgridview
并且我想在复选框中添加 alt。我成功添加了这个,但我想添加 $data->rem_type
我的意思是这个变量。这是我试过的代码
array(
'name' => 'check',
'id' => 'selectedIds',
'value' => '$data->rem_id',
'class' => 'CCheckBoxColumn',
'selectableRows' => '100',
'checkBoxHtmlOptions'=>array(
'alt'=>'$data->rem_type'),
),
但是它产生了这样的 html
<input alt="{$data->rem_type}" value="12" id="selectedIds_0" type="checkbox" name="selectedIds[]">
如果我删除引号(即 'alt'=>$data->rem_type)
),它会显示错误 Undefined variable: data
有人能帮帮我吗??
好的,所以我还没有测试过这个,但我认为你可以这样做:
将 CCheckBoxColumn
扩展为 CheckBoxColumn
并覆盖 getDataCellContent
函数:
class CheckBoxColumn extends CCheckBoxColumn {
public function getDataCellContent($row) {
$data = $this->grid->dataProvider->data[$row];
if ($this->value !== null)
$value = $this->evaluateExpression($this->value, array('data' => $data, 'row' => $row));
elseif ($this->name !== null)
$value = CHtml::value($data, $this->name);
else
$value = $this->grid->dataProvider->keys[$row];
$checked = false;
if ($this->checked !== null)
$checked = $this->evaluateExpression($this->checked, array('data' => $data, 'row' => $row));
$options = $this->checkBoxHtmlOptions;
if ($this->disabled !== null)
$options['disabled'] = $this->evaluateExpression($this->disabled, array('data' => $data, 'row' => $row));
if (array_key_exists("alt", $options)) { //checks if you have set an alt
$options['alt'] = $this->evaluateExpression($options['alt'], array('data' => $data, 'row' => $row)); //if you have it will evaluate the expression
}
$name = $options['name'];
unset($options['name']);
$options['value'] = $value;
$options['id'] = $this->id . '_' . $row;
return CHtml::checkBox($name, $checked, $options);
}
}
我复制了原版的大部分代码function
并添加了这部分:
if (array_key_exists("alt", $options)) { //checks if you have set an alt
$options['alt'] = $this->evaluateExpression($options['alt'], array('data' =>data, 'row' => $row)); //if you have it will evaluate the expression
}
可能有更好的方法,即不复制代码,但我会告诉你这个。
将其保存在您的组件文件夹中,将其包含在您的 config.php
中。
并在您的小部件中使用此 class:
array(
'name' => 'check',
'id' => 'selectedIds',
'value' => '$data->rem_id',
'class' => 'CheckBoxColumn',// <-- instead of CCheckBoxColumn
'selectableRows' => '100',
'checkBoxHtmlOptions'=>array(
'alt'=>'$data->rem_type'),
),
如果有效请告诉我!