0 追蹤者

類別 yii\grid\Column

繼承關係yii\grid\Column » yii\base\BaseObject
實作yii\base\Configurable
子類別yii\grid\ActionColumn, yii\grid\CheckboxColumn, yii\grid\DataColumn, yii\grid\RadioButtonColumn, yii\grid\SerialColumn
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/grid/Column.php

Column 是所有 yii\grid\GridView 欄別類別的基礎類別。

關於 Column 的更多詳細資訊和使用方法,請參閱資料小部件指南文章

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$content callable 這是一個 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
$options array 欄組標籤的 HTML 屬性。 yii\grid\Column
$visible boolean 此欄是否可見。 yii\grid\Column

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫非類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子。 yii\base\BaseObject
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,亦即已定義且非空值。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為空值。 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\base\BaseObject
renderDataCell() 呈現資料格。 yii\grid\Column
renderFilterCell() 呈現篩選器格。 yii\grid\Column
renderFooterCell() 呈現頁尾格。 yii\grid\Column
renderHeaderCell() 呈現標頭格。 yii\grid\Column

受保護的方法

隱藏繼承的方法

方法 描述 定義於
getHeaderCellLabel() 傳回標頭格標籤。 yii\grid\Column
renderDataCellContent() 呈現資料格內容。 yii\grid\Column
renderFilterCellContent() 呈現篩選器格內容。 yii\grid\Column
renderFooterCellContent() 呈現頁尾格內容。 yii\grid\Column
renderHeaderCellContent() 呈現標頭格內容。 yii\grid\Column

屬性詳細資訊

隱藏繼承的屬性

$content public property

這是一個可呼叫的項目,將用於產生每個儲存格的內容。函式簽章應如下所示:function ($model, $key, $index, $column)。其中 $model$key$index 指的是目前正在呈現的列的模型、鍵和索引,而 $column 則是對 yii\grid\Column 物件的參考。

public callable $content null
$contentOptions public property

資料格標籤的 HTML 屬性。這可以是屬性陣列或傳回此類陣列的匿名函式 (Closure)。函式簽章應如下所示:function ($model, $key, $index, $column)。其中 $model$key$index 指的是目前正在呈現的列的模型、鍵和索引,而 $column 則是對 yii\grid\Column 物件的參考。可以使用函式根據該列中的資料,為不同的列指定不同的屬性。

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

$filterOptions public property

篩選器儲存格標籤的 HTML 屬性。

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

public array $filterOptions = []
$footer public property

頁尾格內容。請注意,它不會進行 HTML 編碼。

public string $footer null
$footerOptions public property

頁腳儲存格標籤的 HTML 屬性。

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

public array $footerOptions = []
$grid public property

擁有此欄的網格視圖物件。

public yii\grid\GridView $grid null
$header public property

標頭格內容。請注意,它不會進行 HTML 編碼。

public string|null $header null
$headerOptions public property

標頭儲存格標籤的 HTML 屬性。

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

public array $headerOptions = []
$options public property

欄組標籤的 HTML 屬性。

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

public array $options = []
$visible public property

此欄是否可見。預設值為 true。

public boolean $visible true

方法詳細資訊

隱藏繼承的方法

__call() public method

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

呼叫非類別方法的具名方法。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,將在叫用不明方法時隱含地呼叫。

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

方法名稱

$params array

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

在呼叫不明方法時

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

            
__construct() public method

定義於: 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 method

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

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,將在執行 $value = $object->property; 時隱含地呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws 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 method

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

檢查屬性是否已設定,亦即已定義且非空值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,將在執行 isset($object->property) 時隱含地呼叫。

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

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

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

具名屬性是否已設定 (非空值)。

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

            
__set() public method

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

設定物件屬性的值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,將在執行 $object->property = $value; 時隱含地呼叫。

另請參閱 __get()

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

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws 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 method

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

將物件屬性設定為空值。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,將在執行 unset($object->property) 時隱含地呼叫。

請注意,如果未定義屬性,此方法將不會執行任何動作。如果屬性為唯讀,則會擲回例外狀況。

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

public void __unset ( $name )
$name string

屬性名稱

throws 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 method

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

傳回值,指出屬性是否可讀取。

如果符合以下條件,屬性為可讀取:

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

另請參閱 canSetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可讀取

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

            
canSetProperty() public method

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

傳回值,指出屬性是否可設定。

如果符合以下條件,屬性為可寫入:

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

另請參閱 canGetProperty()

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可寫入

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

            
className() public static method
自 2.0.14 版起已淘汰。在 PHP >=5.5 上,請改用 ::class

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

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

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

            
getHeaderCellLabel() protected method (available since version 2.0.8)

傳回標頭格標籤。

可以覆寫此方法以自訂標頭格的標籤。

protected string getHeaderCellLabel ( )
return string

標籤

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

            
hasMethod() public method

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

傳回值,指出方法是否已定義。

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

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

方法是否已定義

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

            
hasProperty() public method

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

傳回值,指出屬性是否已定義。

如果符合以下條件,則定義屬性:

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

另請參閱

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

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

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

            
init() public method

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

初始化物件。

在物件使用給定組態初始化之後,會在建構函式結尾叫用此方法。

public void init ( )

                public function init()
{
}

            
renderDataCell() public method

呈現資料格。

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

正在呈現的資料模型

$key mixed

與資料模型相關聯的鍵

$index integer

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

return 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 method

呈現資料格內容。

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

資料模型

$key mixed

與資料模型相關聯的鍵

$index integer

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

return string

呈現結果

                protected function renderDataCellContent($model, $key, $index)
{
    if ($this->content !== null) {
        return call_user_func($this->content, $model, $key, $index, $this);
    }
    return $this->grid->emptyCell;
}

            
renderFilterCell() public method

呈現篩選器格。

public void renderFilterCell ( )

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

            
renderFilterCellContent() protected method

呈現篩選器格內容。

預設實作只會呈現空格。可以覆寫此方法以自訂篩選器格的呈現方式 (如果有的話)。

protected string renderFilterCellContent ( )
return string

呈現結果

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

            
renderFooterCell() public method

呈現頁尾格。

public void renderFooterCell ( )

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

            
renderFooterCellContent() protected method

呈現頁尾格內容。

預設實作只會呈現 $footer。可以覆寫此方法以自訂頁尾格的呈現方式。

protected string renderFooterCellContent ( )
return string

呈現結果

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

            
renderHeaderCell() public method

呈現標頭格。

public void renderHeaderCell ( )

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

            
renderHeaderCellContent() protected method

呈現標頭格內容。

預設實作只會呈現 $header。可以覆寫此方法以自訂標頭格的呈現方式。

protected string renderHeaderCellContent ( )
return string

呈現結果

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