0 追蹤者

類別 yii\grid\RadioButtonColumn

繼承關係yii\grid\RadioButtonColumn » yii\grid\Column » yii\base\BaseObject
實作yii\base\Configurable
自版本起可用2.0.11
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/grid/RadioButtonColumn.php

RadioButtonColumn 在網格視圖中顯示單選按鈕的列。

若要將 RadioButtonColumn 新增到 yii\grid\GridView,請將其新增到 columns 配置,如下所示

'columns' => [
    // ...
    [
        'class' => 'yii\grid\RadioButtonColumn',
        'radioOptions' => function ($model) {
             return [
                 'value' => $model['value'],
                 'checked' => $model['value'] == 2
             ];
         }
    ],
]

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$content callable 這是一個可調用的函數,將用於生成每個儲存格的內容。 yii\grid\Column
$contentOptions array|Closure 資料儲存格標籤的 HTML 屬性。 yii\grid\Column
$filterOptions array 篩選器儲存格標籤的 HTML 屬性。 yii\grid\Column
$footer string 頁腳儲存格內容。 yii\grid\Column
$footerOptions array 頁腳儲存格標籤的 HTML 屬性。 yii\grid\Column
$grid yii\grid\GridView 擁有此列的網格視圖物件。 yii\grid\Column
$header string|null 標頭儲存格內容。 yii\grid\Column
$headerOptions array 標頭儲存格標籤的 HTML 屬性。 yii\grid\Column
$name string 單選按鈕輸入欄位的名稱。 yii\grid\RadioButtonColumn
$options array 欄位群組標籤的 HTML 屬性。 yii\grid\Column
$radioOptions array|Closure 單選按鈕的 HTML 屬性。 yii\grid\RadioButtonColumn
$visible boolean 此欄位是否可見。 yii\grid\Column

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫指定的非類別方法。 yii\base\BaseObject
__construct() 建構子。 yii\base\BaseObject
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\BaseObject
className() 傳回此類別的完整命名空間名稱。 yii\base\BaseObject
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\grid\RadioButtonColumn
renderDataCell() 渲染資料儲存格。 yii\grid\Column
renderFilterCell() 渲染篩選器儲存格。 yii\grid\Column
renderFooterCell() 渲染頁腳儲存格。 yii\grid\Column
renderHeaderCell() 渲染標頭儲存格。 yii\grid\Column

保護方法

隱藏繼承的方法

方法 描述 定義於
getHeaderCellLabel() 傳回標頭儲存格標籤。 yii\grid\Column
renderDataCellContent() 渲染資料儲存格內容。 yii\grid\RadioButtonColumn
renderFilterCellContent() 渲染篩選器儲存格內容。 yii\grid\Column
renderFooterCellContent() 渲染頁腳儲存格內容。 yii\grid\Column
renderHeaderCellContent() 渲染標頭儲存格內容。 yii\grid\Column

屬性詳細資訊

隱藏繼承的屬性

$name 公共屬性

單選按鈕輸入欄位的名稱。

public string $name 'radioButtonSelection'
$radioOptions 公共屬性

單選按鈕的 HTML 屬性。這可以是屬性陣列或傳回此類陣列的匿名函數 (Closure)。

該函數的簽名應如下所示: function ($model, $key, $index, $column) 其中 $model$key$index 指的是目前正在渲染的列的模型、鍵和索引,而 $column 是對 yii\grid\RadioButtonColumn 物件的參考。

可以使用函數根據該列中的資料為不同的列分配不同的屬性。 具體來說,如果您想為單選按鈕設定不同的值,您可以透過以下方式使用此選項(在本範例中使用模型的 name 屬性)

'radioOptions' => function ($model, $key, $index, $column) {
    return ['value' => $model->attribute];
}

另請參閱 yii\helpers\Html::renderTagAttributes(),以了解如何渲染屬性的詳細資訊。

方法詳細資訊

隱藏繼承的方法

__call() 公共方法

定義於: yii\base\BaseObject::__call()

呼叫指定的非類別方法。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當未知的方法被調用時,它會被隱式地呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

傳回 mixed

方法傳回值

拋出 yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public 方法

定義於: yii\base\BaseObject::__construct()

建構子。

預設實作會做兩件事

  • 使用給定的配置 $config 初始化物件。
  • 呼叫 init()

如果此方法在子類別中被覆寫,建議

  • 建構子的最後一個參數是一個配置陣列,像此處的 $config 一樣。
  • 在建構子的結尾呼叫父類別的實作。
public void __construct ( $config = [] )
$config array

將用於初始化物件屬性的名稱-值 配對

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public 方法

定義於: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property; 時,它會被隱式地呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

傳回 mixed

屬性值

拋出 yii\base\UnknownPropertyException

如果屬性未定義

拋出 yii\base\InvalidCallException

如果屬性是唯寫的

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public 方法

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property) 時,它會被隱式地呼叫。

請注意,如果屬性未定義,將傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

傳回 boolean

指示具名屬性是否已設定 (非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public 方法

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式地呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

拋出 yii\base\UnknownPropertyException

如果屬性未定義

拋出 yii\base\InvalidCallException

如果屬性是唯讀的

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public 方法

定義於: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property) 時,它會被隱式地呼叫。

請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性是唯讀的,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

拋出 yii\base\InvalidCallException

如果屬性是唯讀的。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public 方法

定義於: yii\base\BaseObject::canGetProperty()

