Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add autocomplete #1514

Merged
merged 15 commits into from Oct 7, 2021
Merged

add autocomplete #1514

merged 15 commits into from Oct 7, 2021

Conversation

edwinhuish
Copy link
Contributor

本地模式

option为字符串:

$form->autocomplete($column[, $label])->options(['foo', 'bar', 'Option name']);

option 为数组:(value键为必须,data可省略)

$form->autocomplete($column[, $label])->options([
    ['value' => 'foo', 'data'=>['group'=>'group a']],
    ['value' => 'bar', 'data'=>['group'=>'group a']],
    ['value' => 'option name', 'data'=>['group'=>'other group name']],
    ....
]);

设定group有更简便的方式,使用方式和select类似:

$form->autocomplete($column[, $label])->groups([
    [
        'label'=>'xxx',
        'options'=>['foo', 'bar', 'other option', ...],
    ],
    ....
]);

以上两种方法都支持闭包,闭包需要返回以上对应的array格式:

$form->autocomplete($column[, $label])->options(function($model, $value){
    return $model->all()->pluck('name');
});

远程API模式

  /**
    * ajax 函数的第一个参数为 ajax url, 第二个参数为 valueField(可选), 第三个参数为 groupField(可选)
    */
  $form->autocomplete($column[, $label])->ajax('/countries', 'name', 'region');

远程API 服务端的请求参数为query,示例代码如下:

class CountryController extends AdminController
{
    public function search()
    {
        $countries = Country::when(request('query'), function ($query, $value) {
            $query->where('name', 'like', "%{$value}%");
        })->get();

        return Admin::json($countries->toArray());
    }
}

自定义 autocomplete 的设置:

详细设置请参考: devbridge/jQuery-Autocomplete

$js = <<<JS
    function (suggestion) {
        alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
    }
JS;

$form->autocomplete($column[, $label])->configs([
    'minChars' => 5,
    'noCache' => true,
    'onSelect' => JavaScript::make($js),
]);

configs 同样也支持闭包:

$form->autocomplete($column[, $label])->configs(function($model, $value){
    return [
        ....
    ];
});

联动:

autocomplete 的联动逻辑和select的刚好相反。
depends 需要写在受影响的字段,不限上级字段类型,上级字段的值将同时上传到API。

$form->select('region')->options([
    'asia',
    'Africa',
    'America',
    'Europe',
]);

$form->autocomplete('country')->ajax('/countries', 'name', 'region');

// 将会发出 /states?query={query}&region={region}&country={country}  的请求
$form->autocomplete('addr')->ajax('/states', 'name')->depends(['region', 'country']);

@jqhph jqhph merged commit ed0d4d2 into jqhph:2.0 Oct 7, 2021
@jqhph
Copy link
Owner

jqhph commented Oct 7, 2021

感谢

@edwinhuish edwinhuish deleted the add_autocomplete_form_field branch October 9, 2021 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants