0 關注者

類別 yii\grid\ActionColumn

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

ActionColumn 是 yii\grid\GridView 小部件的一個欄位,用於顯示檢視和操作項目的按鈕。

若要將 ActionColumn 新增至 GridView,請將其新增至 columns 配置,如下所示:

'columns' => [
    // ...
    [
        'class' => ActionColumn::class,
        // you may configure additional properties here
    ],
]

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

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$buttonOptions 陣列 HTML 選項,將應用於預設按鈕 yii\grid\ActionColumn
$buttons 陣列 按鈕渲染回呼。 yii\grid\ActionColumn
$content 可呼叫 這是一個可呼叫的物件,將用於生成每個儲存格的內容。 yii\grid\Column
$contentOptions 陣列|Closure 資料儲存格標籤的 HTML 屬性。 yii\grid\Column
$controller 字串|null 應處理此處指定動作的控制器 ID。 yii\grid\ActionColumn
$filterOptions 陣列 篩選器儲存格標籤的 HTML 屬性。 yii\grid\Column
$footer 字串 頁腳儲存格內容。 yii\grid\Column
$footerOptions 陣列 頁腳儲存格標籤的 HTML 屬性。 yii\grid\Column
$grid yii\grid\GridView 擁有此欄位的網格檢視物件。 yii\grid\Column
$header 字串|null 標頭儲存格內容。 yii\grid\Column
$headerOptions 陣列 標頭儲存格標籤的 HTML 屬性。 yii\grid\ActionColumn
$icons 陣列 按鈕圖示。 yii\grid\ActionColumn
$options 陣列 欄位群組標籤的 HTML 屬性。 yii\grid\Column
$template 字串 用於組成動作欄中每個儲存格的範本。 yii\grid\ActionColumn
$urlCreator 可呼叫|null 使用指定的模型資訊建立按鈕 URL 的回呼。 yii\grid\ActionColumn
$visible 布林值 此欄位是否可見。 yii\grid\Column
$visibleButtons 陣列 每個按鈕的可見性條件。 yii\grid\ActionColumn

公共方法

隱藏繼承的方法

方法 描述 定義於
__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
createUrl() 為給定的動作和模型建立 URL。 yii\grid\ActionColumn
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否已定義屬性。 yii\base\BaseObject
init() 初始化物件。 yii\grid\ActionColumn
renderDataCell() 渲染資料儲存格。 yii\grid\Column
renderFilterCell() 渲染篩選器儲存格。 yii\grid\Column
renderFooterCell() 渲染頁腳儲存格。 yii\grid\Column
renderHeaderCell() 渲染標頭儲存格。 yii\grid\Column

受保護方法

隱藏繼承的方法

方法 描述 定義於
getHeaderCellLabel() 傳回標頭儲存格標籤。 yii\grid\Column
initDefaultButton() 初始化單個按鈕的預設按鈕渲染回呼。 yii\grid\ActionColumn
initDefaultButtons() 初始化預設按鈕渲染回呼。 yii\grid\ActionColumn
renderDataCellContent() 渲染資料儲存格內容。 yii\grid\ActionColumn
renderFilterCellContent() 渲染篩選器儲存格內容。 yii\grid\Column
renderFooterCellContent() 渲染頁腳儲存格內容。 yii\grid\Column
renderHeaderCellContent() 渲染標頭儲存格內容。 yii\grid\Column

屬性詳情

隱藏繼承的屬性

$buttonOptions 公共屬性 (自版本 2.0.4 起可用)

HTML 選項,將應用於預設按鈕

public array $buttonOptions = []
$buttons public property

按鈕渲染回調函數。陣列的鍵名為按鈕名稱(不帶花括號),值為對應的按鈕渲染回調函數。回調函數應使用以下簽名:

function ($url, $model, $key) {
    // return the button HTML code
}

其中 $url 是欄位為按鈕建立的 URL,$model 是目前列呈現的模型物件,而 $key 是資料提供器陣列中模型的鍵。

您可以為按鈕新增更多條件,例如僅在模型可編輯時顯示它(這裡假設您有一個指示狀態的狀態欄位)

[
    'update' => function ($url, $model, $key) {
        return $model->status === 'editable' ? Html::a('Update', $url) : '';
    },
],
public array $buttons = []
$controller public property

應處理此處指定操作的控制器 ID。如果未設定,將使用目前活動的控制器。此屬性主要由 $urlCreator 用於為不同操作建立 URL。此屬性的值將作為每個操作名稱的前綴,以形成操作的路由。

public string|null $controller null
$headerOptions public property

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

public array $headerOptions = [
    
'class' => 'action-column',
]
$icons public property (available since version 2.0.42)

按鈕圖示。陣列的鍵名為圖示名稱,值為對應的 html: `php [

'eye-open' => '<svg ...></svg>',
'pencil' => Html::tag('span', '', ['class' => 'glyphicon glyphicon-pencil'])

] ` 預設為 FontAwesome 5 免費 svg 圖示。

另請參閱 https://fontawesome.com

public array $icons = [
    
'eye-open' => '<svg aria-hidden="true" style="display:inline-block;font-size:inherit;height:1em;overflow:visible;vertical-align:-.125em;width:1.125em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="currentColor" d="M573 241C518 136 411 64 288 64S58 136 3 241a32 32 0 000 30c55 105 162 177 285 177s230-72 285-177a32 32 0 000-30zM288 400a144 144 0 11144-144 144 144 0 01-144 144zm0-240a95 95 0 00-25 4 48 48 0 01-67 67 96 96 0 1092-71z"/></svg>',
    
'pencil' => '<svg aria-hidden="true" style="display:inline-block;font-size:inherit;height:1em;overflow:visible;vertical-align:-.125em;width:1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M498 142l-46 46c-5 5-13 5-17 0L324 77c-5-5-5-12 0-17l46-46c19-19 49-19 68 0l60 60c19 19 19 49 0 68zm-214-42L22 362 0 484c-3 16 12 30 28 28l122-22 262-262c5-5 5-13 0-17L301 100c-4-5-12-5-17 0zM124 340c-5-6-5-14 0-20l154-154c6-5 14-5 20 0s5 14 0 20L144 340c-6 5-14 5-20 0zm-36 84h48v36l-64 12-32-31 12-65h36v48z"/></svg>',
    
'trash' => '<svg aria-hidden="true" style="display:inline-block;font-size:inherit;height:1em;overflow:visible;vertical-align:-.125em;width:.875em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M32 464a48 48 0 0048 48h288a48 48 0 0048-48V128H32zm272-256a16 16 0 0132 0v224a16 16 0 01-32 0zm-96 0a16 16 0 0132 0v224a16 16 0 01-32 0zm-96 0a16 16 0 0132 0v224a16 16 0 01-32 0zM432 32H312l-9-19a24 24 0 00-22-13H167a24 24 0 00-22 13l-9 19H16A16 16 0 000 48v32a16 16 0 0016 16h416a16 16 0 0016-16V48a16 16 0 00-16-16z"/></svg>',
]
$template public property

用於組成操作欄中每個儲存格的範本。花括號括起來的符記被視為控制器操作 ID(在操作欄的上下文中也稱為按鈕名稱)。它們將被 $buttons 中指定的對應按鈕渲染回調函數替換。例如,符記 {view} 將被回調函數 buttons['view'] 的結果替換。如果找不到回調函數,符記將被替換為空字串。

作為範例,若只想擁有檢視和更新按鈕,您可以將 ActionColumn 新增到 GridView 欄,如下所示

