xililo/searchable-ajax-select

基于 Yii2 的可搜索 Ajax 选择小部件

安装次数: 11

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

语言:JavaScript

类型:yii2-extension

v1.0.0 2023-06-30 19:53 UTC

This package is auto-updated.

Last update: 2024-09-30 01:32:47 UTC


README

Searchable Ajax Select 是一个 Yii2 小部件,它提供具有动态加载搜索选项能力的可搜索选择输入。

安装

您可以通过运行以下命令通过 Composer 安装此包

使用方法

要在您的 Yii2 应用程序中使用 Searchable Ajax 选择小部件,请按照以下步骤操作

composer require xililo/searchable-ajax-select
  1. 在您的表单视图中使用该小部件:

     use xililo\SearchableAjaxSelect\SearchableAjaxSelect;
     use yii\helpers\Url;
    
     <?= $form->field($model, 'attribute')->widget(SearchableAjaxSelect::class, [
         'ajaxURL' => Url::to(['controller/user-list']), // Replace with your actual AJAX URL
         'placeholder' => 'Search for options...',
         'minimumInputLength' => 3,
     ]) ?>

    根据需要自定义 ajaxURL、placeholder 和 minimumInputLength 选项。

  2. 在您的控制器中

     public function actionUserList($q = null, $id = null) {
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         $out = ['results' => ['id' => '', 'text' => '']];
         if (!is_null($q)) {
             $query = new Query;
             $query->select('id, name AS text')
                 ->from('city')
                 ->where(['like', 'name', $q])
                 ->limit(20);
             $command = $query->createCommand();
             $data = $command->queryAll();
             $out['results'] = array_values($data);
         }
         elseif ($id > 0) {
             $item = City::findOne($id);
             $name = ($item !=null) $item->name : 'null';
             $out['results'] = ['id' => $id, 'text' => $name];
         }
         return $out;
     }

要求

  • PHP >= 5.4
  • Yii2 框架

许可证

此软件包是开源软件,根据 MIT 许可证 许可。