傳回一個值,指示是否可以讀取屬性。

如果屬性是可讀取的,則

  • 類別具有與指定名稱相關聯的 getter 方法 (在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數 (當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

傳回 boolean

指示屬性是否可讀取

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public 方法

定義於: yii\base\BaseObject::canSetProperty()

傳回一個值,指示是否可以設定屬性。

如果屬性是可寫入的,則

  • 類別具有與指定名稱相關聯的 setter 方法 (在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數 (當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

傳回 boolean

指示屬性是否可寫入

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static 方法
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整命名空間名稱。

public static string className ( )
傳回 string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
getHeaderCellLabel() protected 方法 (自 2.0.8 版本起可用)

定義於: yii\grid\Column::getHeaderCellLabel()

傳回標頭儲存格標籤。

此方法可以被覆寫,以自訂標頭儲存格的標籤。

protected string getHeaderCellLabel ( )
傳回 string

標籤

                protected function getHeaderCellLabel()
{
    return $this->grid->emptyCell;
}

            
hasMethod() public 方法

定義於: yii\base\BaseObject::hasMethod()

傳回一個值,指示是否已定義方法。

預設實作是對 php 函數 method_exists() 的呼叫。當您實作了 php 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

傳回 boolean

指示方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public 方法

定義於: yii\base\BaseObject::hasProperty()

傳回一個值,指示是否已定義屬性。

如果屬性已定義,則

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數 (當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

傳回 boolean

指示屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public 方法

初始化物件。

在物件使用給定的配置初始化之後,此方法在建構子結束時被調用。

public void init ( )
拋出 yii\base\InvalidConfigException

如果 $name 未設定。

                public function init()
{
    parent::init();
    if (empty($this->name)) {
        throw new InvalidConfigException('The "name" property must be set.');
    }
}

            
renderDataCell() public 方法

定義於: yii\grid\Column::renderDataCell()

渲染資料儲存格。

public string renderDataCell ( $model, $key, $index )
$model mixed

正在呈現的資料模型

$key mixed

與資料模型相關聯的鍵

$index integer

資料項目的從零開始的索引,位於由 yii\grid\GridView::$dataProvider 傳回的項目陣列中。

傳回 string

呈現結果

                public function renderDataCell($model, $key, $index)
{
    if ($this->contentOptions instanceof Closure) {
        $options = call_user_func($this->contentOptions, $model, $key, $index, $this);
    } else {
        $options = $this->contentOptions;
    }
    return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
}

            
renderDataCellContent() protected 方法

渲染資料儲存格內容。

protected string renderDataCellContent ( $model, $key, $index )
$model mixed

資料模型

$key mixed

與資料模型相關聯的鍵

$index integer

資料模型的從零開始的索引,位於由 yii\grid\GridView::$dataProvider 傳回的模型陣列中。

傳回 string

呈現結果

                protected function renderDataCellContent($model, $key, $index)
{
    if ($this->content !== null) {
        return parent::renderDataCellContent($model, $key, $index);
    }
    if ($this->radioOptions instanceof Closure) {
        $options = call_user_func($this->radioOptions, $model, $key, $index, $this);
    } else {
        $options = $this->radioOptions;
        if (!isset($options['value'])) {
            $options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key;
        }
    }
    $checked = isset($options['checked']) ? $options['checked'] : false;
    return Html::radio($this->name, $checked, $options);
}

            
renderFilterCell() public 方法

定義於: yii\grid\Column::renderFilterCell()

渲染篩選器儲存格。

public void renderFilterCell ( )

                public function renderFilterCell()
{
    return Html::tag('td', $this->renderFilterCellContent(), $this->filterOptions);
}

            
renderFilterCellContent() protected 方法

定義於: yii\grid\Column::renderFilterCellContent()

渲染篩選器儲存格內容。

預設實作僅呈現一個空格。此方法可以被覆寫,以自訂篩選器儲存格的呈現方式 (如果有的話)。

protected string renderFilterCellContent ( )
傳回 string

呈現結果

                protected function renderFilterCellContent()
{
    return $this->grid->emptyCell;
}

            
renderFooterCell() public 方法

定義於: yii\grid\Column::renderFooterCell()

渲染頁腳儲存格。

public void renderFooterCell ( )

                public function renderFooterCell()
{
    return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
}

            
renderFooterCellContent() protected 方法

定義於: yii\grid\Column::renderFooterCellContent()

渲染頁腳儲存格內容。

預設實作僅呈現 $footer。此方法可以被覆寫,以自訂頁腳儲存格的呈現方式。

protected string renderFooterCellContent ( )
傳回 string

呈現結果

                protected function renderFooterCellContent()
{
    return $this->footer !== null && trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
}

            
renderHeaderCell() public 方法

定義於: yii\grid\Column::renderHeaderCell()

渲染標頭儲存格。

public void renderHeaderCell ( )

                public function renderHeaderCell()
{
    return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
}

            
renderHeaderCellContent() protected 方法

定義於: yii\grid\Column::renderHeaderCellContent()

渲染標頭儲存格內容。

預設實作僅呈現 $header。此方法可以被覆寫,以自訂標頭儲存格的呈現方式。

protected string renderHeaderCellContent ( )
傳回 string

呈現結果

                protected function renderHeaderCellContent()
{
    return $this->header !== null && trim($this->header) !== '' ? $this->header : $this->getHeaderCellLabel();
}