['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],

另請參閱 $buttons

public string $template '{view} {update} {delete}'
$urlCreator public property

使用指定的模型資訊建立按鈕 URL 的回調函數。回調函數的簽名應與 createUrl() 的簽名相同。自 2.0.10 版本起,它可以接受額外的參數,該參數指的是欄位實例本身

function (string $action, mixed $model, mixed $key, integer $index, ActionColumn $this) {
    //return string;
}

如果未設定此屬性,則將使用 createUrl() 建立按鈕 URL。

public callable|null $urlCreator null
$visibleButtons public property (available since version 2.0.7)

每個按鈕的可見性條件。陣列的鍵名為按鈕名稱(不帶花括號),值為布林值 true/false 或匿名函式。當此陣列中未指定按鈕名稱時,預設將顯示該按鈕。回調函數必須使用以下簽名:

function ($model, $key, $index) {
    return $model->status === 'editable';
}

或者您可以傳遞布林值

[
    'update' => \Yii::$app->user->can('update'),
],
public array $visibleButtons = []

方法詳情

隱藏繼承的方法

__call() public method

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

呼叫指定的非類別方法。

請勿直接呼叫此方法,因為它是 PHP 魔術方法,將在呼叫未知方法時隱式呼叫。

public mixed __call ( $name, $params )
$name 字串

方法名稱

$params 陣列

方法參數

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 陣列

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

                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 字串

屬性名稱

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()

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

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

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

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

public boolean __isset ( $name )
$name 字串

屬性名稱或事件名稱

return 布林值

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

                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 字串

屬性名稱或事件名稱

$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()

將物件屬性設定為 null。

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

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

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

public void __unset ( $name )
$name 字串

屬性名稱

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 字串

屬性名稱

$checkVars 布林值

是否將成員變數視為屬性

return 布林值

屬性是否可讀取

                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 字串

屬性名稱

$checkVars 布林值

是否將成員變數視為屬性

return 布林值

屬性是否可寫入

                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 字串

此類別的完整名稱。

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

            
createUrl() public method

為給定的動作和模型建立 URL。

每個按鈕和每一列都會呼叫此方法。

public string createUrl ( $action, $model, $key, $index )
$action 字串

按鈕名稱(或操作 ID)

$model yii\db\ActiveRecordInterface

資料模型

$key mixed

與資料模型關聯的鍵

$index integer

目前的列索引

return 字串

建立的 URL

                public function createUrl($action, $model, $key, $index)
{
    if (is_callable($this->urlCreator)) {
        return call_user_func($this->urlCreator, $action, $model, $key, $index, $this);
    }
    $params = is_array($key) ? $key : ['id' => (string) $key];
    $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
    return Url::toRoute($params);
}

            
getHeaderCellLabel() protected method (available since version 2.0.8)

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

傳回標頭儲存格標籤。

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

protected string getHeaderCellLabel ( )
return 字串

標籤

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

            
hasMethod() public method

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

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

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

public boolean hasMethod ( $name )
$name 字串

方法名稱

return 布林值

是否已定義方法

                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 字串

屬性名稱

$checkVars 布林值

是否將成員變數視為屬性

return 布林值

是否已定義屬性

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

            
init() public method

初始化物件。

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

public void init ( )

                public function init()
{
    parent::init();
    $this->initDefaultButtons();
}

            
initDefaultButton() protected method (available since version 2.0.11)

初始化單個按鈕的預設按鈕渲染回呼。

protected void initDefaultButton ( $name, $iconName, $additionalOptions = [] )
$name 字串

範本中寫入的按鈕名稱

$iconName 字串

使 Bootstrap glyphicon 類別獨一無二的部分

$additionalOptions 陣列

其他選項的陣列

                protected function initDefaultButton($name, $iconName, $additionalOptions = [])
{
    if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
        $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
            switch ($name) {
                case 'view':
                    $title = Yii::t('yii', 'View');
                    break;
                case 'update':
                    $title = Yii::t('yii', 'Update');
                    break;
                case 'delete':
                    $title = Yii::t('yii', 'Delete');
                    break;
                default:
                    $title = ucfirst($name);
            }
            $options = array_merge([
                'title' => $title,
                'aria-label' => $title,
                'data-pjax' => '0',
            ], $additionalOptions, $this->buttonOptions);
            $icon = isset($this->icons[$iconName])
                ? $this->icons[$iconName]
                : Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]);
            return Html::a($icon, $url, $options);
        };
    }
}

            
initDefaultButtons() protected method

初始化預設按鈕渲染回呼。

protected void initDefaultButtons ( )

                protected function initDefaultButtons()
{
    $this->initDefaultButton('view', 'eye-open');
    $this->initDefaultButton('update', 'pencil');
    $this->initDefaultButton('delete', 'trash', [
        'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
        'data-method' => 'post',
    ]);
}

            
renderDataCell() public method

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

渲染資料儲存格。

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

正在呈現的資料模型

$key mixed

與資料模型關聯的鍵

$index integer

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

return 字串

呈現結果

                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 字串

呈現結果

                protected function renderDataCellContent($model, $key, $index)
{
    return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
        $name = $matches[1];
        if (isset($this->visibleButtons[$name])) {
            $isVisible = $this->visibleButtons[$name] instanceof \Closure
                ? call_user_func($this->visibleButtons[$name], $model, $key, $index)
                : $this->visibleButtons[$name];
        } else {
            $isVisible = true;
        }
        if ($isVisible && isset($this->buttons[$name])) {
            $url = $this->createUrl($name, $model, $key, $index);
            return call_user_func($this->buttons[$name], $url, $model, $key);
        }
        return '';
    }, $this->template);
}

            
renderFilterCell() public method

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

渲染篩選器儲存格。

public void renderFilterCell ( )

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

            
renderFilterCellContent() protected method

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

渲染篩選器儲存格內容。

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

protected string renderFilterCellContent ( )
return 字串

呈現結果

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

            
renderFooterCell() public method

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

渲染頁腳儲存格。

public void renderFooterCell ( )

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

            
renderFooterCellContent() protected method

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

渲染頁腳儲存格內容。

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

protected string renderFooterCellContent ( )
return 字串

呈現結果

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

            
renderHeaderCell() public method

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

渲染標頭儲存格。

public void renderHeaderCell ( )

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

            
renderHeaderCellContent() protected method

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

渲染標頭儲存格內容。

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

protected string renderHeaderCellContent ( )
return 字串

呈現結果

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