alexwenzel / ajax-select
Laravel Nova 4 字段 - Ajax 选择
1.0
2022-05-08 16:24 UTC
Requires
- php: ^8.0
This package is auto-updated.
Last update: 2024-08-29 06:01:56 UTC
README
基于其他字段值和它们变化时填充的 Ajax 选择字段。
这是基于 https://github.com/dillingham/nova-ajax-select 的分支,并且**仅支持 nova 4 和 php 8**。
安装
composer require alexwenzel/ajax-select
使用方法
指定请求 URL 以及可选的 parent($attribute) 以观察并触发 Ajax 选择
use NovaAjaxSelect\AjaxSelect;
BelongsTo::make('Company'), AjaxSelect::make('User') ->get('/api/company/{company}/users') ->parent('company'),
添加索引和详细视图显示的字段。AjaxSelect 仅用于表单
BelongsTo::make('User')->exceptOnForms(),
请求 URL
在上面的示例中,我们说 company 是父字段。
{company} URL 参数将与选中的 公司 字段值相等。
响应格式
选择字段期望一个 value 和 display。将结果映射如下
Route::get('api/company/{company}/users', function($company_id) { $company = \App\Company::find($company_id); return $company->users->map(function($user) { return [ 'value' => $user->id, 'display' => $user->name ]; }); })->middleware(['nova']);
使子项依赖于其他子项
城市 根据所选 州 进行请求,而 州 根据所选 国家 进行请求
Select::make('Country') ->options([]), AjaxSelect::make('State') ->get('/api/country/{country}/states') ->parent('country'), AjaxSelect::make('City') ->get('/api/state/{state}/cities') ->parent('state'),
使多个子项依赖于一个父项
文件 和 评论 都将根据所选 项目 进行请求
BelongsTo::make('Project'), AjaxSelect::make('File') ->get('/{project}/files') ->parent('project'), AjaxSelect::make('Comment') ->get('/{project}/comments') ->parent('project'),
