---
url: /docs/forms/table/column.md
---
# CatchTable 表单栏目类文档

## 概述

`Column` 类是 CatchTable 构建器中的一个核心组件，用于定义表格中的列。该类实现了 `JsonSerializable` 接口，允许将列的配置转换为 JSON 格式。

## 类定义

```php
namespace CatchForm\Table;

use JsonSerializable;

class Column implements JsonSerializable
{
    protected array $column = [];

    public function __construct(string $label, string $props = '')
    {
        $this->column['label'] = $label;

        if ($props) {
            $this->column['props'] = $props;
        }
    }
}
```

## 构造函数

### `__construct(string $label, string $props = '')`

* **参数**:
  * `label`: 列的标签。
  * `props`: 列的属性（可选）。

## 可用方法

| 方法                                           | 描述                                               |
| ---------------------------------------------- | -------------------------------------------------- | -------------------- |
| `selection(): static`                          | 设置列为选择列。                                   |
| `type(string $type): static`                   | 设置列的类型。                                     |
| `expand(): static`                             | 设置列为扩展列。                                   |
| `width(int                                     | string $width): static`                            | 设置列的宽度。       |
| `slot(string $slot): static`                   | 设置列的插槽名称。                                 |
| `header(string $header): static`               | 设置列的表头。                                     |
| `align(string $align): static`                 | 设置列的对齐方式。                                 |
| `fixed(string $fixed): static`                 | 设置列的固定位置（如：'fixed'、'right'、'left'）。 |
| `sortable(bool                                 | string $sortable = true): static`                  | 设置列是否可排序。   |
| `resizable(bool $resizable): static`           | 设置列是否可调整大小。                             |
| `headerAlign(int                               | string $align): static`                            | 设置表头的对齐方式。 |
| `show(bool $show): static`                     | 设置列是否显示。                                   |
| `children(array $columns): static`             | 设置子列。                                         |
| `index($index): static`                        | 设置列的索引。                                     |
| `switch(bool $switch = true): static`          | 设置列是否为开关。                                 |
| `ellipsis(bool $ellipsis = true): static`      | 设置列是否使用省略文字。                           |
| `image(bool $image = true): static`            | 设置列是否为图片预览。                             |
| `preview(bool $preview = true): static`        | 设置列是否为预览。                                 |
| `tags(bool                                     | array $tag): static`                               | 设置列的标签。       |
| `link(bool $isLink, string $linkText): static` | 设置列为链接。                                     |
| `update(bool $update = true): static`          | 设置列是否可更新。                                 |
| `destroy(bool $destroy = true): static`        | 设置列是否可删除。                                 |
| `column(string $key, mixed $value): static`    | 设置列的属性。                                     |
| `jsonSerialize(): array`                       | 将列的配置序列化为 JSON 格式。                     |

## 示例

以下是使用 `Column` 类的示例：

```php
use CatchForm\Table\Column;

$column = new Column('姓名', 'name');
$column->width(150) // 设置列宽度
       ->sortable(true) // 使列可排序
       ->align('center') // 设置列内容居中
       ->show(true) // 显示该列
       ->tags(['重要', '用户']); // 添加标签

$json = json_encode($column); // 将列配置转换为 JSON 格式
```

## JSON 序列化

`jsonSerialize()` 方法实现了 `JsonSerializable` 接口，允许将列的配置序列化为 JSON 格式：

```php
public function jsonSerialize(): array
{
    return $this->column;
}
```
