---
url: /docs/forms/table/search.md
---
# CatchTable 搜索组件

## 概述

`Search` 类是 CatchTable 搜索组件，用于定义搜索项。该类实现了 `JsonSerializable` 接口，允许将搜索项的配置转换为 JSON 格式。

## 类定义

```php
namespace CatchForm\Table;

use JsonSerializable;

class Search implements JsonSerializable
{
    protected array $item = [];

    public function __construct(string $label)
    {
        $this->item['label'] = $label;
    }
}
```

## 构造函数

### `__construct(string $label)`

* **参数**:
  * `label`: 搜索项的标签。

## 可用方法

| 方法                                           | 描述                                 |
| ---------------------------------------------- | ------------------------------------ |
| `input(string $name): static`                  | 定义一个文本输入框类型的搜索项。     |
| `select(string $name): static`                 | 定义一个下拉选择框类型的搜索项。     |
| `number(string $name): static`                 | 定义一个数字输入框类型的搜索项。     |
| `date(string $name): static`                   | 定义一个日期选择器类型的搜索项。     |
| `datetime(string $name): static`               | 定义一个日期时间选择器类型的搜索项。 |
| `range(string $name, array $children): static` | 定义一个范围搜索项，并添加子项。     |
| `type(string $type, string $name): static`     | 设置搜索项的类型和名称。             |
| `api(string $api): static`                     | 设置搜索项的 API 地址。              |
| `placeholder(string $placeholder): static`     | 设置搜索项的占位符文本。             |
| `default(mixed $default): static`              | 设置搜索项的默认值。                 |
| `options(array $options): static`              | 设置下拉选择框的选项。               |
| `show(array $show): static`                    | 设置搜索项的显示条件。               |
| `props(array $props): static`                  | 设置搜索项的其他属性。               |
| `children(array $children): static`            | 设置搜索项的子项。                   |
| `jsonSerialize(): array`                       | 将搜索项配置序列化为 JSON 格式。     |

## 示例

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

```php
use CatchForm\Table\Search;

$search = new Search('姓名');
$search->input('name') // 定义一个输入框搜索项
       ->placeholder('请输入姓名') // 设置占位符
       ->default('张三') // 设置默认值
       ->api('/api/search'); // 设置 API 地址

$json = json_encode($search); // 将搜索配置转换为 JSON 格式
```

## JSON 序列化

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

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